Skip to content

Commit 07a5f4b

Browse files
Update info on rendering of C# data types to OpenAPI in .NET 10 (#35222)
* Update info on OpenAPI rendering of data types * Update doc on nullable properties Co-authored-by: Rick Anderson <[email protected]>
1 parent 24123e8 commit 07a5f4b

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

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)