Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing URI scheme in Swagger when using PollConsul service discovery #308

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -28,7 +28,7 @@
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IOptions<SwaggerOptions> _swaggerOptions;

public SwaggerServiceDiscoveryProvider(

Check warning on line 31 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.SwaggerServiceDiscoveryProvider(IServiceDiscoveryProviderFactory, IServiceProviderConfigurationCreator, IOptionsMonitor<FileConfiguration>, IHttpContextAccessor, IOptions<SwaggerOptions>)'
IServiceDiscoveryProviderFactory serviceDiscovery,
IServiceProviderConfigurationCreator configurationCreator,
IOptionsMonitor<FileConfiguration> options,
Expand Down Expand Up @@ -107,6 +107,12 @@
}

var builder = new UriBuilder(GetScheme(service, route), service.DownstreamHost, service.DownstreamPort);
if (builder.Scheme.IsNullOrEmpty())
Burgyn marked this conversation as resolved.
Show resolved Hide resolved
{
builder.Scheme = conf?.Scheme ?? "http";
}
builder.Scheme = conf?.Scheme ?? "http";

Comment on lines +110 to +115
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Approve changes and remove duplicate line

The added check for a null or empty scheme addresses the PR objective of fixing the missing URI scheme issue in Swagger. This change looks good and should resolve the reported problem.

However, there's a duplicate line that should be removed:

Please remove the duplicate line:

            if (builder.Scheme.IsNullOrEmpty())
            {
                builder.Scheme = conf?.Scheme ?? "http";
            }
-           builder.Scheme = conf?.Scheme ?? "http";
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (builder.Scheme.IsNullOrEmpty())
{
builder.Scheme = conf?.Scheme ?? "http";
}
builder.Scheme = conf?.Scheme ?? "http";
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 +128,20 @@

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 142 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 142 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";
}
}
Loading