Skip to content

Commit e934eb3

Browse files
authored
Prep Only: Using-openapi-documents.md include change for .NET 10 (#35887)
1 parent 4d464b8 commit e934eb3

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
:::moniker range="= aspnetcore-9.0"
2+
3+
## Use Swagger UI for local ad-hoc testing
4+
5+
By default, the `Microsoft.AspNetCore.OpenApi` package doesn't ship with built-in support for visualizing or interacting with the OpenAPI document. Popular tools for visualizing or interacting with the OpenAPI document include [Swagger UI](https://swagger.io/tools/swaggerhub/) and [ReDoc](https://github.com/Redocly/redoc). Swagger UI and ReDoc can be integrated in an app in several ways. Editors such as Visual Studio and VS Code offer extensions and built-in experiences for testing against an OpenAPI document.
6+
7+
The `Swashbuckle.AspNetCore.SwaggerUi` package provides a bundle of Swagger UI's web assets for use in apps. This package can be used to render a UI for the generated document. To configure this:
8+
9+
* Install the `Swashbuckle.AspNetCore.SwaggerUi` package.
10+
* Enable the swagger-ui middleware with a reference to the [OpenAPI route registered earlier](xref:fundamentals/openapi/aspnetcore-openapi#customize-the-openapi-endpoint-route).
11+
12+
[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_swaggerui)]
13+
14+
As a security best practice on limiting information disclosure, ***OpenAPI user interfaces (Swagger UI, ReDoc, Scalar) should only be enabled in development environments.*** For example, see [Swagger OAuth 2.0 configuration](https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/).
15+
16+
Launch the app and navigate to `https://localhost:<port>/swagger` to view the Swagger UI.
17+
18+
To automatically launch the app at the Swagger UI URL using the `https` profile of `Properties/launchSettings.json`:
19+
20+
* Confirm that `launchBrowser` is enabled (`true`).
21+
* Set the `launchUrl` to `swagger`.
22+
23+
```json
24+
"launchBrowser": true,
25+
"launchUrl": "swagger",
26+
```
27+
28+
## Use Scalar for interactive API documentation
29+
30+
[Scalar](https://scalar.com/) is an open-source interactive document UI for OpenAPI. Scalar can integrate with the OpenAPI endpoint provided by ASP.NET Core. To configure Scalar, install the `Scalar.AspNetCore` package.
31+
32+
[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_openapiwithscalar)]
33+
34+
Launch the app and navigate to `https://localhost:<port>/scalar/v1` to view the Scalar UI.
35+
36+
To automatically launch the app at the Scalar UI URL using the `https` profile of `Properties/launchSettings.json`:
37+
38+
* Confirm that `launchBrowser` is enabled (`true`).
39+
* Set the `launchUrl` to `scalar/v1`.
40+
41+
```json
42+
"launchBrowser": true,
43+
"launchUrl": "scalar/v1",
44+
```
45+
46+
## Lint generated OpenAPI documents with Spectral
47+
48+
[Spectral](https://stoplight.io/open-source/spectral) is an open-source OpenAPI document linter. Spectral can be incorporated into an app build to verify the quality of generated OpenAPI documents. Install Spectral according to the [package installation directions](https://github.com/stoplightio/spectral#-installation).
49+
50+
To take advantage of Spectral, install the `Microsoft.Extensions.ApiDescription.Server` package to enable build-time OpenAPI document generation.
51+
52+
Enable document generation at build time by setting the following properties in the app's `.csproj` file":
53+
54+
```xml
55+
<PropertyGroup>
56+
<OpenApiDocumentsDirectory>$(MSBuildProjectDirectory)</OpenApiDocumentsDirectory>
57+
<OpenApiGenerateDocuments>true</OpenApiGenerateDocuments>
58+
</PropertyGroup>
59+
```
60+
61+
Run `dotnet build` to generate the document.
62+
63+
```dotnetcli
64+
dotnet build
65+
```
66+
67+
Create a `.spectral.yml` file with the following contents.
68+
69+
```text
70+
extends: ["spectral:oas"]
71+
```
72+
73+
Run `spectral lint` on the generated file.
74+
75+
```dotnetcli
76+
spectral lint WebMinOpenApi.json
77+
...
78+
79+
The output shows any issues with the OpenAPI document. For example:
80+
81+
```output
82+
1:1 warning oas3-api-servers OpenAPI "servers" must be present and non-empty array.
83+
3:10 warning info-contact Info object must have "contact" object. info
84+
3:10 warning info-description Info "description" must be present and non-empty string. info
85+
9:13 warning operation-description Operation "description" must be present and non-empty string. paths./.get
86+
9:13 warning operation-operationId Operation must have "operationId". paths./.get
87+
88+
✖ 5 problems (0 errors, 5 warnings, 0 infos, 0 hints)
89+
```
90+
:::moniker-end

aspnetcore/fundamentals/openapi/using-openapi-documents.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ uid: fundamentals/openapi/using-openapi-documents
1010
---
1111
# Use openAPI documents
1212

13-
:::moniker range=">= aspnetcore-9.0"
13+
:::moniker range=">= aspnetcore-10.0"
1414

1515
## Use Swagger UI for local ad-hoc testing
1616

@@ -102,3 +102,4 @@ The output shows any issues with the OpenAPI document. For example:
102102
:::moniker-end
103103

104104
[!INCLUDE[](~/fundamentals/openapi/includes/using-openapi-documents-6-8.md)]
105+
[!INCLUDE[](~/fundamentals/openapi/includes/using-openapi-documents-9.md)]

0 commit comments

Comments
 (0)