Skip to content

Commit aa38ac3

Browse files
Update to Microsoft.OpenApi v2.0.0-preview5 (dotnet#60002)
* Update to Microsoft.OpenApi v2.0.0-preview5 * Address feedback * Update src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiDocumentService/OpenApiDocumentServiceTests.Operations.cs * Update src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiDocumentService/OpenApiDocumentServiceTests.Operations.cs Co-authored-by: Brennan <[email protected]> --------- Co-authored-by: Brennan <[email protected]>
1 parent e685d09 commit aa38ac3

15 files changed

+127
-141
lines changed

eng/Versions.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@
335335
<XunitExtensibilityExecutionVersion>$(XunitVersion)</XunitExtensibilityExecutionVersion>
336336
<XUnitRunnerVisualStudioVersion>2.8.2</XUnitRunnerVisualStudioVersion>
337337
<MicrosoftDataSqlClientVersion>5.2.2</MicrosoftDataSqlClientVersion>
338-
<MicrosoftOpenApiVersion>2.0.0-preview4</MicrosoftOpenApiVersion>
339-
<MicrosoftOpenApiReadersVersion>2.0.0-preview4</MicrosoftOpenApiReadersVersion>
338+
<MicrosoftOpenApiVersion>2.0.0-preview5</MicrosoftOpenApiVersion>
339+
<MicrosoftOpenApiReadersVersion>2.0.0-preview5</MicrosoftOpenApiReadersVersion>
340340
<!-- dotnet tool versions (see also auto-updated DotnetEfVersion property). -->
341341
<DotnetDumpVersion>6.0.322601</DotnetDumpVersion>
342342
<DotnetServeVersion>1.10.93</DotnetServeVersion>

eng/testing/linker/project.csproj.template

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.AspNetCore.Http.Generated</InterceptorsPreviewNamespaces>
1717
<!-- Ensure individual warnings are shown when publishing -->
1818
<TrimmerSingleWarn>false</TrimmerSingleWarn>
19-
<!-- But ignore the single warn files marked below to suppress their known warnings. -->
20-
<NoWarn>$(NoWarn);IL2104;IL3053</NoWarn>
2119
{AdditionalProperties}
2220
</PropertyGroup>
2321

@@ -29,19 +27,4 @@
2927
{AdditionalProjectReferences}
3028
</ItemGroup>
3129

32-
<!-- Single warn the following assemblies, which have known warnings, so the warnings can be suppressed for now.
33-
Remove this (and the above NoWarn IL2104) once https://github.com/microsoft/OpenAPI.NET/issues/1875 is addressed. -->
34-
<Target Name="ConfigureTrimming"
35-
BeforeTargets="PrepareForILLink">
36-
<ItemGroup>
37-
<IlcArg Include="--singlewarnassembly:Microsoft.OpenApi" />
38-
</ItemGroup>
39-
<ItemGroup>
40-
<ManagedAssemblyToLink Condition="'%(Filename)' == 'Microsoft.OpenApi'">
41-
<IsTrimmable>true</IsTrimmable>
42-
<TrimmerSingleWarn>true</TrimmerSingleWarn>
43-
</ManagedAssemblyToLink>
44-
</ItemGroup>
45-
</Target>
46-
4730
</Project>

src/OpenApi/src/Extensions/OpenApiDocumentExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static OpenApiSchema AddOpenApiSchemaByReference(this OpenApiDocument doc
2323
document.Components.Schemas[schemaId] = schema;
2424
document.Workspace ??= new();
2525
var location = document.BaseUri + "/components/schemas/" + schemaId;
26-
document.Workspace.RegisterComponent(location, schema);
26+
document.Workspace.RegisterComponentForDocument(document, schema, location);
2727
return new OpenApiSchemaReference(schemaId, document);
2828
}
2929
}

