-
Notifications
You must be signed in to change notification settings - Fork 24.8k
WN: v10 Prev 4: OpenApiSchemas in transformers #35415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0881a64
WN: v10 Prev 4: OpenApiSchemas in transformers
wadepickett c6585eb
Remove include entry, will add later
wadepickett 96c0d5c
Fixed .csproj
wadepickett ce1fedf
Updated openapi-version to 3.1
wadepickett b923bd7
Linked inlcude in WN topic
wadepickett 79763ed
Removed comment
wadepickett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 4 additions & 30 deletions
34
aspnetcore/release-notes/aspnetcore-10/includes/OpenApiSchemasInTransformers.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,16 @@ | ||
| ### Support for generating OpenApiSchemas in transformers | ||
| <!-- https://github.com/dotnet/aspnetcore/pull/61050 --> | ||
|
|
||
| Developers can now generate a schema for a C# type, using the same logic as ASP.NET Core OpenAPI document generation, | ||
| and add it to the OpenAPI document. The schema can then be referenced from elsewhere in the OpenAPI document. | ||
| Developers can now generate a schema for a C# type using the same logic as ASP.NET Core OpenAPI document generation and add it to the OpenAPI document. The schema can then be referenced from elsewhere in the OpenAPI document. | ||
|
|
||
| The context passed to document, operation, and schema transformers now has a `GetOrCreateSchemaAsync` method that can be used to generate a schema for a type. | ||
| The context passed to document, operation, and schema transformers includes a new `GetOrCreateSchemaAsync` method that can be used to generate a schema for a type. | ||
| This method also has an optional `ApiParameterDescription` parameter to specify additional metadata for the generated schema. | ||
|
|
||
| To support adding the schema to the OpenAPI document, a `Document` property has been added to the Operation and Schema transformer contexts. This allows any transformer to add a schema to the OpenAPI document using the document's `AddComponent` method. | ||
|
|
||
| #### Example | ||
|
|
||
| To use this feature in an document, operation, or schema transformer, create the schema using the `GetOrCreateSchemaAsync` method provided in the context | ||
| and add it to the OpenAPI document using the document's `AddComponent` method. | ||
| To use this feature in a document, operation, or schema transformer, create the schema using the `GetOrCreateSchemaAsync` method provided in the context and add it to the OpenAPI document using the document's `AddComponent` method. | ||
|
|
||
| <!-- In the docs, highlight the lines with the call to "GetOrCreateSchemaAsync" and "AddComponent" --> | ||
| ```csharp | ||
| builder.Services.AddOpenApi(options => | ||
| { | ||
| options.AddOperationTransformer(async (operation, context, cancellationToken) => | ||
| { | ||
| // Generate schema for error responses | ||
| var errorSchema = await context.GetOrCreateSchemaAsync(typeof(ProblemDetails), null, cancellationToken); | ||
| context.Document?.AddComponent("Error", errorSchema); | ||
|
|
||
| operation.Responses ??= new OpenApiResponses(); | ||
| // Add a "4XX" response to the operation with the newly created schema | ||
| operation.Responses["4XX"] = new OpenApiResponse | ||
| { | ||
| Description = "Bad Request", | ||
| Content = new Dictionary<string, OpenApiMediaType> | ||
| { | ||
| ["application/problem+json"] = new OpenApiMediaType | ||
| { | ||
| Schema = new OpenApiSchemaReference("Error", context.Document) | ||
| } | ||
| } | ||
| }; | ||
| }); | ||
| }); | ||
| ``` | ||
| :::code language="csharp" source="~/release-notes/aspnetcore-10/samples/WebAppOpenAPI10/Program.cs" id="snippet_Generate_OpenApiSchemas_for_type" highlight="6-7"::: | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.