Skip to content

Commit 33aa353

Browse files
committed
WN:v10:Move OpenAPI inline code to sample
1 parent f719297 commit 33aa353

File tree

9 files changed

+135
-2
lines changed

9 files changed

+135
-2
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ 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):
19+
20+
:::code language="csharp" source="~/release-notes/aspnetcore-10/samples/WebAppOpenAPI10/Program.cs" id="snippet_AddControllers" highlight="3":::
1921

2022
```csharp
2123
builder.Services.AddOpenApi(options =>
@@ -25,7 +27,9 @@ builder.Services.AddOpenApi(options =>
2527
});
2628
```
2729

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.
30+
When generating the OpenAPI document at build time, the OpenAPI version can be selected by setting the `--openapi-version` in the `OpenApiGenerateDocumentsOptions` MSBuild item:
31+
32+
:::code language="csharp" source="~/release-notes/aspnetcore-10/samples/WebAppOpenAPI10/WebAppOpenAPI10.csproj" id="snippet_ConfigBuildTimeOpenApiDocVersion" highlight="7":::
2933

3034
```xml
3135
<!-- Configure build-time OpenAPI generation to produce an OpenAPI 3.0 document. -->
@@ -44,6 +48,8 @@ Breaking changes in this iteration include the following:
4448

4549
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:
4650

51+
:::code language="csharp" source="~/release-notes/aspnetcore-10/samples/WebAppOpenAPI10/TransformerJsonNode.diff":::
52+
4753
```diff
4854
options.AddSchemaTransformer((schema, context, cancellationToken) =>
4955
{
@@ -74,6 +80,8 @@ ASP.NET now supports serving the generated OpenAPI document in YAML format. YAML
7480

7581
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:
7682

83+
:::code language="csharp" source="~/release-notes/aspnetcore-10/samples/WebAppOpenAPI10/Program.cs" id="snippet_ConfigOpenApiYAM" highlight="3":::
84+
7785
```csharp
7886
app.MapOpenApi("/openapi/{documentName}.yaml");
7987
```
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<OpenApiGenerateDocuments>true</OpenApiGenerateDocuments>
8+
<OpenApiGenerateDocumentsOptions>--openapi-version OpenApi3_0</OpenApiGenerateDocumentsOptions>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0-preview.3.25172.1" />
13+
</ItemGroup>
14+
15+
</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)