|
| 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. |
0 commit comments