Swashbuckle.AspNetCore
Swashbuckle.AspNetCore copied to clipboard
Include Descriptions from XML Comments For Minimal Api Not work
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.
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.
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.
Maybe this workaround can help: https://github.com/dotnet/aspnetcore/issues/37906#issuecomment-954494599
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.
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
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 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
@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
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.