Skip to content

Commit c165b8e

Browse files
committed
Add the Customize schema reuse section
1 parent 2ddf75e commit c165b8e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

aspnetcore/fundamentals/openapi/customize-openapi.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,43 @@ For example, the following schema transformer sets the `format` of decimal types
103103

104104
[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_schematransformer1)]
105105

106+
## Customize schema reuse
107+
108+
After all transformers have been applied, the framework makes a pass over the document to transfer certain schemas
109+
to the `components.schemas` section, replacing them with `$ref` references to the transferred schema.
110+
This reduces the size of the document and makes it easier to read.
111+
112+
The details of this processing are a bit complicated, and might change in future versions of .NET, but in general:
113+
114+
* Schemas for class/record/struct types are replaced with a `$ref` to a schema in `components.schemas`
115+
if they appear more than once in the document.
116+
* Schemas for primitive types and standard collections are left inline.
117+
* Schemas for enum types are always replaced with a `$ref` to a schema in to components.schemas.
118+
119+
Typically the name of the schema in `components.schemas` is the name of the class/record/struct type,
120+
but in some circumstances a different name must be used.
121+
122+
ASP.NET Core lets you customize which schemas are replaced with a `$ref` to a schema in `components.schemas`
123+
using the <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions.CreateSchemaReferenceId> property of <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions>.
124+
This property is a delegate that takes a <xref:System.Text.Json.Serialization.Metadata.JsonTypeInfo> object and returns the name of the schema
125+
in `components.schemas` that should be used for that type.
126+
The framework provides a default implementation of this delegate, <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions.CreateDefaultSchemaReferenceId%2A>
127+
that uses the name of the type, but you can replace it with your own implementation.
128+
129+
As a simple example of this customization, you might choose to always inline enum schemas.
130+
This is done by setting <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions.CreateSchemaReferenceId> to a delegate
131+
that returns null for enum types, and otherwise returns value from the default implementation.
132+
The following code shows how to do this:
133+
134+
```csharp
135+
builder.Services.AddOpenApi(options =>
136+
{
137+
// Always inline enum schemas
138+
options.CreateSchemaReferenceId = (type) =>
139+
type.Type.IsEnum ? null : OpenApiOptions.CreateDefaultSchemaReferenceId(type);
140+
});
141+
```
142+
106143
## Additional resources
107144

108145
* <xref:fundamentals/openapi/using-openapi-documents>

0 commit comments

Comments
 (0)