You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Minimal API] Incorrect OpenAPI definition of path, query and header parameters when there is a body and we're using WithOpenApi extension method
#45365
Closed
1 task done
marcominerva opened this issue
Nov 30, 2022
· 1 comment
· Fixed by #45404
This problem is related to #45305, but I have opened a new issue because I have identified a more generic scenario that involves it.
Let's consider the following endpoint:
app.MapPost("/api/sample/{id:guid}", (string id, [FromHeader] int number, bool boolean, Input input) =>
{
return TypedResults.Ok();
});
public record class Input(Guid id, int Number);
This produces a swagger.json definition that contains, among the others:
A new section content has been inserted in the definition, and it totally messes the documentation. This happens only if the endpoint contains also a body parameter. The only solution I have found is to create an OperationFilter that manually resets the Content property:
internal class ResetContentOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (operation.Parameters?.Any() ?? false)
{
foreach (var parameter in operation.Parameters)
{
if (parameter.In is ParameterLocation.Query or ParameterLocation.Path or ParameterLocation.Header)
{
parameter.Content = null;
}
}
}
}
}
Expected Behavior
content section should not be added to path, query and header parameters definition if I have an endpoint that contains also a body parameter and I'm using the WithOpenApi extension method.
content section should not be added to path, query and header parameters definition if I have an endpoint that contains also a body parameter and I'm using the WithOpenApi extension method.
To extend further, context shouldn't be part of the generated parameter definition since the schema is produced later down the line by Swashbuckle.
Is there an existing issue for this?
Describe the bug
This problem is related to #45305, but I have opened a new issue because I have identified a more generic scenario that involves it.
Let's consider the following endpoint:
This produces a swagger.json definition that contains, among the others:
If, however, I add the
WithOpenApi
extension method to the endpoint, swagger.json becomes:A new section content has been inserted in the definition, and it totally messes the documentation. This happens only if the endpoint contains also a body parameter. The only solution I have found is to create an
OperationFilter
that manually resets theContent
property:Expected Behavior
content section should not be added to path, query and header parameters definition if I have an endpoint that contains also a body parameter and I'm using the
WithOpenApi
extension method.Steps To Reproduce
Minimal repro here: https://github.com/marcominerva/OpenApiParameterIssue.
Exceptions (if any)
No response
.NET Version
7.0.100
Anything else?
No response
The text was updated successfully, but these errors were encountered: