Skip to content

Commit bb32864

Browse files
authored
WN: v10 Prev 4: OpenApiSchemas in transformers (#35415)
* WN: v10 Prev 4: OpenApiSchemas in transformers * Remove include entry, will add later * Fixed .csproj * Updated openapi-version to 3.1 * Linked inlcude in WN topic * Removed comment
1 parent bcd928a commit bb32864

File tree

4 files changed

+64
-35
lines changed

4 files changed

+64
-35
lines changed

aspnetcore/release-notes/aspnetcore-10.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ This section describes new features for OpenAPI.
5353

5454
[!INCLUDE[](~/release-notes/aspnetcore-10/includes/webapiaotTemplateAddedOpenAPI.md)]
5555

56+
[!INCLUDE[](~/release-notes/aspnetcore-10/includes/OpenApiSchemasInTransformers.md)]
57+
5658
## Authentication and authorization
5759

5860
This section describes new features for authentication and authorization.
Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,15 @@
11
### Support for generating OpenApiSchemas in transformers
22
<!-- https://github.com/dotnet/aspnetcore/pull/61050 -->
33

4-
Developers can now generate a schema for a C# type, using the same logic as ASP.NET Core OpenAPI document generation,
5-
and add it to the OpenAPI document. The schema can then be referenced from elsewhere in the OpenAPI document.
4+
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.
65

7-
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.
6+
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.
87
This method also has an optional `ApiParameterDescription` parameter to specify additional metadata for the generated schema.
98

109
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.
1110

1211
#### Example
1312

14-
To use this feature in an document, operation, or schema transformer, create the schema using the `GetOrCreateSchemaAsync` method provided in the context
15-
and add it to the OpenAPI document using the document's `AddComponent` method.
13+
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.
1614

17-
<!-- In the docs, highlight the lines with the call to "GetOrCreateSchemaAsync" and "AddComponent" -->
18-
```csharp
19-
builder.Services.AddOpenApi(options =>
20-
{
21-
options.AddOperationTransformer(async (operation, context, cancellationToken) =>
22-
{
23-
// Generate schema for error responses
24-
var errorSchema = await context.GetOrCreateSchemaAsync(typeof(ProblemDetails), null, cancellationToken);
25-
context.Document?.AddComponent("Error", errorSchema);
26-
27-
operation.Responses ??= new OpenApiResponses();
28-
// Add a "4XX" response to the operation with the newly created schema
29-
operation.Responses["4XX"] = new OpenApiResponse
30-
{
31-
Description = "Bad Request",
32-
Content = new Dictionary<string, OpenApiMediaType>
33-
{
34-
["application/problem+json"] = new OpenApiMediaType
35-
{
36-
Schema = new OpenApiSchemaReference("Error", context.Document)
37-
}
38-
}
39-
};
40-
});
41-
});
42-
```
15+
:::code language="csharp" source="~/release-notes/aspnetcore-10/samples/WebAppOpenAPI10/Program.cs" id="snippet_Generate_OpenApiSchemas_for_type" highlight="6-7":::

aspnetcore/release-notes/aspnetcore-10/samples/WebAppOpenAPI10/Program.cs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#define SCHEMA_IN_TRANSFORMER_EXAMPLE //YAML_EXAMPLE //SCHEMA_IN_TRANSFORMER_EXAMPLE
2+
#if YAML_EXAMPLE
3+
14
var builder = WebApplication.CreateBuilder(args);
25

36
// Add services to the container.
@@ -6,7 +9,7 @@
69
// <snippet_DefaultOpenApiVersion>
710
builder.Services.AddOpenApi(options =>
811
{
9-
options.OpenApiVersion = Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0;
12+
options.OpenApiVersion = Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_1;
1013
});
1114
// </snippet_DefaultOpenApiVersion>
1215

@@ -27,3 +30,54 @@
2730
app.MapControllers();
2831

2932
app.Run();
33+
34+
#elif SCHEMA_IN_TRANSFORMER_EXAMPLE
35+
36+
var builder = WebApplication.CreateBuilder(args);
37+
38+
// Add services to the container.
39+
builder.Services.AddControllers();
40+
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
41+
// <snippet_Generate_OpenApiSchemas_for_type>
42+
builder.Services.AddOpenApi(options =>
43+
{
44+
options.AddOperationTransformer(async (operation, context, cancellationToken) =>
45+
{
46+
// Generate schema for error responses
47+
var errorSchema = await context.GetOrCreateSchemaAsync(typeof(ProblemDetails), null, cancellationToken);
48+
context.Document?.AddComponent("Error", errorSchema);
49+
50+
operation.Responses ??= new OpenApiResponses();
51+
// Add a "4XX" response to the operation with the newly created schema
52+
operation.Responses["4XX"] = new OpenApiResponse
53+
{
54+
Description = "Bad Request",
55+
Content = new Dictionary<string, OpenApiMediaType>
56+
{
57+
["application/problem+json"] = new OpenApiMediaType
58+
{
59+
Schema = new OpenApiSchemaReference("Error", context.Document)
60+
}
61+
}
62+
};
63+
});
64+
});
65+
// </snippet_Generate_OpenApiSchemas_for_type>
66+
67+
var app = builder.Build();
68+
69+
// Configure the HTTP request pipeline.
70+
if (app.Environment.IsDevelopment())
71+
{
72+
app.MapOpenApi("/openapi/{documentName}.yaml");
73+
}
74+
75+
app.UseHttpsRedirection();
76+
77+
app.UseAuthorization();
78+
79+
app.MapControllers();
80+
81+
app.Run();
82+
83+
#endif

aspnetcore/release-notes/aspnetcore-10/samples/WebAppOpenAPI10/WebAppOpenAPI10.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
<Nullable>enable</Nullable>
77
<ImplicitUsings>enable</ImplicitUsings>
88
<OpenApiGenerateDocuments>true</OpenApiGenerateDocuments>
9-
<!-- Configure build-time OpenAPI generation to produce an OpenAPI 3.0 document. -->
10-
<OpenApiGenerateDocumentsOptions>--openapi-version OpenApi3_0</OpenApiGenerateDocumentsOptions>
9+
<!-- Configure build-time OpenAPI generation to produce an OpenAPI 3.1 document. -->
10+
<OpenApiGenerateDocumentsOptions>--openapi-version OpenApi3_1</OpenApiGenerateDocumentsOptions>
1111
</PropertyGroup>
1212
<!-- </snippet_ConfigBuildTimeOpenApiDocVersion> -->
1313

1414
<ItemGroup>
15-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0-preview.3.25172.1" />
15+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0-preview.4.24272.1" />
1616
</ItemGroup>
1717

1818
</Project>

0 commit comments

Comments
 (0)