Skip to content

Commit

Permalink
Support multiple instances of Swagger middleware with scoped options
Browse files Browse the repository at this point in the history
  • Loading branch information
rmorris committed Feb 16, 2021
1 parent 392de3e commit 89d75c1
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<LicenseUrl>https://licenses.nuget.org/MIT</LicenseUrl>
<VersionPrefix>6.0.6</VersionPrefix>
<VersionPrefix>6.0.7</VersionPrefix>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ public static IApplicationBuilder UseSwagger(
this IApplicationBuilder app,
Action<SwaggerOptions> setupAction = null)
{
SwaggerOptions options = app.ApplicationServices.GetRequiredService<IOptions<SwaggerOptions>>().Value;
setupAction?.Invoke(options);
SwaggerOptions options;
using (var scope = app.ApplicationServices.CreateScope())
{
options = scope.ServiceProvider.GetRequiredService<IOptionsSnapshot<SwaggerOptions>>().Value;
setupAction?.Invoke(options);
}

return app.UseSwagger(options);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task IndexUrl_IgnoresUrlCase()
[Theory]
[InlineData("/redoc/1.0/index.html", "/swagger/1.0/swagger.json")]
[InlineData("/redoc/2.0/index.html", "/swagger/2.0/swagger.json")]
public async Task VersionUrls_ProperlyHandlesDifferentVersions(string redocUrl, string swaggerPath)
public async Task ReDocMiddleware_CanBeConfiguredMultipleTimes(string redocUrl, string swaggerPath)
{
var client = new TestSite(typeof(MultipleVersions.Startup)).BuildClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Readers;
Expand All @@ -10,7 +11,7 @@

namespace Swashbuckle.AspNetCore.IntegrationTests
{
public class SwaggerGenIntegrationTests
public class SwaggerIntegrationTests
{
[Theory]
[InlineData(typeof(Basic.Startup), "/swagger/v1/swagger.json")]
Expand Down Expand Up @@ -113,5 +114,24 @@ public async Task SwaggerEndpoint_InfersServerMetadata_FromRequestHeaders(
Assert.Equal(1, openApiDoc.Servers.Count);
Assert.Equal(expectedServerUrl, openApiDoc.Servers[0].Url);
}

[Theory]
[InlineData("/swagger/v1/swagger.json", "openapi", "3.0.1")]
[InlineData("/swagger/v1/swaggerv2.json", "swagger", "2.0")]
public async Task SwaggerMiddleware_CanBeConfiguredMultipleTimes(
string swaggerUrl,
string expectedVersionProperty,
string expectedVersionValue)
{
var client = new TestSite(typeof(Basic.Startup)).BuildClient();

var response = await client.GetAsync(swaggerUrl);

response.EnsureSuccessStatusCode();
var contentStream = await response.Content.ReadAsStreamAsync();

var json = await JsonSerializer.DeserializeAsync<JsonElement>(contentStream);
Assert.Equal(expectedVersionValue, json.GetProperty(expectedVersionProperty).GetString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public async Task IndexUrl_ReturnsCustomIndexHtml_IfConfigured()
[InlineData("/swagger/index.html", new [] { "Version 1.0", "Version 2.0" })]
[InlineData("/swagger/1.0/index.html", new [] { "Version 1.0" })]
[InlineData("/swagger/2.0/index.html", new [] { "Version 2.0" })]
public async Task VersionUrls_ProperlyHandlesDifferentVersions(string swaggerUiUrl, string[] versions)
public async Task SwaggerUIMiddleware_CanBeConfiguredMultipleTimes(string swaggerUiUrl, string[] versions)
{
var client = new TestSite(typeof(MultipleVersions.Startup)).BuildClient();

Expand Down
6 changes: 6 additions & 0 deletions test/WebSites/Basic/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();

// Expose Swagger/OpenAPI JSON in new (v3) and old (v2) formats
endpoints.MapSwagger("swagger/{documentName}/swagger.json");
endpoints.MapSwagger("swagger/{documentName}/swaggerv2.json", c =>
{
c.SerializeAsV2 = true;
});
});

var supportedCultures = new[]
Expand Down
12 changes: 1 addition & 11 deletions test/WebSites/NetCore21/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Swashbuckle.AspNetCore.Swagger;

namespace NetCore21
{
Expand Down Expand Up @@ -49,16 +48,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)

app.UseMvc();

// Expose v2 and v3 Swagger/OpenAPI formats
app.UseSwagger(new SwaggerOptions
{
RouteTemplate = "/swagger/{documentName}/swagger.json",
SerializeAsV2 = true
});
app.UseSwagger(new SwaggerOptions
{
RouteTemplate = "/swagger/{documentName}/openapi.json",
});
app.UseSwagger();

app.UseSwaggerUI();
}
Expand Down

0 comments on commit 89d75c1

Please sign in to comment.