Skip to content

OpenAPI: merge multiple body content types into one operation #61401

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 18 additions & 1 deletion src/OpenApi/src/Services/OpenApiDocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ private async Task<Dictionary<OperationType, OpenApiOperation>> GetOperationsAsy
};

_operationTransformerContextCache.TryAdd(description.ActionDescriptor.Id, operationContext);
operations[description.GetOperationType()] = operation;

// Use index-based for loop to avoid allocating an enumerator with a foreach.
for (var i = 0; i < operationTransformers.Length; i++)
Expand All @@ -295,6 +294,24 @@ private async Task<Dictionary<OperationType, OpenApiOperation>> GetOperationsAsy
{
await endpointOperationTransformer.TransformAsync(operation, operationContext, cancellationToken);
}

var operationType = description.GetOperationType();
Copy link
Member

Choose a reason for hiding this comment

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

This logic looks great overall! However, I believe we want to apply this before operation transformers are applied so that transformers registered by the user get the correct view.

I'm reviewing this on mobile so if this is already the case and I missed it let me know.

Also, we might want to add a test case to verify that transformers see the correct data.

if (
operations.TryGetValue(operationType, out var existingOperation) &&
existingOperation.RequestBody?.Content is not null &&
operation.RequestBody is { Content.Count: > 0 }
)
{
// Merge additional accepted content types into the existing operation.
foreach (var body in operation.RequestBody.Content)
{
existingOperation.RequestBody.Content.Add(body);
}
}
else
{
operations[operationType] = operation;
}
}
return operations;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,40 @@ await VerifyOpenApiDocument(builder, document =>
});
}

/// <summary>
/// Tests documented behavior at https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-9.0#define-supported-request-content-types-with-the-consumes-attribute-1
/// </summary>
[Fact]
public async Task GetRequestBody_HandlesMultipleAcceptedContentType()
{
// Arrange
var builder = CreateBuilder();

// Act
builder.MapPost("/", [Consumes("application/json")] (TodoWithDueDate name) => { });
builder.MapPost("/", [Consumes("application/x-www-form-urlencoded")] ([FromForm] TodoWithDueDate name) => { });

// Assert
await VerifyOpenApiDocument(builder, document =>
{
var paths = Assert.Single(document.Paths.Values);
var operation = paths.Operations[OperationType.Post];
Assert.NotNull(operation.RequestBody);

Assert.Collection(operation.RequestBody.Content,
pair =>
{
Assert.Equal("application/json", pair.Key);
Assert.Equal("TodoWithDueDate", pair.Value.Schema.Annotations["x-schema-id"]);
},
pair =>
{
Assert.Equal("application/x-www-form-urlencoded", pair.Key);
Assert.Equal("TodoWithDueDate", pair.Value.Schema.Annotations["x-schema-id"]);
});
});
}

[Fact]
public async Task GetRequestBody_HandlesJsonBody()
{
Expand Down
Loading