Skip to content

Commit

Permalink
Respect API version when building URL to downstream swagger.json (#301)
Browse files Browse the repository at this point in the history
* Respect configured API version in downstream path

* Add test, fix implementation
  • Loading branch information
satano authored Jul 15, 2024
1 parent ecc9644 commit b027617
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public class SwaggerService
/// <summary>
/// Gets or sets the path.
/// </summary>
public string Path { get; set; } = "/swagger/v1/swagger.json";
public string Path { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -106,10 +106,16 @@ private async Task<Uri> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit b027617

Please sign in to comment.