Skip to content

Commit

Permalink
Missing Scheme in URI for Swagger when using PollConsul Service Disco…
Browse files Browse the repository at this point in the history
…very with Ocelot
  • Loading branch information
rabdulatif committed Oct 4, 2024
1 parent 3066153 commit 04778d0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public async Task Invoke(HttpContext context,
ISwaggerEndPointProvider swaggerEndPointRepository,
IDownstreamSwaggerDocsRepository downstreamSwaggerDocs)
{
(string version, SwaggerEndPointOptions endPoint) = GetEndPoint(context.Request.Path, swaggerEndPointRepository);
(string version, SwaggerEndPointOptions endPoint) =
GetEndPoint(context.Request.Path, swaggerEndPointRepository);

if (_downstreamInterceptor is not null &&
!_downstreamInterceptor.DoDownstreamSwaggerEndpoint(context, version, endPoint))
Expand All @@ -92,7 +93,8 @@ public async Task Invoke(HttpContext context,
RouteOptions route = routeOptions.FirstOrDefault(r => r.SwaggerKey == endPoint.Key);

string content = await downstreamSwaggerDocs.GetSwaggerJsonAsync(route, endPoint, version);
if (SwaggerServiceDiscoveryProvider.ServiceProviderType != "Consul")
if (SwaggerServiceDiscoveryProvider.ServiceProviderType != "Consul" &&
SwaggerServiceDiscoveryProvider.ServiceProviderType != "PollConsul")
{
if (endPoint.TransformByOcelotConfig)
{
Expand Down Expand Up @@ -125,7 +127,7 @@ private string GetServerName(HttpContext context, SwaggerEndPointOptions endPoin
if (string.IsNullOrWhiteSpace(_options.ServerOcelot))
{
serverName = endPoint.HostOverride
?? $"{context.Request.Scheme}://{context.Request.Host.Value.RemoveSlashFromEnd()}";
?? $"{context.Request.Scheme}://{context.Request.Host.Value.RemoveSlashFromEnd()}";
}
else
{
Expand All @@ -137,7 +139,8 @@ private string GetServerName(HttpContext context, SwaggerEndPointOptions endPoin

private async Task<string> ReconfigureUpstreamSwagger(HttpContext context, string swaggerJson)
{
if (_options.ReConfigureUpstreamSwaggerJson is not null && _options.ReConfigureUpstreamSwaggerJsonAsync is not null)
if (_options.ReConfigureUpstreamSwaggerJson is not null &&
_options.ReConfigureUpstreamSwaggerJsonAsync is not null)
{
throw new Exception(
"Both ReConfigureUpstreamSwaggerJson and ReConfigureUpstreamSwaggerJsonAsync cannot have a value. Only use one method.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ private async Task<Uri> GetSwaggerUri(SwaggerEndPointConfig endPoint, RouteOptio
}

var builder = new UriBuilder(GetScheme(service, route), service.DownstreamHost, service.DownstreamPort);
if (builder.Scheme.IsNullOrEmpty())
builder.Scheme = conf?.Scheme ?? "http";

if (endPoint.Service.Path.IsNullOrEmpty())
{
string version = endPoint.Version.IsNullOrEmpty() ? "v1" : endPoint.Version;
Expand All @@ -122,19 +125,20 @@ private async Task<Uri> GetSwaggerUri(SwaggerEndPointConfig endPoint, RouteOptio

private string GetScheme(ServiceHostAndPort service, RouteOptions route)
=> (route is not null && !route.DownstreamScheme.IsNullOrEmpty())
? route.DownstreamScheme
: !service.Scheme.IsNullOrEmpty()
? service.Scheme
: service.DownstreamPort
switch
{
443 => Uri.UriSchemeHttps,
80 => Uri.UriSchemeHttp,
_ => string.Empty,
};
? route.DownstreamScheme
: !service.Scheme.IsNullOrEmpty()
? service.Scheme
: service.DownstreamPort
switch
{
443 => Uri.UriSchemeHttps,
80 => Uri.UriSchemeHttp,
_ => string.Empty,
};

public static string? ServiceProviderType { get; set; }

Check warning on line 139 in src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs

View workflow job for this annotation

GitHub Actions / build-and-test

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 139 in src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Missing XML comment for publicly visible type or member 'SwaggerServiceDiscoveryProvider.ServiceProviderType'

private static string GetErrorMessage(SwaggerEndPointConfig endPoint) => $"Service with swagger documentation '{endPoint.Service.Name}' cann't be discovered";
private static string GetErrorMessage(SwaggerEndPointConfig endPoint) =>
$"Service with swagger documentation '{endPoint.Service.Name}' cann't be discovered";
}
}

0 comments on commit 04778d0

Please sign in to comment.