Skip to content

Commit ed52947

Browse files
committed
Update info on OpenAPI rendering of data types
1 parent 5694813 commit ed52947

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

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

+65
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,69 @@ 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>` | <!-- pattern: "^-?(?:0|[1-9]\\d*)$" -->
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 >` | <!-- pattern: "^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$" -->
544+
| double | [number,string] | double | pattern `<digits with decimal >` |
545+
| decimal | [number,string] | double | pattern `<digits with decimal >` | <!-- pattern: "^-?(?:0|[1-9]\\d*)(?:\\.\\d+)?$" -->
546+
547+
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.
548+
549+
When the <xref:System.Text.Json.JsonSerializerOptions.NumberHandling> property is set to `JsonNumberHandling.Strict`, the numeric types are mapped as follows:
550+
551+
| C# Type | OpenAPI `type` | OpenAPI `format` |
552+
| -------------- | -------------- | ---------------- |
553+
| int | integer | int32 |
554+
| long | integer | int64 |
555+
| short | integer | int16 |
556+
| byte | integer | uint8 |
557+
| float | number | float |
558+
| double | number | double |
559+
| decimal | number | double |
560+
561+
#### String types
562+
563+
The following C# types map to `string` type properties in the generated OpenAPI document as follows:
564+
565+
| C# Type | OpenAPI `type` | OpenAPI `format` | Other assertions |
566+
| -------------- | -------------- | ---------------- | ------------------------------ |
567+
| string | string | | |
568+
| char | string | char | minLength: 1, maxLength: 1 |
569+
| byte[] | string | byte | |
570+
| DateTimeOffset | string | date-time | |
571+
| DateOnly | string | date | |
572+
| TimeOnly | string | time | |
573+
| Uri | string | uri | |
574+
| Guid | string | uuid | |
575+
576+
#### Other types
577+
578+
Here's how other C# types are represented in the generated OpenAPI document:
579+
580+
| C# Type | OpenAPI `type` | OpenAPI `format` |
581+
| -------------- | -------------- | ---------------- |
582+
| bool | boolean | |
583+
| object | _omitted_ | |
584+
| dynamic | _omitted_ | |
585+
586+
:::moniker-end
587+
523588
### Use attributes to add metadata
524589

525590
ASP.NET uses metadata from attributes on class or record properties to set metadata on the corresponding properties of the generated schema.

0 commit comments

Comments
 (0)