Skip to content

Commit

Permalink
Update WithOpenApi docs and scrub description IDs (#55924)
Browse files Browse the repository at this point in the history
  • Loading branch information
captainsafia authored May 31, 2024
1 parent 399a5ed commit 5f8f891
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public static class OpenApiEndpointConventionBuilderExtensions
/// Adds an OpenAPI annotation to <see cref="Endpoint.Metadata" /> associated
/// with the current endpoint.
/// </summary>
/// <remarks>
/// This method does not integrate with built-in OpenAPI document generation support in ASP.NET Core
/// and is primarily intended for consumption along-side Swashbuckle.AspNetCore.
/// </remarks>
/// <param name="builder">The <see cref="IEndpointConventionBuilder"/>.</param>
/// <returns>A <see cref="IEndpointConventionBuilder"/> that can be used to further customize the endpoint.</returns>
[RequiresDynamicCode(TrimWarningMessage)]
Expand All @@ -38,6 +42,10 @@ public static TBuilder WithOpenApi<TBuilder>(this TBuilder builder) where TBuild
/// Adds an OpenAPI annotation to <see cref="Endpoint.Metadata" /> associated
/// with the current endpoint and modifies it with the given <paramref name="configureOperation"/>.
/// </summary>
/// <remarks>
/// This method does not integrate with built-in OpenAPI document generation support in ASP.NET Core
/// and is primarily intended for consumption along-side Swashbuckle.AspNetCore.
/// </remarks>
/// <param name="builder">The <see cref="IEndpointConventionBuilder"/>.</param>
/// <param name="configureOperation">An <see cref="Func{T, TResult}"/> that returns a new OpenAPI annotation given a generated operation.</param>
/// <returns>A <see cref="IEndpointConventionBuilder"/> that can be used to further customize the endpoint.</returns>
Expand Down
16 changes: 16 additions & 0 deletions src/OpenApi/src/Services/OpenApiConstants.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.OpenApi.Models;

namespace Microsoft.AspNetCore.OpenApi;

internal static class OpenApiConstants
Expand All @@ -10,4 +12,18 @@ internal static class OpenApiConstants
internal const string DefaultOpenApiRoute = "/openapi/{documentName}.json";
internal const string DescriptionId = "x-aspnetcore-id";
internal const string DefaultOpenApiResponseKey = "default";
// Since there's a finite set of operation types that can be included in a given
// OpenApiPaths, we can pre-allocate an array of these types and use a direct
// lookup on the OpenApiPaths dictionary to avoid allocating an enumerator
// over the KeyValuePairs in OpenApiPaths.
internal static readonly OperationType[] OperationTypes = [
OperationType.Get,
OperationType.Post,
OperationType.Put,
OperationType.Delete,
OperationType.Options,
OperationType.Head,
OperationType.Patch,
OperationType.Trace
];
}
3 changes: 3 additions & 0 deletions src/OpenApi/src/Services/OpenApiDocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ internal sealed class OpenApiDocumentService(
{
private readonly OpenApiOptions _options = optionsMonitor.Get(documentName);
private readonly OpenApiComponentService _componentService = serviceProvider.GetRequiredKeyedService<OpenApiComponentService>(documentName);
private readonly IOpenApiDocumentTransformer _scrubExtensionsTransformer = new ScrubExtensionsTransformer();

private static readonly OpenApiEncoding _defaultFormEncoding = new OpenApiEncoding { Style = ParameterStyle.Form, Explode = true };

Expand Down Expand Up @@ -75,6 +76,8 @@ private async Task ApplyTransformersAsync(OpenApiDocument document, Cancellation
var transformer = _options.DocumentTransformers[i];
await transformer.TransformAsync(document, documentTransformerContext, cancellationToken);
}
// Remove `x-aspnetcore-id` extension from operations after all transformers have run.
await _scrubExtensionsTransformer.TransformAsync(document, documentTransformerContext, cancellationToken);
}

// Note: Internal for testing.
Expand Down
18 changes: 2 additions & 16 deletions src/OpenApi/src/Transformers/DelegateOpenApiDocumentTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,6 @@ namespace Microsoft.AspNetCore.OpenApi;

internal sealed class DelegateOpenApiDocumentTransformer : IOpenApiDocumentTransformer
{
// Since there's a finite set of operation types that can be included in a given
// OpenApiPaths, we can pre-allocate an array of these types and use a direct
// lookup on the OpenApiPaths dictionary to avoid allocating an enumerator
// over the KeyValuePairs in OpenApiPaths.
private static readonly OperationType[] _operationTypes = [
OperationType.Get,
OperationType.Post,
OperationType.Put,
OperationType.Delete,
OperationType.Options,
OperationType.Head,
OperationType.Patch,
OperationType.Trace
];
private readonly Func<OpenApiDocument, OpenApiDocumentTransformerContext, CancellationToken, Task>? _documentTransformer;
private readonly Func<OpenApiOperation, OpenApiOperationTransformerContext, CancellationToken, Task>? _operationTransformer;

Expand All @@ -48,9 +34,9 @@ public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransf
var documentService = context.ApplicationServices.GetRequiredKeyedService<OpenApiDocumentService>(context.DocumentName);
foreach (var pathItem in document.Paths.Values)
{
for (var i = 0; i < _operationTypes.Length; i++)
for (var i = 0; i < OpenApiConstants.OperationTypes.Length; i++)
{
var operationType = _operationTypes[i];
var operationType = OpenApiConstants.OperationTypes[i];
if (!pathItem.Operations.TryGetValue(operationType, out var operation))
{
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.OpenApi.Models;

namespace Microsoft.AspNetCore.OpenApi;

/// <summary>
/// A transformer class that removes implementation-specific extension properties
/// from the OpenAPI document.
/// </summary>
internal sealed class ScrubExtensionsTransformer : IOpenApiDocumentTransformer
{
public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken)
{
foreach (var pathItem in document.Paths.Values)
{
for (var i = 0; i < OpenApiConstants.OperationTypes.Length; i++)
{
var operationType = OpenApiConstants.OperationTypes[i];
if (!pathItem.Operations.TryGetValue(operationType, out var operation))
{
continue;
}

operation.Extensions.Remove(OpenApiConstants.DescriptionId);
}
}
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ await Verifier.Verify(GetOpenApiJson(document))
.UseDirectory(SkipOnHelixAttribute.OnHelix()
? Path.Combine(Environment.GetEnvironmentVariable("HELIX_WORKITEM_ROOT"), "Integration", "snapshots")
: "snapshots")
.ScrubLinesContaining(Microsoft.AspNetCore.OpenApi.OpenApiConstants.DescriptionId)
.UseParameters(documentName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"200": {
"description": "OK"
}
},
}
}
},
"/forms/form-files": {
Expand Down Expand Up @@ -73,7 +73,7 @@
"200": {
"description": "OK"
}
},
}
}
},
"/forms/form-file-multiple": {
Expand Down Expand Up @@ -124,7 +124,7 @@
"200": {
"description": "OK"
}
},
}
}
},
"/forms/form-todo": {
Expand Down Expand Up @@ -207,7 +207,7 @@
"200": {
"description": "OK"
}
},
}
}
},
"/forms/forms-pocos-and-files": {
Expand Down Expand Up @@ -271,7 +271,7 @@
"200": {
"description": "OK"
}
},
}
}
},
"/getbyidandname/{id}/{name}": {
Expand Down Expand Up @@ -320,7 +320,7 @@
}
}
}
},
}
}
},
"/forms": {
Expand Down Expand Up @@ -360,7 +360,7 @@
"200": {
"description": "OK"
}
},
}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
}
}
}
},
}
}
},
"/responses/200-only-xml": {
Expand Down Expand Up @@ -111,7 +111,7 @@
}
}
}
},
}
}
},
"/responses/triangle": {
Expand All @@ -130,7 +130,7 @@
}
}
}
},
}
}
},
"/responses/shape": {
Expand Down Expand Up @@ -172,7 +172,7 @@
}
}
}
},
}
}
},
"/getbyidandname/{id}/{name}": {
Expand Down Expand Up @@ -221,7 +221,7 @@
}
}
}
},
}
}
},
"/forms": {
Expand Down Expand Up @@ -261,7 +261,7 @@
"200": {
"description": "OK"
}
},
}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
}
}
}
},
}
}
},
"/v1/todos": {
Expand Down Expand Up @@ -102,7 +102,7 @@
"200": {
"description": "OK"
}
},
}
}
},
"/v1/todos/{id}": {
Expand Down Expand Up @@ -168,7 +168,7 @@
}
}
}
},
}
}
},
"/getbyidandname/{id}/{name}": {
Expand Down Expand Up @@ -225,7 +225,7 @@
}
}
}
},
}
}
},
"/forms": {
Expand Down Expand Up @@ -275,7 +275,7 @@
"200": {
"description": "OK"
}
},
}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
}
}
}
},
}
},
"post": {
"tags": [
Expand All @@ -41,7 +41,7 @@
"200": {
"description": "OK"
}
},
}
}
},
"/getbyidandname/{id}/{name}": {
Expand Down Expand Up @@ -90,7 +90,7 @@
}
}
}
},
}
}
},
"/forms": {
Expand Down Expand Up @@ -130,7 +130,7 @@
"200": {
"description": "OK"
}
},
}
}
}
},
Expand Down

0 comments on commit 5f8f891

Please sign in to comment.