Skip to content

Commit 79e60de

Browse files
Merge pull request #35239 from dotnet/main
Merge to Live
2 parents 9c36209 + 4b56bfb commit 79e60de

File tree

9 files changed

+447
-36
lines changed

9 files changed

+447
-36
lines changed

Diff for: aspnetcore/blazor/forms/index.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ The <xref:Microsoft.AspNetCore.Components.Forms.EditForm> provides the following
243243

244244
## Clear a form or field
245245

246-
Reset a form by clearing its model back its default state, which can be performed inside or outside of an <xref:Microsoft.AspNetCore.Components.Forms.EditForm>'s markup:
246+
Reset a form by clearing its model back to its default state, which can be performed inside or outside of an <xref:Microsoft.AspNetCore.Components.Forms.EditForm>'s markup:
247247

248248
```razor
249249
<button @onclick="ClearForm">Clear form</button>
@@ -368,7 +368,7 @@ To disable enhanced form handling:
368368
* For an <xref:Microsoft.AspNetCore.Components.Forms.EditForm>, remove the <xref:Microsoft.AspNetCore.Components.Forms.EditForm.Enhance%2A> parameter from the form element (or set it to `false`: `Enhance="false"`).
369369
* For an HTML `<form>`, remove the `data-enhance` attribute from form element (or set it to `false`: `data-enhance="false"`).
370370

371-
Blazor's enhanced navigation and form handing may undo dynamic changes to the DOM if the updated content isn't part of the server rendering. To preserve the content of an element, use the `data-permanent` attribute.
371+
Blazor's enhanced navigation and form handling may undo dynamic changes to the DOM if the updated content isn't part of the server rendering. To preserve the content of an element, use the `data-permanent` attribute.
372372

373373
In the following example, the content of the `<div>` element is updated dynamically by a script when the page loads:
374374

Diff for: aspnetcore/fundamentals/openapi/aspnetcore-openapi.md

+40-26
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,43 @@ description: Learn how to generate and customize OpenAPI documents in an ASP.NET
55
ms.author: safia
66
monikerRange: '>= aspnetcore-6.0'
77
ms.custom: mvc
8-
ms.date: 2/23/2025
8+
ms.date: 3/18/2025
99
uid: fundamentals/openapi/aspnetcore-openapi
1010
---
1111
# Generate OpenAPI documents
1212

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

1515
The [`Microsoft.AspNetCore.OpenApi`](https://www.nuget.org/packages/Microsoft.AspNetCore.OpenApi) package provides built-in support for OpenAPI document generation in ASP.NET Core. The package provides the following features:
1616

17+
* Support for generating [OpenAPI version 3.1] documents.
18+
* Support for [JSON Schema draft 2020-12].
1719
* Support for generating OpenAPI documents at run time and accessing them via an endpoint on the app.
1820
* Support for "transformer" APIs that allow modifying the generated document.
1921
* Support for generating multiple OpenAPI documents from a single app.
2022
* Takes advantage of JSON schema support provided by [`System.Text.Json`](/dotnet/api/system.text.json).
2123
* Is compatible with native AoT.
2224

25+
[OpenAPI version 3.1]: https://spec.openapis.org/oas/v3.1.1.html
26+
[JSON Schema draft 2020-12]: https://json-schema.org/specification-links#2020-12
27+
28+
The default OpenAPI version for generated documents is`3.1`. The version can be changed by explicitly setting the [OpenApiVersion](/dotnet/api/microsoft.aspnetcore.openapi.openapioptions.openapiversion) property of the [OpenApiOptions](/dotnet/api/microsoft.aspnetcore.openapi.openapioptions) in the `configureOptions` delegate parameter of [AddOpenApi](/dotnet/api/microsoft.extensions.dependencyinjection.openapiservicecollectionextensions.addopenapi):
29+
30+
```csharp
31+
builder.Services.AddOpenApi(options =>
32+
{
33+
// Specify the OpenAPI version to use.
34+
options.OpenApiVersion = Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0;
35+
});
36+
```
37+
38+
When generating the OpenAPI document at build time, the OpenAPI version can be selected by setting the `--openapi-version` in the `OpenApiGenerateDocumentsOptions` MSBuild item.
39+
40+
```xml
41+
<!-- Configure build-time OpenAPI generation to produce an OpenAPI 3.1 document. -->
42+
<OpenApiGenerateDocumentsOptions>--openapi-version OpenApi3_1</OpenApiGenerateDocumentsOptions>
43+
```
44+
2345
## Package installation
2446

2547
Install the `Microsoft.AspNetCore.OpenApi` package:
@@ -56,6 +78,16 @@ Launch the app and navigate to `https://localhost:<port>/openapi/v1.json` to vie
5678

5779
The following sections demonstrate how to customize OpenAPI document generation.
5880

81+
### Generate OpenAPI document in YAML format
82+
83+
The OpenAPI document can be generated in either JSON or YAML format. By default, the OpenAPI document is generated in JSON format. To generate the OpenAPI document in YAML format, specify the endpoint in the MapOpenApi call with a ".yaml" or ".yml" suffix, as shown in the following example:
84+
85+
```csharp
86+
app.MapOpenApi("/openapi/{documentName}.yaml");
87+
```
88+
89+
Generating penAPI documents in YAML format at build time is currently not supported, but planned in a future preview.
90+
5991
### Customize the OpenAPI document name
6092

6193
Each OpenAPI document in an app has a unique name. The default document name that is registered is `v1`.
@@ -81,12 +113,12 @@ GET http://localhost:5000/openapi/internal.json
81113

82114
### Customize the OpenAPI version of a generated document
83115

84-
By default, OpenAPI document generation creates a document that is compliant with [v3.0 of the OpenAPI specification](https://spec.openapis.org/oas/v3.0.0). The following code demonstrates how to modify the default version of the OpenAPI document:
116+
By default, OpenAPI document generation creates a document that is compliant with [OpenAPI version 3.1](https://spec.openapis.org/oas/v3.1.1.html). The following code demonstrates how to modify the default version of the OpenAPI document:
85117

86118
```csharp
87119
builder.Services.AddOpenApi(options =>
88120
{
89-
options.OpenApiVersion = OpenApiSpecVersion.OpenApi2_0;
121+
options.OpenApiVersion = OpenApiSpecVersion.OpenApi3_0;
90122
});
91123
```
92124

@@ -259,32 +291,14 @@ Create a new ASP.NET Core Web API (Native AOT) project.
259291
dotnet new webapiaot
260292
```
261293

262-
:::moniker-end
263-
264-
:::moniker range="= aspnetcore-9.0"
265-
266-
Add the Microsoft.AspNetCore.OpenAPI package.
267-
268-
```console
269-
dotnet add package Microsoft.AspNetCore.OpenApi
270-
```
271-
272-
Update `Program.cs` to enable generating OpenAPI documents.
273-
274-
```diff
275-
+ builder.Services.AddOpenApi();
276-
277-
var app = builder.Build();
278-
279-
+ app.MapOpenApi();
280-
```
281-
282-
:::moniker-end
283-
284294
Publish the app.
285295

286296
```console
287297
dotnet publish
288298
```
289299

300+
:::moniker-end
301+
290302
[!INCLUDE[](~/fundamentals/openapi/includes/aspnetcore-openapi6-8.md)]
303+
304+
[!INCLUDE[](~/fundamentals/openapi/includes/aspnetcore-openapi9.md)]

Diff for: aspnetcore/fundamentals/openapi/include-metadata.md

+98
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,8 @@ of the property in the schema.
493493

494494
### type and format
495495

496+
:::moniker range="< aspnetcore-10.0"
497+
496498
The JSON Schema library maps standard C# types to OpenAPI `type` and `format` as follows:
497499

498500
| C# Type | OpenAPI `type` | OpenAPI `format` |
@@ -520,6 +522,79 @@ Note that object and dynamic types have _no_ type defined in the OpenAPI because
520522

521523
The `type` and `format` can also be set with a [Schema Transformer](xref:fundamentals/openapi/customize-openapi#use-schema-transformers). For example, you may want the `format` of decimal types to be `decimal` instead of `double`.
522524

525+
:::moniker-end
526+
527+
:::moniker range=">= aspnetcore-10.0"
528+
529+
#### Numeric types
530+
531+
The JSON Schema library maps standard C# numeric types to OpenAPI `type` and `format` based on the
532+
<xref:System.Text.Json.JsonSerializerOptions.NumberHandling> property of the <xref:System.Text.Json.JsonSerializerOptions>
533+
used in the app. In ASP.NET Core Web API apps, the default value of this property is `JsonNumberHandling.AllowReadingFromString`.
534+
535+
When the <xref:System.Text.Json.JsonSerializerOptions.NumberHandling> property is set to `JsonNumberHandling.AllowReadingFromString`, the numeric types are mapped as follows:
536+
537+
| C# Type | OpenAPI `type` | OpenAPI `format` | Other assertions |
538+
| -------------- | ---------------- | ---------------- | ------------------------------ |
539+
| int | [integer,string] | int32 | pattern `<digits>` |
540+
| long | [integer,string] | int64 | pattern `<digits>` |
541+
| short | [integer,string] | int16 | pattern `<digits>` |
542+
| byte | [integer,string] | uint8 | pattern `<digits>` |
543+
| float | [number,string] | float | pattern `<digits with decimal >` |
544+
| double | [number,string] | double | pattern `<digits with decimal >` |
545+
| decimal | [number,string] | double | pattern `<digits with decimal >` |
546+
547+
<!--
548+
| int | [integer,string] | int32 | pattern `<digits>` | pattern: `"^-?(?:0|[1-9]\\d*)$"`
549+
| long | [integer,string] | int64 | pattern `<digits>` |
550+
| short | [integer,string] | int16 | pattern `<digits>` |
551+
| byte | [integer,string] | uint8 | pattern `<digits>` |
552+
| float | [number,string] | float | pattern `<digits with decimal >` | pattern: `"^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$"`
553+
| double | [number,string] | double | pattern `<digits with decimal >` |
554+
| decimal | [number,string] | double | pattern `<digits with decimal >` | pattern: `"^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?$"`
555+
-->
556+
557+
If the app is configured to produce OpenAPI 3.0 or OpenAPI v2 documents, where the `type` field cannot have an array value, the `type` field is dropped.
558+
559+
When the <xref:System.Text.Json.JsonSerializerOptions.NumberHandling> property is set to `JsonNumberHandling.Strict`, the numeric types are mapped as follows:
560+
561+
| C# Type | OpenAPI `type` | OpenAPI `format` |
562+
| -------------- | -------------- | ---------------- |
563+
| int | integer | int32 |
564+
| long | integer | int64 |
565+
| short | integer | int16 |
566+
| byte | integer | uint8 |
567+
| float | number | float |
568+
| double | number | double |
569+
| decimal | number | double |
570+
571+
#### String types
572+
573+
The following table shows how C# types map to `string` type properties in the generated OpenAPI document:
574+
575+
| C# Type | OpenAPI `type` | OpenAPI `format` | Other assertions |
576+
| -------------- | -------------- | ---------------- | ------------------------------ |
577+
| string | string | | |
578+
| char | string | char | minLength: 1, maxLength: 1 |
579+
| byte[] | string | byte | |
580+
| DateTimeOffset | string | date-time | |
581+
| DateOnly | string | date | |
582+
| TimeOnly | string | time | |
583+
| Uri | string | uri | |
584+
| Guid | string | uuid | |
585+
586+
#### Other types
587+
588+
Other C# types are represented in the generated OpenAPI document as shown in the following table:
589+
590+
| C# Type | OpenAPI `type` | OpenAPI `format` |
591+
| -------------- | -------------- | ---------------- |
592+
| bool | boolean | |
593+
| object | _omitted_ | |
594+
| dynamic | _omitted_ | |
595+
596+
:::moniker-end
597+
523598
### Use attributes to add metadata
524599

525600
ASP.NET uses metadata from attributes on class or record properties to set metadata on the corresponding properties of the generated schema.
@@ -600,8 +675,31 @@ An enum type without a [`[JsonConverter]`](xref:System.Text.Json.Serialization.
600675

601676
#### nullable
602677

678+
:::moniker range="< aspnetcore-10.0"
679+
603680
Properties defined as a nullable value or reference type have `nullable: true` in the generated schema. This is consistent with the default behavior of the <xref:System.Text.Json> deserializer, which accepts `null` as a valid value for a nullable property.
604681

682+
:::moniker-end
683+
:::moniker range=">= aspnetcore-10.0"
684+
685+
Properties defined as a nullable value or reference type appear in the generated schema with a `type` keyword whose value is an array that includes `null` as one of the types. This is consistent with the default behavior of the <xref:System.Text.Json> deserializer, which accepts `null` as a valid value for a nullable property.
686+
687+
For example, a C# property defined as `string?` is represented in the generated schema as:
688+
689+
```json
690+
"nullableString": {
691+
"description": "A property defined as string?",
692+
"type": [
693+
"null",
694+
"string"
695+
]
696+
},
697+
```
698+
699+
If the app is configured to produce OpenAPI v3.0 or OpenAPI v2 documents, nullable value or reference types have `nullable: true` in the generated schema because these OpenAPI versions do not allow the `type` field to be an array.
700+
701+
:::moniker-end
702+
605703
#### additionalProperties
606704

607705
Schemas are generated without an `additionalProperties` assertion by default, which implies the default of `true`. This is consistent with the default behavior of the <xref:System.Text.Json> deserializer, which silently ignores additional properties in a JSON object.

0 commit comments

Comments
 (0)