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

[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

Comments

@marcominerva
Copy link
Contributor

Is there an existing issue for this?

  • I have searched the existing issues

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:

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:

{
  "name": "id",
  "in": "path",
  "required": true,
  "style": "simple",
  "schema": {
    "type": "string"
  }
}

If, however, I add the WithOpenApi extension method to the endpoint, swagger.json becomes:

{
  "name": "id",
  "in": "path",
  "required": true,
  "style": "simple",
  "schema": {
    "type": "string"
  },
  "content": {
    "application/json": { }
  }
}

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.

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

@captainsafia
Copy link
Member

Thanks for filing this issue, @marcominerva!

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.

https://github.com/microsoft/OpenAPI.NET/blob/714401356834a92896b429520065a765e226bc4a/src/Microsoft.OpenApi/Models/OpenApiParameter.cs#L129-L138

Should be a straightforward fix and a good backport candidate.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Development

Successfully merging a pull request may close this issue.

4 participants