Swashbuckle.AspNetCore icon indicating copy to clipboard operation
Swashbuckle.AspNetCore copied to clipboard

Include Descriptions from XML Comments For Minimal Api Not work

Open MayueCif opened this issue 3 years ago • 8 comments

I Create a .Net 6 Minimal Api Project ,But comment not display in swagger html。 The partten of this:https://github.com/domaindrivendev/Swashbuckle.AspNetCore#include-descriptions-from-xml-comments

Code: app.MapGet("Test", Handler.Test).WithName("Test"); public static class Handler { /// <summary> /// Test Comment /// </summary> public static string Test() { return "Test Comment"; } }

generate xml document is right。Isn't it supported yet? For Minimal Api.

MayueCif avatar Nov 05 '21 08:11 MayueCif

I see the same thing. Playing with Microsoft's "ToDo" examples for .Net Core and I see the xml comments for the ToDoItemDTO type show up in the documentation, but not for the app.MapGet entries.

Seems like classes do get comments added to their documentation.

Methods - the things mapped to http verbs - have documentation generated, but without the comments associated with them.

adrianstovall71 avatar Nov 21 '21 04:11 adrianstovall71

I see the same thing. Playing with Microsoft's "ToDo" examples for .Net Core and I see the xml comments for the ToDoItemDTO type show up in the documentation, but not for the app.MapGet entries.

Seems like classes do get comments added to their documentation.

Methods - the things mapped to http verbs - have documentation generated, but without the comments associated with them.

yes ,it not work for minimal api.present i can`t find solution seems.

MayueCif avatar Nov 22 '21 14:11 MayueCif

Maybe this workaround can help: https://github.com/dotnet/aspnetcore/issues/37906#issuecomment-954494599

farlop avatar Feb 17 '22 08:02 farlop

Hi folks! Yes, this isn't supported yet. We've actually got an issue tracking adding support for this over on the aspnetcore repo (https://github.com/dotnet/aspnetcore/issues/39927). If you'd like to see this happen, give it a thumbs-up and it'll help us with prioritization.

captainsafia avatar Feb 17 '22 18:02 captainsafia

I was able to get it running by using a structure like the following one with Swashbuckle.AspNetCore 6.3.0:

namespace MyAwesomeWebApi;

public static class GetContactsEndpoint
{
    public static WebApplication MapGetContactsEndpoint(this WebApplication app)
    {
        app.MapGet("/api/contacts", GetContacts)
           .Produces<ContactsListDto>()
           .Produces(StatusCodes.Status400BadRequest)
           .Produces(StatusCodes.Status500InternalServerError);
        return app;
    }

    /// <summary>
    /// Gets the contacts as a paged result.
    /// </summary>
    /// <param name="skip">The number of contacts to skip (optional). Default value is 0. Must be greater than or equal to 0.</param>
    /// <param name="take">The number of contacts to take (optional). Default value is 30. Must be between 1 and 100.</param>
    /// <param name="searchTerm">The search term (optional). White space at the front and back will be trimmed automatically. Contacts whose name start with the search term will be found.</param>
    /// <response code="400">Bad Request: the paging parameters are invalid.</response>
    public static async Task<IResult> GetContacts(int skip = 0, int take = 30, string? searchTerm = null)
    {
         if (skip < 0 || take is <= 0 or > 100)
             return Result.BadRequest();

         // Open a database session here, load the paged contacts and return them
         return Result.Ok(contactListDto);
    }
}

You can then map this endpoint in Program.cs like so

app.MapGetContactsEndpoint();

Of course, you need to setup Swashbuckle in your ASP.NET Core app as described here: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-6.0#openapi

feO2x avatar Apr 06 '22 10:04 feO2x

This worked!

On top of configuring Swashbuckle, I needed to add this extra part:

builder.Services.AddSwaggerGen(opts =>
{
    var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
    opts.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});

ch-lee avatar May 19 '22 21:05 ch-lee

@ch-lee I can confirm this worked:

In my case I still need to add this to the csproj file:

<GenerateDocumentationFile>true</GenerateDocumentationFile>

https://stackoverflow.com/questions/69790435/swagger-asp-net-core-minimal-api-include-xml-comments-files

LeoJHarris avatar May 21 '22 03:05 LeoJHarris

@ch-lee I can confirm this worked:

In my case I still need to add this to the csproj file:

<GenerateDocumentationFile>true</GenerateDocumentationFile>

https://stackoverflow.com/questions/69790435/swagger-asp-net-core-minimal-api-include-xml-comments-files

I just created a blank api in .net 6 and I needed to add it too!, Thanks

Hoopou avatar Sep 03 '22 16:09 Hoopou

On .NET 7 I was able to get this working, just as @feO2x describes, however examples on elements don't work. All descriptions are correctly shown.

dnperfors avatar Feb 18 '23 13:02 dnperfors