Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Microsoft.OpenApi v2.0.0-preview5 #60002

Merged
merged 4 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@
<XunitExtensibilityExecutionVersion>$(XunitVersion)</XunitExtensibilityExecutionVersion>
<XUnitRunnerVisualStudioVersion>2.8.2</XUnitRunnerVisualStudioVersion>
<MicrosoftDataSqlClientVersion>5.2.2</MicrosoftDataSqlClientVersion>
<MicrosoftOpenApiVersion>2.0.0-preview4</MicrosoftOpenApiVersion>
<MicrosoftOpenApiReadersVersion>2.0.0-preview4</MicrosoftOpenApiReadersVersion>
<MicrosoftOpenApiVersion>2.0.0-preview5</MicrosoftOpenApiVersion>
<MicrosoftOpenApiReadersVersion>2.0.0-preview5</MicrosoftOpenApiReadersVersion>
<!-- dotnet tool versions (see also auto-updated DotnetEfVersion property). -->
<DotnetDumpVersion>6.0.322601</DotnetDumpVersion>
<DotnetServeVersion>1.10.93</DotnetServeVersion>
Expand Down
17 changes: 0 additions & 17 deletions eng/testing/linker/project.csproj.template
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.AspNetCore.Http.Generated</InterceptorsPreviewNamespaces>
<!-- Ensure individual warnings are shown when publishing -->
<TrimmerSingleWarn>false</TrimmerSingleWarn>
<!-- But ignore the single warn files marked below to suppress their known warnings. -->
<NoWarn>$(NoWarn);IL2104;IL3053</NoWarn>
{AdditionalProperties}
</PropertyGroup>

Expand All @@ -29,19 +27,4 @@
{AdditionalProjectReferences}
</ItemGroup>

<!-- Single warn the following assemblies, which have known warnings, so the warnings can be suppressed for now.
Remove this (and the above NoWarn IL2104) once https://github.com/microsoft/OpenAPI.NET/issues/1875 is addressed. -->
<Target Name="ConfigureTrimming"
BeforeTargets="PrepareForILLink">
<ItemGroup>
<IlcArg Include="--singlewarnassembly:Microsoft.OpenApi" />
</ItemGroup>
<ItemGroup>
<ManagedAssemblyToLink Condition="'%(Filename)' == 'Microsoft.OpenApi'">
<IsTrimmable>true</IsTrimmable>
<TrimmerSingleWarn>true</TrimmerSingleWarn>
</ManagedAssemblyToLink>
</ItemGroup>
</Target>

