Skip to content

Commit f263ef4

Browse files
MikeKistler OpenAPI etc migration to WN 10 (#34722)
1 parent 5fe0dd8 commit f263ef4

3 files changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
### OpenAPI 3.1 support
2+
3+
https://github.com/dotnet/aspnetcore/pull/59480
4+
https://github.com/dotnet/aspnetcore/pull/60002
5+
6+
ASP.NET Core has added support for generating [OpenAPI version 3.1] documents in .NET 10.
7+
Despite the minor version bump, OpenAPI 3.1 is a significant update to the OpenAPI specification,
8+
in particular with full support for [JSON Schema draft 2020-12].
9+
10+
[OpenAPI version 3.1]: https://spec.openapis.org/oas/v3.1.1.html
11+
[JSON Schema draft 2020-12]: https://json-schema.org/specification-links#2020-12
12+
13+
Some of the changes you will see in the generated OpenAPI document include:
14+
- Nullable types will no longer have the `nullable: true` property in the schema, and instead will have a `type` keyword whose value is an array that includes `null` as one of the types.
15+
16+
With this feature, the default OpenAPI version for generated documents will be 3.1, but you can easily change this
17+
by explicitly setting the `OpenApiVersion` property of the `OpenApiOptions` in the `configureOptions` delegate
18+
parameter of `AddOpenApi`.
19+
20+
```csharp
21+
builder.Services.AddOpenApi(options =>
22+
{
23+
// Specify the OpenAPI version to use.
24+
options.OpenApiVersion = Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0;
25+
});
26+
```
27+
28+
If you are generating the OpenAPI document at build time, you can select the OpenAPI version by setting the `--openapi-version` in the `OpenApiGenerateDocumentsOptions` MSBuild item.
29+
30+
```xml
31+
<!-- Configure build-time OpenAPI generation to produce an OpenAPI 3.0 document. -->
32+
<OpenApiGenerateDocumentsOptions>--openapi-version OpenApi3_0</OpenApiGenerateDocumentsOptions>
33+
```
34+
35+
### Breaking changes
36+
37+
Support for OpenAPI 3.1 requires an update to the underlying OpenAPI.NET library to a new major version, 2.0.
38+
This new version has some breaking changes from the previous version, and this may impact your applications
39+
if you have any document, operation, or schema transformers.
40+
Perhaps the most significant change is that the `OpenApiAny` class has been dropped in favor of using `JsonNode` directly.
41+
If your transformers use `OpenApiAny`, you will need to update them to use `JsonNode` instead.
42+
For example, a schema transformer to add an example in .NET 9 might look like this:
43+
44+
```csharp
45+
options.AddSchemaTransformer((schema, context, cancellationToken) =>
46+
{
47+
if (context.JsonTypeInfo.Type == typeof(WeatherForecast))
48+
{
49+
schema.Example = new OpenApiObject
50+
{
51+
["date"] = new OpenApiString(DateTime.Now.AddDays(1).ToString("yyyy-MM-dd")),
52+
["temperatureC"] = new OpenApiInteger(0),
53+
["temperatureF"] = new OpenApiInteger(32),
54+
["summary"] = new OpenApiString("Bracing"),
55+
};
56+
}
57+
return Task.CompletedTask;
58+
});
59+
```
60+
61+
In .NET 10 the transformer to do the same task will look like this:
62+
63+
```csharp
64+
options.AddSchemaTransformer((schema, context, cancellationToken) =>
65+
{
66+
if (context.JsonTypeInfo.Type == typeof(WeatherForecast))
67+
{
68+
schema.Example = new JsonObject
69+
{
70+
["date"] = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"),
71+
["temperatureC"] = 0,
72+
["temperatureF"] = 32,
73+
["summary"] = "Bracing",
74+
};
75+
}
76+
return Task.CompletedTask;
77+
});
78+
```
79+
80+
Note that these changes will be necessary even if you congfigure the OpenAPI version to 3.0.
81+
82+
### OpenAPI in Yaml
83+
84+
https://github.com/dotnet/aspnetcore/pull/58616
85+
86+
ASP.NET now supports serving the generated OpenAPI document in YAML format.
87+
YAML can be more concise than JSON, eliminating curly braces and quotation marks when these can be inferred.
88+
YAML also supports multi-line strings, which can be useful for long descriptions.
89+
90+
To configure your application to serve the generated OpenAPI document in YAML format,
91+
specify the endpoint in the MapOpenApi call with a ".yaml" or ".yml" suffix, as shown in this example:
92+
93+
```csharp
94+
app.MapOpenApi("/openapi/{documentName}.yaml");
95+
```
96+
97+
Support for YAML is currently only available for the the OpenAPI served from the OpenAPI endpoint.
98+
Support for generating OpenAPI documents in YAML format at build time will be added in a future preview.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
### Response description on ProducesResponseType
2+
3+
https://github.com/dotnet/aspnetcore/pull/58193
4+
5+
The ProducesAttribute, ProducesResponseTypeAttribute, and ProducesDefaultResponseType attributes now accept an optional string parameter, `Description`, that will set the description of the response. Here's an example:
6+
7+
```csharp
8+
[HttpGet(Name = "GetWeatherForecast")]
9+
[ProducesResponseType<IEnumerable<WeatherForecast>>(StatusCodes.Status200OK, Description = "The weather forecast for the next 5 days.")]
10+
public IEnumerable<WeatherForecast> Get()
11+
{
12+
```
13+
14+
And the generated OpenAPI will be
15+
16+
```json
17+
"responses": {
18+
"200": {
19+
"description": "The weather forecast for the next 5 days.",
20+
"content": {
21+
```
22+
23+
Community contribution! 🙏
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## Better support for testing apps with top-level statements
2+
3+
https://github.com/dotnet/aspnetcore/pull/58199
4+
https://github.com/dotnet/aspnetcore/pull/58482
5+
6+
.NET 10 now has better support for testing apps that use top-level statements.
7+
Previously developers had to manually add `public partial class Program` to the
8+
Program.cs file so that the test project could reference the Program class.
9+
This is because the top-level statement feature in C# 9 generated a Program class
10+
that was declared as internal.
11+
12+
In .NET 10, a source generator is used to generate the `public partial class Program`
13+
declaration if the programmer did not declare it explicitly. In addition, an analyzer
14+
was added to detect when `public partial class Program` is declared explicitly and
15+
advise the developer to remove it.
16+
17+
![Image](https://github.com/user-attachments/assets/a37f0c81-a58a-453f-8da5-fa49356ca180)

0 commit comments

Comments
 (0)