It took me a long time to narrow this down, so I'm posting here to inform everyone of my findings as well as to try and determine if this is an actual issue that needs resolution (maybe I missed something obvious).
Anyways, according to the Microsoft documentation found here:
OpenAPI support in minimal API apps - Describe request body and parameters
"The framework infers the types for request parameters in the path, query, and header string automatically based on the signature of the route handler"
However it has been my experience that when specifying a delegate method (rather than an anonymous one), these parameters are not parsed for the associated documentation, and as such, are not described in the resultant swagger page.
This can be seen by running the following Program.cs:
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Server.Kestrel.Core.Features;
using Microsoft.OpenApi.Models;
using System.Reflection;
namespace MinAPI_SwagXML {
public class Program {
public static void Main(string[] args) {
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options => {
options.SwaggerDoc($"v1.0.0", new OpenApiInfo {
Title = "Minimal API Swagger Documentation Test",
Description = "Test page for documenting all aspects of Minimal API endpoints in Swagger.",
Version = $"v1.0.0"
});
//Use full namespaces for schema IDs to prevent potential collisions
options.CustomSchemaIds(x => x.FullName);
//Link xml documentation files to Swagger.
String bin = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
String[] docList = Directory.GetFiles(bin, "*.xml");
for (int i = 0; i < docList.Length; i++) {
options.IncludeXmlComments(docList[i]);
}
});
WebApplication app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) {
app.UseSwagger();
app.UseSwaggerUI(options => options.SwaggerEndpoint($"/swagger/v1.0.0/swagger.json", "v1.0.0"));
}
app.UseHttpsRedirection();
app.MapGet("/test/{one}", test).WithOpenApi(); // *** THIS IS THE CULPRIT!!!! ***
app.Run();
}
/// <summary>
/// This is the endpoint description...
/// </summary>
/// <param name="one">This is the parameter description for one...</param>
/// <param name="two">This is the parameter description for two...</param>
/// <param name="three">This is the parameter description for three...</param>
/// <returns>This is the method return description...</returns>
/// <response code="200">This is the 200 response code description...</response>
/// <response code="404">This is the 404 response code description...</response>
/// <response code="500">This is the 500 response code description...</response>
[ProducesResponseType(typeof(RequestData), StatusCodes.Status200OK)]
private static IResult test([FromRoute] String one, [FromQuery] String? two, [FromHeader] String? three) {
RequestData data = new RequestData() {
one = one,
two = two,
three = three
};
return Results.Ok(data);
}
}
/// <summary>
/// Request Data
/// </summary>
public class RequestData {
/// <summary>
/// Route Parameter
/// </summary>
public String one { get; set; } = String.Empty;
/// <summary>
/// Query Parameter
/// </summary>
public String? two { get; set; } = String.Empty;
/// <summary>
/// Header Parameter
/// </summary>
public String? three { get; set; } = String.Empty;
}
}
Note: Make sure your associated *.csproj file is setup to generate documentation, as such:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
</Project>
What I have found is that using .WithOpenApi() seems to be the root cause for the exclusion of the parameter descriptions. By default this is added to Web API projects (by the project template) when specifying to use Minimal APIs. However removing this chained method resolves the issue and the parameter descriptions are subsequently added to the swagger page.
Can anyone add any insight on this topic?
Is there some other way to achieve the desired output while still using .WithOpenApi()?
Or does this need to be reported as an issue for resolution, and would this be a .NET issue or a Swashbuckle one?