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

Respect API version when building URL to downstream swagger.json #301

Merged
merged 2 commits into from
Jul 15, 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 @@ -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 All @@ -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 @@ -106,10 +106,16 @@
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 All @@ -127,7 +133,7 @@
_ => string.Empty,
};

public static string? ServiceProviderType { get; set; }

Check warning on line 136 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 136 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";
}
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
Loading