From 04778d0ee5ce8924063767f214305e89e68f1a4a Mon Sep 17 00:00:00 2001 From: rabdulatif Date: Fri, 4 Oct 2024 17:55:08 +0500 Subject: [PATCH 1/2] Missing Scheme in URI for Swagger when using PollConsul Service Discovery with Ocelot --- .../Middleware/SwaggerForOcelotMiddleware.cs | 11 +++++--- .../SwaggerServiceDiscoveryProvider.cs | 26 +++++++++++-------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs b/src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs index b5d99d1..d15c279 100644 --- a/src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs +++ b/src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs @@ -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)) @@ -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) { @@ -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 { @@ -137,7 +139,8 @@ private string GetServerName(HttpContext context, SwaggerEndPointOptions endPoin private async Task 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."); diff --git a/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs b/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs index 2cc8b7e..5056fa4 100644 --- a/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs +++ b/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs @@ -107,6 +107,9 @@ private async Task 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; @@ -122,19 +125,20 @@ private async Task 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; } - 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"; } } From c676fe91d80a565dbcdcdcc1d32700c7a2674948 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=88o=20Martiniak?= Date: Fri, 4 Oct 2024 15:41:39 +0200 Subject: [PATCH 2/2] Update src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs --- .../ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs b/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs index 5056fa4..2769c73 100644 --- a/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs +++ b/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs @@ -108,6 +108,9 @@ private async Task GetSwaggerUri(SwaggerEndPointConfig endPoint, RouteOptio var builder = new UriBuilder(GetScheme(service, route), service.DownstreamHost, service.DownstreamPort); if (builder.Scheme.IsNullOrEmpty()) + { + builder.Scheme = conf?.Scheme ?? "http"; + } builder.Scheme = conf?.Scheme ?? "http"; if (endPoint.Service.Path.IsNullOrEmpty())