</Project>
2 changes: 1 addition & 1 deletion src/OpenApi/src/Extensions/OpenApiDocumentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static OpenApiSchema AddOpenApiSchemaByReference(this OpenApiDocument doc
document.Components.Schemas[schemaId] = schema;
document.Workspace ??= new();
var location = document.BaseUri + "/components/schemas/" + schemaId;
document.Workspace.RegisterComponent(location, schema);
document.Workspace.RegisterComponentForDocument(document, schema, location);
return new OpenApiSchemaReference(schemaId, document);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public static IEndpointConventionBuilder MapOpenApi(this IEndpointRouteBuilder e
{
if (UseYaml(pattern))
{
document.Serialize(new OpenApiYamlWriter(writer), documentOptions.OpenApiVersion);
await document.SerializeAsync(new OpenApiYamlWriter(writer), documentOptions.OpenApiVersion);
context.Response.ContentType = "text/plain+yaml;charset=utf-8";
}
else
{
document.Serialize(new OpenApiJsonWriter(writer), documentOptions.OpenApiVersion);
await document.SerializeAsync(new OpenApiJsonWriter(writer), documentOptions.OpenApiVersion);
context.Response.ContentType = "application/json;charset=utf-8";
}
await context.Response.BodyWriter.WriteAsync(output.ToArray(), context.RequestAborted);
Expand Down
2 changes: 1 addition & 1 deletion src/OpenApi/src/Services/OpenApiDocumentProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async Task GenerateAsync(string documentName, TextWriter writer, OpenApiS
using var scopedService = serviceProvider.CreateScope();
var document = await targetDocumentService.GetOpenApiDocumentAsync(scopedService.ServiceProvider);
var jsonWriter = new OpenApiJsonWriter(writer);
document.Serialize(jsonWriter, openApiSpecVersion);
await document.SerializeAsync(jsonWriter, openApiSpecVersion);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be flowing a cancellation token from somewhere? Looks like this might only be for the command line tool so it's fine?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, for this particular codepath, we don't have a CancellationToken to pass through to this API. We could probably consider adding one for the interface overall as part of #58353 but it's not feasible at the moment.

}

/// <summary>
Expand Down
35 changes: 20 additions & 15 deletions src/OpenApi/src/Services/OpenApiDocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models.References;

namespace Microsoft.AspNetCore.OpenApi;

Expand Down Expand Up @@ -59,12 +60,12 @@ public async Task<OpenApiDocument> GetOpenApiDocumentAsync(IServiceProvider scop
// Schema and operation transformers are scoped per-request and can be
// pre-allocated to hold the same number of transformers as the associated
// options object.
IOpenApiSchemaTransformer[] schemaTransformers = _options.SchemaTransformers.Count > 0
var schemaTransformers = _options.SchemaTransformers.Count > 0
? new IOpenApiSchemaTransformer[_options.SchemaTransformers.Count]
: Array.Empty<IOpenApiSchemaTransformer>();
IOpenApiOperationTransformer[] operationTransformers = _options.OperationTransformers.Count > 0 ?
: [];
var operationTransformers = _options.OperationTransformers.Count > 0 ?
new IOpenApiOperationTransformer[_options.OperationTransformers.Count]
: Array.Empty<IOpenApiOperationTransformer>();
: [];
InitializeTransformers(scopedServiceProvider, schemaTransformers, operationTransformers);
var document = new OpenApiDocument
{
Expand Down Expand Up @@ -277,14 +278,7 @@ private async Task<OpenApiOperation> GetOperationAsync(
IOpenApiSchemaTransformer[] schemaTransformers,
CancellationToken cancellationToken)
{
var tags = GetTags(description);
if (tags != null)
{
foreach (var tag in tags)
{
document.Tags?.Add(tag);
}
}
var tags = GetTags(description, document);
var operation = new OpenApiOperation
{
OperationId = GetOperationId(description),
Expand All @@ -308,16 +302,27 @@ private async Task<OpenApiOperation> GetOperationAsync(
=> description.ActionDescriptor.AttributeRouteInfo?.Name ??
description.ActionDescriptor.EndpointMetadata.OfType<IEndpointNameMetadata>().LastOrDefault()?.EndpointName;

private static List<OpenApiTag>? GetTags(ApiDescription description)
private static List<OpenApiTagReference>? GetTags(ApiDescription description, OpenApiDocument document)
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
{
var actionDescriptor = description.ActionDescriptor;
if (actionDescriptor.EndpointMetadata?.OfType<ITagsMetadata>().LastOrDefault() is { } tagsMetadata)
{
return tagsMetadata.Tags.Select(tag => new OpenApiTag { Name = tag }).ToList();
List<OpenApiTagReference> tags = [];
foreach (var tag in tagsMetadata.Tags)
{
document.Tags ??= [];
document.Tags.Add(new OpenApiTag { Name = tag });
tags.Add(new OpenApiTagReference(tag, document));
captainsafia marked this conversation as resolved.
Show resolved Hide resolved

}
return tags;
}
// If no tags are specified, use the controller name as the tag. This effectively
// allows us to group endpoints by the "resource" concept (e.g. users, todos, etc.)
return [new OpenApiTag { Name = description.ActionDescriptor.RouteValues["controller"] }];
var controllerName = description.ActionDescriptor.RouteValues["controller"];
document.Tags ??= [];
document.Tags.Add(new OpenApiTag { Name = controllerName });
return [new OpenApiTagReference(controllerName, document)];
}

private async Task<OpenApiResponses> GetResponsesAsync(
Expand Down
14 changes: 10 additions & 4 deletions src/OpenApi/src/Services/OpenApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Primitives;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models.References;

namespace Microsoft.AspNetCore.OpenApi;

Expand Down Expand Up @@ -322,19 +323,22 @@ private static void GenerateDefaultResponses(Dictionary<int, (Type?, MediaTypeCo
return null;
}

private List<OpenApiTag> GetOperationTags(MethodInfo methodInfo, EndpointMetadataCollection metadata)
private List<OpenApiTagReference> GetOperationTags(MethodInfo methodInfo, EndpointMetadataCollection metadata)
{
var metadataList = metadata.GetOrderedMetadata<ITagsMetadata>();
var document = new OpenApiDocument();

if (metadataList.Count > 0)
{
var tags = new List<OpenApiTag>();
var tags = new List<OpenApiTagReference>();

foreach (var metadataItem in metadataList)
{
foreach (var tag in metadataItem.Tags)
{
tags.Add(new OpenApiTag() { Name = tag });
document.Tags ??= [];
document.Tags.Add(new OpenApiTag { Name = tag });
tags.Add(new OpenApiTagReference(tag, document));
}
}

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

return new List<OpenApiTag>() { new OpenApiTag() { Name = controllerName } };
document.Tags ??= [];
document.Tags.Add(new OpenApiTag { Name = controllerName });
return [new(controllerName, document)];
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
}

private List<OpenApiParameter> GetOpenApiParameters(MethodInfo methodInfo, RoutePattern pattern, bool disableInferredBody)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
"/getbyidandname/{id}/{name}": {
"get": {
"tags": [
{
"name": "Test"
}
"Test"
],
"parameters": [
{
Expand Down Expand Up @@ -59,9 +57,7 @@
"/forms": {
"post": {
"tags": [
{
"name": "Test"
}
"Test"
],
"requestBody": {
"content": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
"/forms/form-file": {
"post": {
"tags": [
{
"name": "Sample"
}
"Sample"
],
"requestBody": {
"content": {
Expand Down Expand Up @@ -40,9 +38,7 @@
"/forms/form-files": {
"post": {
"tags": [
{
"name": "Sample"
}
"Sample"
],
"requestBody": {
"content": {
Expand Down Expand Up @@ -72,9 +68,7 @@
"/forms/form-file-multiple": {
"post": {
"tags": [
{
"name": "Sample"
}
"Sample"
],
"requestBody": {
"content": {
Expand Down Expand Up @@ -118,9 +112,7 @@
"/forms/form-todo": {
"post": {
"tags": [
{
"name": "Sample"
}
"Sample"
],
"requestBody": {
"content": {
Expand All @@ -147,9 +139,7 @@
"/forms/forms-pocos-and-files": {
"post": {
"tags": [
{
"name": "Sample"
}
"Sample"
],
"requestBody": {
"content": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
"/responses/200-add-xml": {
"get": {
"tags": [
{
"name": "Sample"
}
"Sample"
],
"responses": {
"200": {
Expand All @@ -34,9 +32,7 @@
"/responses/200-only-xml": {
"get": {
"tags": [
{
"name": "Sample"
}
"Sample"
],
"responses": {
"200": {
Expand All @@ -55,9 +51,7 @@
"/responses/triangle": {
"get": {
"tags": [
{
"name": "Sample"
}
"Sample"
],
"responses": {
"200": {
Expand All @@ -76,9 +70,7 @@
"/responses/shape": {
"get": {
"tags": [
{
"name": "Sample"
}
"Sample"
],
"responses": {
"200": {
Expand Down
Loading
Loading