From c7f660236131e65dc629257b58648c72589b35dd Mon Sep 17 00:00:00 2001 From: Radoslav Radev Date: Wed, 27 Mar 2024 10:35:11 +0100 Subject: [PATCH] implemented a workaround for Conul service discovary support. (#288) * implemented a workaround for Conul service discovary. all thought the solution in this PR works (please take it as just guidance on how to fix it) , i didnt had time to get fully familiar with your code base and just aplied a hot fix * added changes requested to, accept PR * updated the ServiceProviderType to support null since the conf is null when unit testing --------- Co-authored-by: Radoslav Radev --- .../Middleware/SwaggerForOcelotMiddleware.cs | 16 ++++++++-------- .../DownstreamSwaggerDocsRepository.cs | 5 +++++ .../SwaggerServiceDiscoveryProvider.cs | 4 ++++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs b/src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs index 544c8b9..4e4fa5d 100644 --- a/src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs +++ b/src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs @@ -1,4 +1,4 @@ -using Kros.Utils; +using Kros.Utils; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; using MMLib.SwaggerForOcelot.Configuration; @@ -14,6 +14,7 @@ using Microsoft.OpenApi.Writers; using System.Text; using Microsoft.OpenApi.Models; +using MMLib.SwaggerForOcelot.ServiceDiscovery; namespace MMLib.SwaggerForOcelot.Middleware { @@ -91,15 +92,14 @@ public async Task Invoke(HttpContext context, RouteOptions route = routeOptions.FirstOrDefault(r => r.SwaggerKey == endPoint.Key); string content = await downstreamSwaggerDocs.GetSwaggerJsonAsync(route, endPoint, version); - - if (endPoint.TransformByOcelotConfig) + if (SwaggerServiceDiscoveryProvider.ServiceProviderType != "Consul") { - content = _transformer.Transform( - content, - routeOptions, - GetServerName(context, endPoint), - endPoint); + if (endPoint.TransformByOcelotConfig) + { + content = _transformer.Transform(content, routeOptions, GetServerName(context, endPoint), endPoint); + } } + content = await ReconfigureUpstreamSwagger(context, content); await context.Response.WriteAsync(content); diff --git a/src/MMLib.SwaggerForOcelot/Repositories/DownstreamSwaggerDocsRepository.cs b/src/MMLib.SwaggerForOcelot/Repositories/DownstreamSwaggerDocsRepository.cs index 2c9615b..a597746 100644 --- a/src/MMLib.SwaggerForOcelot/Repositories/DownstreamSwaggerDocsRepository.cs +++ b/src/MMLib.SwaggerForOcelot/Repositories/DownstreamSwaggerDocsRepository.cs @@ -46,6 +46,11 @@ public async Task GetSwaggerJsonAsync( var clientName = _options.Value.HttpClientName ?? ((route?.DangerousAcceptAnyServerCertificateValidator ?? false) ? ServiceCollectionExtensions.IgnoreSslCertificate : string.Empty); var httpClient = _httpClientFactory.CreateClient(clientName); + if (!(url.StartsWith("http://",StringComparison.InvariantCultureIgnoreCase) || url.StartsWith("https://",StringComparison.InvariantCultureIgnoreCase))) + { + url = "https://" + url; + } + SetHttpVersion(httpClient, route); AddHeaders(httpClient); diff --git a/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs b/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs index bab1ea6..bd5b7a3 100644 --- a/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs +++ b/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs @@ -81,6 +81,8 @@ private async Task GetSwaggerUri(SwaggerEndPointConfig endPoint, RouteOptio { var conf = _configurationCreator.Create(_options.CurrentValue.GlobalConfiguration); + ServiceProviderType = conf?.Type; + var downstreamRoute = new DownstreamRouteBuilder() .WithUseServiceDiscovery(true) .WithServiceName(endPoint.Service.Name) @@ -125,6 +127,8 @@ private string GetScheme(ServiceHostAndPort service, RouteOptions route) _ => 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"; } }