src/OpenApi/src/Extensions/OpenApiEndpointRouteBuilderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ public static IEndpointConventionBuilder MapOpenApi(this IEndpointRouteBuilder e
5959
{
6060
if (UseYaml(pattern))
6161
{
62-
document.Serialize(new OpenApiYamlWriter(writer), documentOptions.OpenApiVersion);
62+
await document.SerializeAsync(new OpenApiYamlWriter(writer), documentOptions.OpenApiVersion);
6363
context.Response.ContentType = "text/plain+yaml;charset=utf-8";
6464
}
6565
else
6666
{
67-
document.Serialize(new OpenApiJsonWriter(writer), documentOptions.OpenApiVersion);
67+
await document.SerializeAsync(new OpenApiJsonWriter(writer), documentOptions.OpenApiVersion);
6868
context.Response.ContentType = "application/json;charset=utf-8";
6969
}
7070
await context.Response.BodyWriter.WriteAsync(output.ToArray(), context.RequestAborted);

src/OpenApi/src/Services/OpenApiDocumentProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public async Task GenerateAsync(string documentName, TextWriter writer, OpenApiS
5757
using var scopedService = serviceProvider.CreateScope();
5858
var document = await targetDocumentService.GetOpenApiDocumentAsync(scopedService.ServiceProvider);
5959
var jsonWriter = new OpenApiJsonWriter(writer);
60-
document.Serialize(jsonWriter, openApiSpecVersion);
60+
await document.SerializeAsync(jsonWriter, openApiSpecVersion);
6161
}
6262

6363
/// <summary>

src/OpenApi/src/Services/OpenApiDocumentService.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using Microsoft.Extensions.Options;
2727
using Microsoft.Net.Http.Headers;
2828
using Microsoft.OpenApi.Models;
29+
using Microsoft.OpenApi.Models.References;
2930

3031
namespace Microsoft.AspNetCore.OpenApi;
3132

@@ -59,12 +60,12 @@ public async Task<OpenApiDocument> GetOpenApiDocumentAsync(IServiceProvider scop
5960
// Schema and operation transformers are scoped per-request and can be
6061
// pre-allocated to hold the same number of transformers as the associated
6162
// options object.
62-
IOpenApiSchemaTransformer[] schemaTransformers = _options.SchemaTransformers.Count > 0
63+
var schemaTransformers = _options.SchemaTransformers.Count > 0
6364
? new IOpenApiSchemaTransformer[_options.SchemaTransformers.Count]
64-
: Array.Empty<IOpenApiSchemaTransformer>();
65-
IOpenApiOperationTransformer[] operationTransformers = _options.OperationTransformers.Count > 0 ?
65+
: [];
66+
var operationTransformers = _options.OperationTransformers.Count > 0 ?
6667
new IOpenApiOperationTransformer[_options.OperationTransformers.Count]
67-
: Array.Empty<IOpenApiOperationTransformer>();
68+
: [];
6869
InitializeTransformers(scopedServiceProvider, schemaTransformers, operationTransformers);
6970
var document = new OpenApiDocument
7071
{
@@ -277,14 +278,7 @@ private async Task<OpenApiOperation> GetOperationAsync(
277278
IOpenApiSchemaTransformer[] schemaTransformers,
278279
CancellationToken cancellationToken)
279280
{
280-
var tags = GetTags(description);
281-
if (tags != null)
282-
{
283-
foreach (var tag in tags)
284-
{
285-
document.Tags?.Add(tag);
286-
}
287-
}
281+
var tags = GetTags(description, document);
288282
var operation = new OpenApiOperation
289283
{
290284
OperationId = GetOperationId(description),
@@ -308,16 +302,27 @@ private async Task<OpenApiOperation> GetOperationAsync(
308302
=> description.ActionDescriptor.AttributeRouteInfo?.Name ??
309303
description.ActionDescriptor.EndpointMetadata.OfType<IEndpointNameMetadata>().LastOrDefault()?.EndpointName;
310304

311-
private static List<OpenApiTag>? GetTags(ApiDescription description)
305+
private static List<OpenApiTagReference> GetTags(ApiDescription description, OpenApiDocument document)
312306
{
313307
var actionDescriptor = description.ActionDescriptor;
314308
if (actionDescriptor.EndpointMetadata?.OfType<ITagsMetadata>().LastOrDefault() is { } tagsMetadata)
315309
{
316-
return tagsMetadata.Tags.Select(tag => new OpenApiTag { Name = tag }).ToList();
310+
List<OpenApiTagReference> tags = [];
311+
foreach (var tag in tagsMetadata.Tags)
312+
{
313+
document.Tags ??= [];
314+
document.Tags.Add(new OpenApiTag { Name = tag });
315+
tags.Add(new OpenApiTagReference(tag, document));
316+
317+
}
318+
return tags;
317319
}
318320
// If no tags are specified, use the controller name as the tag. This effectively
319321
// allows us to group endpoints by the "resource" concept (e.g. users, todos, etc.)
320-
return [new OpenApiTag { Name = description.ActionDescriptor.RouteValues["controller"] }];
322+
var controllerName = description.ActionDescriptor.RouteValues["controller"];
323+
document.Tags ??= [];
324+
document.Tags.Add(new OpenApiTag { Name = controllerName });
325+
return [new OpenApiTagReference(controllerName, document)];
321326
}
322327

323328
private async Task<OpenApiResponses> GetResponsesAsync(

src/OpenApi/src/Services/OpenApiGenerator.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Microsoft.Extensions.Internal;
2121
using Microsoft.Extensions.Primitives;
2222
using Microsoft.OpenApi.Models;
23+
using Microsoft.OpenApi.Models.References;
2324

2425
namespace Microsoft.AspNetCore.OpenApi;
2526

@@ -322,19 +323,22 @@ private static void GenerateDefaultResponses(Dictionary<int, (Type?, MediaTypeCo
322323
return null;
323324
}
324325

325-
private List<OpenApiTag> GetOperationTags(MethodInfo methodInfo, EndpointMetadataCollection metadata)
326+
private List<OpenApiTagReference> GetOperationTags(MethodInfo methodInfo, EndpointMetadataCollection metadata)
326327
{
327328
var metadataList = metadata.GetOrderedMetadata<ITagsMetadata>();
329+
var document = new OpenApiDocument();
328330

329331
if (metadataList.Count > 0)
330332
{
331-
var tags = new List<OpenApiTag>();
333+
var tags = new List<OpenApiTagReference>();
332334

333335
foreach (var metadataItem in metadataList)
334336
{
335337
foreach (var tag in metadataItem.Tags)
336338
{
337-
tags.Add(new OpenApiTag() { Name = tag });
339+
document.Tags ??= [];
340+
document.Tags.Add(new OpenApiTag { Name = tag });
341+
tags.Add(new OpenApiTagReference(tag, document));
338342
}
339343
}
340344

@@ -354,7 +358,9 @@ private List<OpenApiTag> GetOperationTags(MethodInfo methodInfo, EndpointMetadat
354358
controllerName = _environment?.ApplicationName ?? string.Empty;
355359
}
356360

357-
return new List<OpenApiTag>() { new OpenApiTag() { Name = controllerName } };
361+
document.Tags ??= [];
362+
document.Tags.Add(new OpenApiTag { Name = controllerName });
363+
return [new(controllerName, document)];
358364
}
359365

360366
private List<OpenApiParameter> GetOpenApiParameters(MethodInfo methodInfo, RoutePattern pattern, bool disableInferredBody)

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/snapshots/OpenApiDocumentIntegrationTests.VerifyOpenApiDocument_documentName=controllers.verified.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
"/getbyidandname/{id}/{name}": {
99
"get": {
1010
"tags": [
11-
{
12-
"name": "Test"
13-
}
11+
"Test"
1412
],
1513
"parameters": [
1614
{
@@ -59,9 +57,7 @@
5957
"/forms": {
6058
"post": {
6159
"tags": [
62-
{
63-
"name": "Test"
64-
}
60+
"Test"
6561
],
6662
"requestBody": {
6763
"content": {

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/snapshots/OpenApiDocumentIntegrationTests.VerifyOpenApiDocument_documentName=forms.verified.txt

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
"/forms/form-file": {
99
"post": {
1010
"tags": [
11-
{
12-
"name": "Sample"
13-
}
11+
"Sample"
1412
],
1513
"requestBody": {
1614
"content": {
@@ -40,9 +38,7 @@
4038
"/forms/form-files": {
4139
"post": {
4240
"tags": [
43-
{
44-
"name": "Sample"
45-
}
41+
"Sample"
4642
],
4743
"requestBody": {
4844
"content": {
@@ -72,9 +68,7 @@
7268
"/forms/form-file-multiple": {
7369
"post": {
7470
"tags": [
75-
{
76-
"name": "Sample"
77-
}
71+
"Sample"
7872
],
7973
"requestBody": {
8074
"content": {
@@ -118,9 +112,7 @@
118112
"/forms/form-todo": {
119113
"post": {
120114
"tags": [
121-
{
122-
"name": "Sample"
123-
}
115+
"Sample"
124116
],
125117
"requestBody": {
126118
"content": {
@@ -147,9 +139,7 @@
147139
"/forms/forms-pocos-and-files": {
148140
"post": {
149141
"tags": [
150-
{
151-
"name": "Sample"
152-
}
142+
"Sample"
153143
],
154144
"requestBody": {
155145
"content": {

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/snapshots/OpenApiDocumentIntegrationTests.VerifyOpenApiDocument_documentName=responses.verified.txt

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
"/responses/200-add-xml": {
99
"get": {
1010
"tags": [
11-
{
12-
"name": "Sample"
13-
}
11+
"Sample"
1412
],
1513
"responses": {
1614
"200": {
@@ -34,9 +32,7 @@
3432
"/responses/200-only-xml": {
3533
"get": {
3634
"tags": [
37-
{
38-
"name": "Sample"
39-
}
35+
"Sample"
4036
],
4137
"responses": {
4238
"200": {
@@ -55,9 +51,7 @@
5551
"/responses/triangle": {
5652
"get": {
5753
"tags": [
58-
{
59-
"name": "Sample"
60-
}
54+
"Sample"
6155
],
6256
"responses": {
6357
"200": {
@@ -76,9 +70,7 @@
7670
"/responses/shape": {
7771
"get": {
7872
"tags": [
79-
{
80-
"name": "Sample"
81-
}
73+
"Sample"
8274
],
8375
"responses": {
8476
"200": {

0 commit comments

Comments
 (0)