FastEndpoints无法为GET请求生成Swagger查询参数文档的问题
问题
我在基于FastEndpoints的ASP.NET Core 7应用中,尝试让查询参数显示在生成的Swagger文档里,但目前参数列表是空的。我的代码如下:
端点类
public class EchoEndpoint : EndpointBase<EchoPayload, EchoPayload> { private readonly ILogger<EchoEndpoint> _logger; public EchoEndpoint(ILogger<EchoEndpoint> logger) { _logger = logger; } public override void Configure() { Get("test/echo"); Description(b => b .Produces<EchoPayload>(StatusCodes.Status200OK, "application/json") .Produces(StatusCodes.Status401Unauthorized), clearDefaults:true ); Summary(s => { s.Summary = "quick test to echo the received payload"; s.Params["message"] = "message to echo back"; }); base.Configure(); } public override async Task HandleAsync(EchoPayload req, CancellationToken token) { _logger.LogInformation("Echo called"); await SendOkAsync(req, token); } } public class EchoPayload { [QueryParam] public string Message { get; set; } = ""; }
Program.cs中的配置
public static IServiceCollection ConfigureSwagger(this IServiceCollection services, IConfiguration configuration) { services.AddSwaggerDoc(AddSwaggerDocs_ConfigureAuth, addJWTBearerAuth: false, serializerSettings: c => { c.PropertyNamingPolicy = null; c.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; }, removeEmptySchemas: false); return services; } private static void ConfigureSwagger(this IApplicationBuilder app) { app.UseOpenApi(c => { // no options yet }); app.UseSwaggerUi3(c => { c.ConfigureDefaults(); c.OAuth2Client = new OAuth2ClientSettings { //TODO: read from config ClientId = "", AppName = "WebApi", UsePkceWithAuthorizationCodeGrant = true }; }); }
我研究了FastEndpoints的源码,发现问题似乎出在OperationProcessor类的Process方法里的var apiDescription = ((AspNetCoreOperationProcessorContext)ctx).ApiDescription;变量的ParameterDescriptions属性,这导致DTO的[QueryParam]属性没被检测到。请问需要补充哪些配置才能让NSwag生成的Swagger文档显示查询参数?
解决方案
要让FastEndpoints识别DTO中的[QueryParam]并生成正确的Swagger文档,可按以下步骤调整:
- 明确指定请求绑定源
在端点的Configure方法中,需显式声明请求从查询参数绑定,这是Swagger生成参数文档的关键触发条件:
public override void Configure() { Get("test/echo"); RequestBinding(BindingSource.Query); // 指定请求绑定来源为查询参数 Description(b => b .Produces<EchoPayload>(StatusCodes.Status200OK, "application/json") .Produces(StatusCodes.Status401Unauthorized), clearDefaults:true ); Summary(s => { s.Summary = "quick test to echo the received payload"; s.Params["Message"] = "message to echo back"; // 键名需与DTO属性名完全匹配 }); base.Configure(); }
- 调整Swagger注册配置
在AddSwaggerDoc中补充文档处理器配置,确保FastEndpoints的参数逻辑被NSwag正确识别:
services.AddSwaggerDoc(AddSwaggerDocs_ConfigureAuth, addJWTBearerAuth: false, serializerSettings: c => { c.PropertyNamingPolicy = null; c.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; }, documentSettings: doc => { // 添加FastEndpoints的ASP.NET Core操作处理器 doc.OperationProcessors.Add(new AspNetCoreOperationProcessor(doc)); }, removeEmptySchemas: false);
修正Summary参数键名匹配
确保Summary.Params的键名与DTO中属性名大小写完全一致(示例中DTO属性为Message,而非小写的message),否则Swagger无法关联参数描述。升级FastEndpoints版本
若使用旧版本,建议升级到最新稳定版,新版本修复了多处与NSwag集成的参数解析问题。
完成以上调整后,重新生成Swagger文档,查询参数即可正常显示。
内容的提问来源于stack exchange,提问作者Alexei - check Codidact
相关产品推荐
相关产品推荐

