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

Models used in Minimal API Endpoint Metadata do not respect the Swashbuckle Annotations Attributes #52713

Closed
1 task done
syedsuhaib opened this issue Dec 11, 2023 · 4 comments
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update.

Comments

@syedsuhaib
Copy link

syedsuhaib commented Dec 11, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I am using the Delegate overload of MapGet / MapDelete in a custom extension method of IEndpointRouteBuilder to add a couple of endpoints - which then gets called in the main Program.cs. Currently, I am calling WithOpenApi() on the RouteGroupBuilder before calling the MapX extension methods:

 public static IEndpointRouteBuilder MapTwoEndpoints(this IEndpointRouteBuilder endpoints)
 {
     var group = endpoints.MapGroup("/base");

   group.AddEndpointFilterFactory(/* some validation related filters */)
        .WithOpenApi(op =>
        {
            op.Parameters.Add(
                  /* details omitted */
            );
            return op;
        });

     group.MapGet("/{Id}", async ([AsParameters] RequestModel request, [FromServices] ISomeService service)
         =>
     {
        /* call the service */
         return TypedResults.Ok(myResponse); // myResponse is returned from the service
     })
         .Produces<MyResponse>()
         .Produces<Error>(StatusCodes.Status404NotFound); // Error model is defined elsewhere

     group.MapDelete("/{Id}", async ([AsParameters] RequestModel request, [FromServices] ISomeService service)
         =>
     {
         /* call the service */
         return TypedResults.NoContent();
     })
         .Produces(StatusCodes.Status204NoContent);

     return group;
 }

I would like to customize the display title of the response dto (MyResponse) in Swagger. While using controllers, decorating the response model with a [SwaggerSchema(Title = "Something")] would do the trick. However, this no longer seems to be respected by minimal APIs. SwaggerUI still shows the type name under 'Schema' at the bottom, rather than the value of Title specified via the SwaggerSchemaAttribute decoration.

Please note that:

  1. I am using the [AsParameters] attribute on the request model; hope that is not affecting the way endpoint metadata is constructed?
  2. My response model is a record, not a plain class.

Expected Behavior

The display title of the request or response model as specified via the SwaggerSchemaAttribute on the respective models should be generated in the Open API json file and also visible in the Swagger UI.

Steps To Reproduce

[SwaggerSchema(Title = "SchemaNameToShowInSwagger", Description = "I also expect to see this description is swagger.")]
public class MyResponse
{
}

Exceptions (if any)

None

.NET Version

8.0.100

Anything else?

Already saw #44970 and #2652. Was advised to create a separate issue.

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Dec 11, 2023
@captainsafia
Copy link
Member

@syedsuhaib I suspect that this issue might be related to the order in which Swashbuckle's annotations are processed on endpoints relative to the WithOpenApi call. See the issue reported at domaindrivendev/Swashbuckle.AspNetCore#2561.

As an alternatively, you can consider setting the title and description yourself using the WithOpenApi method:

.WithOpenApi(operation => {
   operation.Responses["200"].Description = "..."
});

@captainsafia captainsafia added Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Dec 15, 2023
@ghost
Copy link

ghost commented Dec 15, 2023

Hi @syedsuhaib. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@syedsuhaib
Copy link
Author

@captainsafia, I tried your suggestion, and it sets the description of the response itself. However, what I am after is to customize the name of the type which this response returns in body (i.e, name of the schema generated in OpenAPI JSON):
image

I would like the control over nomenclature of all schemas that are used by various requests and responses and are also displayed by Swagger UI at the end:
image
As I mentioned, currently, it defaults to the C# type name which - as per practice of many developers - may have suffix "dto" for example. Motivation to customize would be to make it friendlier to the consuming developers.

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Dec 19, 2023
@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2024
@wtgodbe wtgodbe removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2024
@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2024
@wtgodbe wtgodbe removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 13, 2024
@dotnet dotnet deleted a comment from dotnet-policy-service bot Feb 13, 2024
@dotnet dotnet deleted a comment from dotnet-policy-service bot Feb 13, 2024
@captainsafia
Copy link
Member

@syedsuhaib We just published an announcement about the future of OpenAPI support in the ASP.NET Core over in this issue.

One of the things that we are considering is adding first-class support for aggregating XML doc info into the generated OpenAPI document as tracked in #39927.

Since I believe that would solve your dilemma of being able to add descriptions to response types I'm going to close this issue in favor of using the other one to track this work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update.
Projects
None yet
Development

No branches or pull requests

3 participants