NSwag无法为指定方法生成响应包装器的问题求助
NSwag部分API方法包装配置问题及文件方法包装咨询
问题场景
控制器中有两个用于生成API客户端的方法:
LoginUserAsync:需要包装以处理403状态码GetUserPictureAsync:无需包装(始终返回200,不存在的用户会返回默认图片)
使用配置项/WrapResponses:true /WrapResponseMethods:LoginUserAsync时,NSwag要么为所有方法生成包装器(导致GetUserPictureAsync出现编译错误),要么完全不生成包装器。已尝试多种方法命名格式修改(如{生成的客户端类名}.LoginUserAsync、UsersController.LoginUserAsync等),均无效果。
补充咨询
NSwag能否为文件获取类方法生成SwaggerWrapper包装,以处理404或204状态码?
相关代码与配置
控制器代码
[ApiController] [Route("users")] public sealed class UsersController(UsersService usersService) : ControllerBase { private readonly UsersService usersService = usersService; [HttpPost] [Route("login", Name = "LoginUserAsync")] [ProducesResponseType<string>(StatusCodes.Status200OK, "text/plain")] [ProducesResponseType<string>(StatusCodes.Status401Unauthorized, "text/plain")] public async Task<ActionResult<string>> LoginUserAsync([FromBody, Required] UserLoginDto user, CancellationToken cancellationToken) { Result<string> tokenResult = await usersService.GenerateTokenAsync(user, cancellationToken); if (tokenResult.IsFailed) { return Unauthorized(tokenResult.Errors[0].Message); } return Ok(tokenResult.Value); } [HttpGet] [Route("{userId:long}/picture", Name = "GetUserPictureAsync")] [Produces("image/jpeg", "image/png", "application/octet-stream")] [ProducesResponseType(StatusCodes.Status200OK)] public async Task<ActionResult> GetUserPictureAsync([Required] long userId, CancellationToken cancellationToken) { (byte[] photo, string type) = await usersService.GetUserPictureAsync(userId, cancellationToken); return File(photo, type); } }
项目配置项
<ItemGroup> <OpenApiReference Include="External\FastMessagingApi.json" Namespace="FastMessaging.Frontend.External.Api" Options="/WrapResponses:true /WrapResponseMethods:LoginUserAsync"> <ClassName></ClassName> </OpenApiReference> </ItemGroup>
OpenAPI JSON片段
"/users/login": { "post": { "tags": [ "Users" ], "operationId": "LoginUserAsync", "requestBody": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/UserLoginDto" } }, "text/json": { "schema": { "$ref": "#/components/schemas/UserLoginDto" } }, "application/*+json": { "schema": { "$ref": "#/components/schemas/UserLoginDto" } } }, "required": true }, "responses": { "200": { "description": "OK", "content": { "text/plain": { "schema": { "type": "string" } } } }, "401": { "description": "Unauthorized", "content": { "text/plain": { "schema": { "type": "string" } } } } } } }, "/users/{userId}/picture": { "get": { "tags": [ "Users" ], "operationId": "GetUserPictureAsync", "parameters": [ { "name": "userId", "in": "path", "required": true, "schema": { "pattern": "^-?(?:0|[1-9]\\d*)$", "type": "integer", "format": "int64" } } ], "responses": { "200": { "description": "OK", "content": { "image/jpeg": { }, "image/png": { }, "application/octet-stream": { } } } } } }
已尝试的无效操作
- 将
LoginUserAsync改为{生成的客户端类名}.LoginUserAsync - 将
LoginUserAsync改为UsersController.LoginUserAsync - 其他多种命名格式修改尝试
内容的提问来源于stack exchange,提问作者waifu_anton
相关产品推荐
相关产品推荐

