Skip to content

Commit 3c32fa0

Browse files
authored
OpenAPI: Update tools for generating documentation website (#1661)
1 parent 6a60d4a commit 3c32fa0

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

docs/usage/openapi-documentation.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,45 @@
11
# OpenAPI documentation
22

3-
After [enabling OpenAPI](~/usage/openapi.md), you can expose a documentation website with SwaggerUI or Redoc.
3+
After [enabling OpenAPI](~/usage/openapi.md), you can expose a documentation website with SwaggerUI, Redoc and/or Scalar.
44

5-
### SwaggerUI
5+
## SwaggerUI
66

7-
Swashbuckle ships with [SwaggerUI](https://swagger.io/tools/swagger-ui/), which enables to visualize and interact with the JSON:API endpoints through a web page.
8-
This can be enabled by installing the `Swashbuckle.AspNetCore.SwaggerUI` NuGet package and adding the following to your `Program.cs` file:
7+
[SwaggerUI](https://swagger.io/tools/swagger-ui/) enables to visualize and interact with the JSON:API endpoints through a web page.
8+
While it conveniently provides the ability to execute requests, it doesn't show properties of derived types when component schema inheritance is used.
9+
10+
SwaggerUI can be enabled by installing the `Swashbuckle.AspNetCore.SwaggerUI` NuGet package and adding the following to your `Program.cs` file:
911

1012
```c#
1113
app.UseSwaggerUI();
1214
```
1315

14-
By default, SwaggerUI will be available at `http://localhost:<port>/swagger`.
16+
Then run your app and open `/swagger` in your browser.
1517

16-
### Redoc
18+
## Redoc
1719

1820
[Redoc](https://github.com/Redocly/redoc) is another popular tool that generates a documentation website from an OpenAPI document.
1921
It lists the endpoints and their schemas, but doesn't provide the ability to execute requests.
22+
However, this tool most accurately reflects properties when component schema inheritance is used; choosing a different "type" from the
23+
dropdown box dynamically adapts the list of schema properties.
24+
2025
The `Swashbuckle.AspNetCore.ReDoc` NuGet package provides integration with Swashbuckle.
26+
After installing the package, add the following to your `Program.cs` file:
27+
28+
```c#
29+
app.UseReDoc();
30+
```
31+
32+
Next, run your app and navigate to `/api-docs` to view the documentation.
33+
34+
## Scalar
35+
36+
[Scalar](https://scalar.com/) is a modern documentation website generator, which includes the ability to execute requests.
37+
It shows component schemas in a low-level way (not collapsing `allOf` nodes), but does a poor job in handling component schema inheritance.
38+
39+
After installing the `Scalar.AspNetCore` NuGet package, add the following to your `Program.cs` to make it use the OpenAPI document produced by Swashbuckle:
40+
41+
```c#
42+
app.MapScalarApiReference(options => options.OpenApiRoutePattern = "/swagger/{documentName}/swagger.json");
43+
```
44+
45+
Then run your app and navigate to `/scalar/v1` to view the documentation.

package-versions.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<MicrosoftApiServerVersion>9.0.*</MicrosoftApiServerVersion>
2323
<NSwagApiClientVersion>14.2.*</NSwagApiClientVersion>
2424
<NewtonsoftJsonVersion>13.0.*</NewtonsoftJsonVersion>
25+
<ScalarAspNetCoreVersion>1.2.*</ScalarAspNetCoreVersion>
2526
<SourceLinkVersion>8.0.*</SourceLinkVersion>
2627
<SwashbuckleVersion>7.*-*</SwashbuckleVersion>
2728
<SystemTextJsonVersion>9.0.*</SystemTextJsonVersion>

src/Examples/JsonApiDotNetCoreExample/JsonApiDotNetCoreExample.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="$(EntityFrameworkCoreVersion)" />
1919
<PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="$(MicrosoftApiServerVersion)" PrivateAssets="all" />
2020
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="$(EntityFrameworkCoreVersion)" />
21+
<PackageReference Include="Scalar.AspNetCore" Version="$(ScalarAspNetCoreVersion)" />
2122
<PackageReference Include="Swashbuckle.AspNetCore" Version="$(SwashbuckleVersion)" />
23+
<PackageReference Include="Swashbuckle.AspNetCore.ReDoc" Version="$(SwashbuckleVersion)" />
2224
</ItemGroup>
2325
</Project>

src/Examples/JsonApiDotNetCoreExample/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.EntityFrameworkCore;
1010
using Microsoft.EntityFrameworkCore.Diagnostics;
1111
using Microsoft.Extensions.DependencyInjection.Extensions;
12+
using Scalar.AspNetCore;
1213

1314
[assembly: ExcludeFromCodeCoverage]
1415

@@ -103,6 +104,8 @@ static void ConfigurePipeline(WebApplication app)
103104

104105
app.UseSwagger();
105106
app.UseSwaggerUI();
107+
app.UseReDoc();
108+
app.MapScalarApiReference(options => options.OpenApiRoutePattern = "/swagger/{documentName}/swagger.json");
106109

107110
app.MapControllers();
108111
}

0 commit comments

Comments
 (0)