diff --git a/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerService.cs b/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerService.cs index 7120519..5487e3b 100644 --- a/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerService.cs +++ b/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerService.cs @@ -13,6 +13,6 @@ public class SwaggerService /// /// Gets or sets the path. /// - public string Path { get; set; } = "/swagger/v1/swagger.json"; + public string Path { get; set; } } } diff --git a/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs b/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs index bd5b7a3..8c89179 100644 --- a/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs +++ b/src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs @@ -1,7 +1,4 @@ -using System; -using System.Linq; -using System.Threading.Tasks; -using Kros.Extensions; +using Kros.Extensions; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; using MMLib.SwaggerForOcelot.Configuration; @@ -13,6 +10,9 @@ using Ocelot.ServiceDiscovery.Providers; using Ocelot.Values; using Swashbuckle.AspNetCore.Swagger; +using System; +using System.Linq; +using System.Threading.Tasks; namespace MMLib.SwaggerForOcelot.ServiceDiscovery { @@ -106,10 +106,16 @@ private async Task GetSwaggerUri(SwaggerEndPointConfig endPoint, RouteOptio throw new InvalidOperationException(GetErrorMessage(endPoint)); } - var builder = new UriBuilder(GetScheme(service, route), service.DownstreamHost, service.DownstreamPort) + var builder = new UriBuilder(GetScheme(service, route), service.DownstreamHost, service.DownstreamPort); + if (endPoint.Service.Path.IsNullOrEmpty()) { - Path = endPoint.Service.Path - }; + string version = endPoint.Version.IsNullOrEmpty() ? "v1" : endPoint.Version; + builder.Path = $"/swagger/{version}/swagger.json"; + } + else + { + builder.Path = endPoint.Service.Path; + } return builder.Uri; } diff --git a/tests/MMLib.SwaggerForOcelot.Tests/ServiceDiscovery/SwaggerServiceDiscoveryProviderShould.cs b/tests/MMLib.SwaggerForOcelot.Tests/ServiceDiscovery/SwaggerServiceDiscoveryProviderShould.cs index b91757c..1463a9e 100644 --- a/tests/MMLib.SwaggerForOcelot.Tests/ServiceDiscovery/SwaggerServiceDiscoveryProviderShould.cs +++ b/tests/MMLib.SwaggerForOcelot.Tests/ServiceDiscovery/SwaggerServiceDiscoveryProviderShould.cs @@ -49,6 +49,22 @@ public async Task ReturnUriFromServiceDiscovery() uri.AbsoluteUri.Should().Be("http://localhost:5000/swagger/v1/json"); } + [Fact] + public async Task RespectApiVersionWhenUriIsNotExplicitlySet() + { + SwaggerServiceDiscoveryProvider provider = CreateProvider(CreateService("Projects", "localhost", 5000)); + + Uri uri = await provider.GetSwaggerUriAsync( + new SwaggerEndPointConfig() + { + Version = "1.0", + Service = new SwaggerService() { Name = "Projects" } + }, + new Configuration.RouteOptions()); + + uri.AbsoluteUri.Should().Be("http://localhost:5000/swagger/1.0/swagger.json"); + } + [Fact] public async Task ReturnUriFromServiceDiscoveryWhenRouteDoesntExist() {