Skip to content

Commit b9dffd6

Browse files
authored
WN: v10 Prev 3: Move OpenAPI inline code to sample (#35342)
* WN:v10:Move OpenAPI inline code to sample * fixed diff
1 parent 781c7b1 commit b9dffd6

File tree

9 files changed

+136
-39
lines changed

9 files changed

+136
-39
lines changed

aspnetcore/release-notes/aspnetcore-10/includes/openApi.md

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,13 @@ Some of the changes you will see in the generated OpenAPI document include:
1515
and have a `pattern` field limiting the value to digits.
1616
This happens when the <xref:System.Text.Json.JsonSerializerOptions.NumberHandling> property in the <xref:System.Text.Json.JsonSerializerOptions> is set to `AllowReadingFromString`, the default for ASP.NET Core Web apps. To enable C# `int` and `long` to be represented in the OpenAPI document as `type: integer`, set the <xref:System.Text.Json.JsonSerializerOptions.NumberHandling> property to `Strict`.
1717

18-
With this feature, the default OpenAPI version for generated documents is`3.1`. The version can be changed by explicitly setting the [OpenApiVersion](/dotnet/api/microsoft.aspnetcore.openapi.openapioptions.openapiversion) property of the [OpenApiOptions](/dotnet/api/microsoft.aspnetcore.openapi.openapioptions) in the `configureOptions` delegate parameter of [AddOpenApi](/dotnet/api/microsoft.extensions.dependencyinjection.openapiservicecollectionextensions.addopenapi).
18+
With this feature, the default OpenAPI version for generated documents is`3.1`. The version can be changed by explicitly setting the [OpenApiVersion](/dotnet/api/microsoft.aspnetcore.openapi.openapioptions.openapiversion) property of the [OpenApiOptions](/dotnet/api/microsoft.aspnetcore.openapi.openapioptions) in the `configureOptions` delegate parameter of [AddOpenApi](/dotnet/api/microsoft.extensions.dependencyinjection.openapiservicecollectionextensions.addopenapi):
1919

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-
```
20+
:::code language="csharp" source="~/release-notes/aspnetcore-10/samples/WebAppOpenAPI10/Program.cs" id="snippet_DefaultOpenApiVersion" highlight="3":::
2721

28-
When generating the OpenAPI document at build time, the OpenAPI version can be selected by setting the `--openapi-version` in the `OpenApiGenerateDocumentsOptions` MSBuild item.
22+
When generating the OpenAPI document at build time, the OpenAPI version can be selected by setting the `--openapi-version` in the `OpenApiGenerateDocumentsOptions` MSBuild item:
2923

30-
```xml
31-
<!-- Configure build-time OpenAPI generation to produce an OpenAPI 3.0 document. -->
32-
<OpenApiGenerateDocumentsOptions>--openapi-version OpenApi3_0</OpenApiGenerateDocumentsOptions>
33-
```
24+
:::code language="xml" source="~/release-notes/aspnetcore-10/samples/WebAppOpenAPI10/WebAppOpenAPI10.csproj" id="snippet_ConfigBuildTimeOpenApiDocVersion" highlight="7":::
3425

3526
OpenAPI 3.1 support was primarily added in the following [PR](https://github.com/dotnet/aspnetcore/pull/59480).
3627

@@ -42,29 +33,9 @@ Breaking changes in this iteration include the following:
4233
* Entities within the OpenAPI document, like operations and parameters, are typed as interfaces. Concrete implementations exist for the inlined and referenced variants of an entity. For example, an `IOpenApiSchema` can either be an inlined `OpenApiSchema` or an `OpenApiSchemaReference` that points to a schema defined elsewhere in the document.
4334
* The `Nullable` property has been removed from the `OpenApiSchema` type. To determine if a type is nullable, evaluate if the `OpenApiSchema.Type` property sets `JsonSchemaType.Null`.
4435

45-
One of the most significant changes is that the `OpenApiAny` class has been dropped in favor of using `JsonNode` directly. Transformers that use `OpenApiAny` need to be updated to use `JsonNode`. The following diff shows the changes in schema transformer from .NET 9 to .NET 10:
46-
47-
```diff
48-
options.AddSchemaTransformer((schema, context, cancellationToken) =>
49-
{
50-
if (context.JsonTypeInfo.Type == typeof(WeatherForecast))
51-
{
52-
- schema.Example = new OpenApiObject
53-
+ schema.Example = new JsonObject
54-
{
55-
- ["date"] = new OpenApiString(DateTime.Now.AddDays(1).ToString("yyyy-MM-dd")),
56-
+ ["date"] = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"),
57-
- ["temperatureC"] = new OpenApiInteger(0),
58-
+ ["temperatureC"] = 0,
59-
- ["temperatureF"] = new OpenApiInteger(32),
60-
+ ["temperatureF"] = 32,
61-
- ["summary"] = new OpenApiString("Bracing"),
62-
+ ["summary"] = "Bracing",
63-
};
64-
}
65-
return Task.CompletedTask;
66-
});
67-
```
36+
One of the most significant changes is that the `OpenApiAny` class has been dropped in favor of using `JsonNode` directly. Transformers that use `OpenApiAny` need to be updated to use `JsonNode`. The following diff shows the changes in schema transformer from .NET 9 to .NET 10:
37+
38+
:::code language="diff" source="~/release-notes/aspnetcore-10/samples/WebAppOpenAPI10/TransformerJsonNode.cs":::
6839

6940
Note that these changes are necessary even when only configuring the OpenAPI version to 3.0.
7041

@@ -74,9 +45,7 @@ ASP.NET now supports serving the generated OpenAPI document in YAML format. YAML
7445

7546
To configure an app to serve the generated OpenAPI document in YAML format, specify the endpoint in the MapOpenApi call with a ".yaml" or ".yml" suffix, as shown in the following example:
7647

77-
```csharp
78-
app.MapOpenApi("/openapi/{documentName}.yaml");
79-
```
48+
:::code language="csharp" source="~/release-notes/aspnetcore-10/samples/WebAppOpenAPI10/Program.cs" id="snippet_ConfigOpenApiYAML" highlight="3":::
8049

8150
Support for:
8251

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace WebAppOpenAPI10.Controllers
4+
{
5+
[ApiController]
6+
[Route("[controller]")]
7+
public class WeatherForecastController : ControllerBase
8+
{
9+
private static readonly string[] Summaries =
10+
[
11+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
12+
];
13+
14+
[HttpGet(Name = "GetWeatherForecast")]
15+
public IEnumerable<WeatherForecast> Get()
16+
{
17+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
18+
{
19+
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
20+
TemperatureC = Random.Shared.Next(-20, 55),
21+
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
22+
})
23+
.ToArray();
24+
}
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
3+
// Add services to the container.
4+
builder.Services.AddControllers();
5+
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
6+
// <snippet_DefaultOpenApiVersion>
7+
builder.Services.AddOpenApi(options =>
8+
{
9+
options.OpenApiVersion = Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0;
10+
});
11+
// </snippet_DefaultOpenApiVersion>
12+
13+
var app = builder.Build();
14+
15+
// Configure the HTTP request pipeline.
16+
// <snippet_ConfigOpenApiYAML>
17+
if (app.Environment.IsDevelopment())
18+
{
19+
app.MapOpenApi("/openapi/{documentName}.yaml");
20+
}
21+
// </snippet_ConfigOpenApiYAML>
22+
23+
app.UseHttpsRedirection();
24+
25+
app.UseAuthorization();
26+
27+
app.MapControllers();
28+
29+
app.Run();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
options.AddSchemaTransformer((schema, context, cancellationToken) =>
2+
{
3+
if (context.JsonTypeInfo.Type == typeof(WeatherForecast))
4+
{
5+
- schema.Example = new OpenApiObject
6+
+ schema.Example = new JsonObject
7+
{
8+
- ["date"] = new OpenApiString(DateTime.Now.AddDays(1).ToString("yyyy-MM-dd")),
9+
+ ["date"] = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"),
10+
- ["temperatureC"] = new OpenApiInteger(0),
11+
+ ["temperatureC"] = 0,
12+
- ["temperatureF"] = new OpenApiInteger(32),
13+
+ ["temperatureF"] = 32,
14+
- ["summary"] = new OpenApiString("Bracing"),
15+
+ ["summary"] = "Bracing",
16+
};
17+
}
18+
return Task.CompletedTask;
19+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace WebAppOpenAPI10
2+
{
3+
public class WeatherForecast
4+
{
5+
public DateOnly Date { get; set; }
6+
7+
public int TemperatureC { get; set; }
8+
9+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
10+
11+
public string? Summary { get; set; }
12+
}
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<!-- <snippet_ConfigBuildTimeOpenApiDocVersion> -->
4+
<PropertyGroup>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<OpenApiGenerateDocuments>true</OpenApiGenerateDocuments>
9+
<!-- Configure build-time OpenAPI generation to produce an OpenAPI 3.0 document. -->
10+
<OpenApiGenerateDocumentsOptions>--openapi-version OpenApi3_0</OpenApiGenerateDocumentsOptions>
11+
</PropertyGroup>
12+
<!-- </snippet_ConfigBuildTimeOpenApiDocVersion> -->
13+
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0-preview.3.25172.1" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@WebAppOpenAPI10_HostAddress = http://localhost:5077
2+
3+
GET {{WebAppOpenAPI10_HostAddress}}/weatherforecast/
4+
Accept: application/json
5+
6+
###
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)