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 d88a5df
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 24 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Once you have an API that can describe itself in Swagger, you've opened the trea
|Swashbuckle Version|ASP.NET Core|Swagger / OpenAPI Spec.|swagger-ui|ReDoc UI|
|----------|----------|----------|----------|----------|
|[master](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/tree/master/README.md)|>= 2.0.0|2.0, 3.0|3.42.0|2.0.0-rc.40|
|[6.0.6](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/tree/v6.0.6)|>= 2.0.0|2.0, 3.0|3.42.0|2.0.0-rc.40|
|[6.0.7](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/tree/v6.0.7)|>= 2.0.0|2.0, 3.0|3.42.0|2.0.0-rc.40|
|[5.6.3](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/tree/v5.6.3)|>= 2.0.0|2.0, 3.0|3.32.5|2.0.0-rc.40|
|[4.0.0](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/tree/v4.0.0)|>= 2.0.0, < 3.0.0|2.0|3.19.5|1.22.2|
|[3.0.0](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/tree/v3.0.0)|>= 1.0.4, < 3.0.0|2.0|3.17.1|1.20.0|
Expand All @@ -33,8 +33,8 @@ Once you have an API that can describe itself in Swagger, you've opened the trea
1. Install the standard Nuget package into your ASP.NET Core application.

```
Package Manager : Install-Package Swashbuckle.AspNetCore -Version 6.0.6
CLI : dotnet add package --version 6.0.6 Swashbuckle.AspNetCore
Package Manager : Install-Package Swashbuckle.AspNetCore -Version 6.0.7
CLI : dotnet add package --version 6.0.7 Swashbuckle.AspNetCore
```
2. In the `ConfigureServices` method of `Startup.cs`, register the Swagger generator, defining one or more Swagger documents.
Expand Down Expand Up @@ -98,8 +98,8 @@ If you're using **System.Text.Json (STJ)**, then the setup described above will
If you're using **Newtonsoft**, then you'll need to install a separate package and explicitly opt-in to ensure that *Newtonsoft* settings/attributes are automatically honored by the Swagger generator:
```
Package Manager : Install-Package Swashbuckle.AspNetCore.Newtonsoft -Version 6.0.6
CLI : dotnet add package --version 6.0.6 Swashbuckle.AspNetCore.Newtonsoft
Package Manager : Install-Package Swashbuckle.AspNetCore.Newtonsoft -Version 6.0.7
CLI : dotnet add package --version 6.0.7 Swashbuckle.AspNetCore.Newtonsoft
```
```csharp
Expand Down Expand Up @@ -1528,7 +1528,7 @@ It's packaged as a [.NET Core Tool](https://docs.microsoft.com/en-us/dotnet/core
1. Install as a [global tool](https://docs.microsoft.com/en-us/dotnet/core/tools/global-tools#install-a-global-tool)
```
dotnet tool install -g --version 6.0.6 Swashbuckle.AspNetCore.Cli
dotnet tool install -g --version 6.0.7 Swashbuckle.AspNetCore.Cli
```

2. Verify that the tool was installed correctly
Expand Down Expand Up @@ -1559,7 +1559,7 @@ It's packaged as a [.NET Core Tool](https://docs.microsoft.com/en-us/dotnet/core
2. Install as a [local tool](https://docs.microsoft.com/en-us/dotnet/core/tools/global-tools#install-a-local-tool)
```
dotnet tool install --version 6.0.6 Swashbuckle.AspNetCore.Cli
dotnet tool install --version 6.0.7 Swashbuckle.AspNetCore.Cli
```

3. Verify that the tool was installed correctly
Expand Down
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 d88a5df

Please sign in to comment.