From 38dcd6de853c323d68490d70f2c16520d625e5fd Mon Sep 17 00:00:00 2001 From: Mike Kistler Date: Mon, 16 Sep 2024 17:12:08 -0500 Subject: [PATCH] Add content on data types and schema transformers (#33603) * Add content on data types and schema transformers * Fix refs/links * Update aspnetcore-openapi.md * Update aspnetcore/fundamentals/openapi/aspnetcore-openapi.md * Apply suggestions from code review * Apply suggestions from PR review Co-authored-by: Safia Abdalla * Remove unused imports. * Fixes to enum details --------- Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Co-authored-by: Safia Abdalla --- .../9.0-samples/WebMinOpenApi/Program.cs | 42 ++++++- .../WebMinOpenApi/WebMinOpenApi.csproj | 6 +- .../WebMinOpenApi/WebMinOpenApi.json | 9 +- .../9.0-samples/WebMinOpenApi/nuget.config | 1 - .../openapi/aspnetcore-openapi.md | 119 +++++++++++++++++- 5 files changed, 162 insertions(+), 15 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs b/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs index 6a6934e6fb93..3de9cb2e1f80 100644 --- a/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs @@ -1,7 +1,7 @@ -//#define DEFAULT -//#define DOCUMENTtransformerInOut -//#define DOCUMENTtransformer1 -//#define DOCUMENTtransformer2 +//#define DEFAULT +//#define DOCUMENTtransformerInOut +//#define DOCUMENTtransformer1 +//#define DOCUMENTtransformer2 #define DOCUMENTtransformerUse999 //#define DEFAULT //#define FIRST @@ -233,6 +233,38 @@ public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransf // #endif +#if SCHEMAtransformer1 +// +using Microsoft.AspNetCore.OpenApi; + +var builder = WebApplication.CreateBuilder(); + +builder.Services.AddOpenApi(options => { + // Schema transformer to set the format of decimal to 'decimal' + options.AddSchemaTransformer((schema, context, cancellationToken) => + { + if (context.JsonTypeInfo.Type == typeof(decimal)) + { + schema.Format = "decimal"; + } + return Task.CompletedTask; + }); +}); + +var app = builder.Build(); + +app.MapOpenApi(); + +app.MapGet("/", () => new Body { Amount = 1.1m }); + +app.Run(); + +public class Body { + public decimal Amount { get; set; } +} +// +#endif + #if SWAGGERUI // using Microsoft.AspNetCore.Authentication; @@ -371,7 +403,7 @@ public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransf builder.Services.AddOpenApi(options => { - options.AddDocumentTransformer((document, context, cancellationToken) + options.AddDocumentTransformer((document, context, cancellationToken) => Task.CompletedTask); options.AddDocumentTransformer(new MyDocumentTransformer()); options.AddDocumentTransformer(); diff --git a/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/WebMinOpenApi.csproj b/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/WebMinOpenApi.csproj index 0d63b7ae5d3b..2a67e5a014e3 100644 --- a/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/WebMinOpenApi.csproj +++ b/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/WebMinOpenApi.csproj @@ -12,9 +12,9 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/WebMinOpenApi.json b/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/WebMinOpenApi.json index 7aa05da9c5fb..714c9ee61bd8 100644 --- a/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/WebMinOpenApi.json +++ b/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/WebMinOpenApi.json @@ -2,6 +2,7 @@ "openapi": "3.0.1", "info": { "title": "GetDocument.Insider | v1", + "description": "Transformed OpenAPI document", "version": "1.0.0" }, "paths": { @@ -10,22 +11,24 @@ "tags": [ "GetDocument.Insider" ], + "summary": "Transformed OpenAPI operation", "responses": { "200": { "description": "OK", "content": { "text/plain": { "schema": { - "type": "string" + "type": "string", + "description": "Transformed OpenAPI schema" } } } } - }, - "x-aspnetcore-id": "14ccb7f6-1846-48e4-aeaf-c760aadda7c3" + } } } }, + "components": { }, "tags": [ { "name": "GetDocument.Insider" diff --git a/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/nuget.config b/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/nuget.config index 134bfb7890f0..6ce97590acdd 100644 --- a/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/nuget.config +++ b/aspnetcore/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/nuget.config @@ -4,6 +4,5 @@ - diff --git a/aspnetcore/fundamentals/openapi/aspnetcore-openapi.md b/aspnetcore/fundamentals/openapi/aspnetcore-openapi.md index 14cd79836a80..33bacf2f0669 100644 --- a/aspnetcore/fundamentals/openapi/aspnetcore-openapi.md +++ b/aspnetcore/fundamentals/openapi/aspnetcore-openapi.md @@ -344,6 +344,101 @@ app.MapGet("/attributes", () => "Hello world!"); ``` +## Including OpenAPI metadata for data types + +C# classes or records used in request or response bodies are represented as schemas +in the generated OpenAPI document. +By default, only public properties are represented in the schema, but there are + to also create schema properties for fields. + +When the is set to camel-case (this is the default +in ASP.NET web applications), property names in a schema are the camel-case form +of the class or record property name. +The can be used on an individual property to specify the name +of the property in the schema. + +## type and format + +The JSON Schema library maps standard C# types to OpenAPI `type` and `format` as follows: + +| C# Type | OpenAPI `type` | OpenAPI `format` | +| -------------- | -------------- | ---------------- | +| int | integer | int32 | +| long | integer | int64 | +| short | integer | int16 | +| byte | integer | uint8 | +| float | number | float | +| double | number | double | +| decimal | number | double | +| bool | boolean | | +| string | string | | +| char | string | char | +| byte[] | string | byte | +| DateTimeOffset | string | date-time | +| DateOnly | string | date | +| TimeOnly | string | time | +| Uri | string | uri | +| Guid | string | uuid | +| object | _omitted_ | | +| dynamic | _omitted_ | | + +Note that object and dynamic types have _no_ type defined in the OpenAPI because these can contain data of any type, including primitive types like int or string. + +The `type` and `format` can also be set with a [Schema Transformer](#use-schema-transformers). For example, you may want the `format` of decimal types to be `decimal` instead of `double`. + +## Using attributes to add metadata + +ASP.NET uses metadata from attributes on class or record properties to set metadata on the corresponding properties of the generated schema. + +The following table summarizes attributes from the `System.ComponentModel` namespace that provide metadata for the generated schema: + +| Attribute | Description | +| ---------------------------- | ----------- | +| | Sets the `description` of a property in the schema. | +| | Marks a property as `required` in the schema. | +| | Sets the `default` value of a property in the schema. | +| | Sets the `minimum` and `maximum` value of an integer or number. | +| | Sets the `minLength` of a string. | +| | Sets the `maxLength` of a string. | +| | Sets the `pattern` of a string. | + +Note that in controller-based apps, these attributes add filters to the operation to validate that any incoming data satisfies the constraints. In Minimal APIs, these attributes set the metadata in the generated schema but validation must be performed explicitly via an endpoint filter, in the route handler's logic, or via a third-party package. + +## Other sources of metadata for generated schemas + +### required + +Properties can also be marked as `required` with the [required](/dotnet/csharp/language-reference/proposals/csharp-11.0/required-members#required-modifier) modifier. + +### enum + +Enum types in C# are integer-based, but can be represented as strings in JSON with a and a . When an enum type is represented as a string in JSON, the generated schema will have an `enum` property with the string values of the enum. +An enum type without a will be defined as `type: integer` in the generated schema. + +**Note:** The does not set the `enum` values of a property. + +### nullable + +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 deserializer, which accepts `null` as a valid value for a nullable property. + +### additionalProperties + +Schemas are generated without an `additionalProperties` assertion by default, which implies the default of `true`. This is consistent with the default behavior of the deserializer, which silently ignores additional properties in a JSON object. + +If the additional properties of a schema should only have values of a specific type, define the property or class as a `Dictionary`. The key type for the dictionary must be `string`. This generates a schema with `additionalProperties` specifying the schema for "type" as the required value types. + +### Metadata for polymorphic types + +Use the and attributes on a parent class to to specify the discriminator field and subtypes for a polymorphic type. + +The adds the discriminator field to the schema for each subclass, with an enum specifying the specific discriminator value for the subclass. This attribute also modifies the constructor of each derived class to set the discriminator value. + +An abstract class with a attribute has a `discriminator` field in the schema, but a concrete class with a attribute doesn't have a `discriminator` field. OpenAPI requires that the discriminator property be a required property in the schema, but since the discriminator property isn't defined in the concrete base class, the schema cannot include a `discriminator` field. + +## Adding metadata with a schema transformer + +A schema transformer can be used to override any default metadata or add additional metadata, such as `example` values, to the generated schema. See [Use schema transformers](#use-schema-transformers) for more information. + ## Options to Customize OpenAPI document generation The following sections demonstrate how to customize OpenAPI document generation. @@ -398,7 +493,7 @@ Because the OpenAPI document is served via a route handler endpoint, any customi #### Limit OpenAPI document access to authorized users -The OpenAPI endpoint doesn't enable any authorization checks by default. However, it's possible to limit access to the OpenAPI document. For example, in the following code, access to the OpenAPI document is limited to those with the `tester` role: +The OpenAPI endpoint doesn't enable any authorization checks by default. However, authorization checks can be applied to the OpenAPI document. In the following code, access to the OpenAPI document is limited to those with the `tester` role: [!code-csharp[](~/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs?name=snippet_mapopenapiwithauth)] @@ -422,10 +517,11 @@ Transformers provide an API for modifying the OpenAPI document with user-defined * Modifying descriptions for parameters or operations. * Adding top-level information to the OpenAPI document. -Transformers fall into two categories: +Transformers fall into three categories: * Document transformers have access to the entire OpenAPI document. These can be used to make global modifications to the document. * Operation transformers apply to each individual operation. Each individual operation is a combination of path and HTTP method. These can be used to modify parameters or responses on endpoints. +* Schema transformers apply to each schema in the document. These can be used to modify the schema of request or response bodies, or any nested schemas. Transformers can be registered onto the document by calling the [`AddDocumentTransformer`](https://source.dot.net/#Microsoft.AspNetCore.OpenApi/Services/OpenApiOptions.cs,90bbc6506b8eff7a) method on the [`OpenApiOptions`](https://source.dot.net/#Microsoft.AspNetCore.OpenApi/Services/OpenApiOptions.cs,c0a8b420f4ce6918) object. The following snippet shows different ways to register transformers onto the document: @@ -455,7 +551,7 @@ Document transformers have access to a context object that includes: * The list of `ApiDescriptionGroups` associated with that document. * The `IServiceProvider` used in document generation. -Document transformers also can mutate the OpenAPI document that is generated. The following example demonstrates a document transformer that adds some information about the API to the OpenAPI document. +Document transformers can also mutate the OpenAPI document that is generated. The following example demonstrates a document transformer that adds some information about the API to the OpenAPI document. [!code-csharp[](~/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs?name=snippet_documenttransformer1)] @@ -487,6 +583,23 @@ For example, the following operation transformer adds `500` as a response status [!code-csharp[](~/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs?name=snippet_operationtransformer1)] +### Use schema transformers + +Schemas are the data models that are used in request and response bodies in an OpenAPI document. Schema transformers are useful when a modification: + +* Should be made to each schema in the document, or +* Conditionally applied to certain schemas. + +Schema transformers have access to a context object which contains: + +* The name of the document the schema belongs to. +* The JSON type information associated with the target schema. +* The `IServiceProvider` used in document generation. + +For example, the following schema transformer sets the `format` of decimal types to `decimal` instead of `double`: + +[!code-csharp[](~/fundamentals/minimal-apis/9.0-samples/WebMinOpenApi/Program.cs?name=snippet_schematransformer1)] + ## Additional resources *