在指定其他响应类型的同时自动生成默认的200 OK响应

编程入门 行业动态 更新时间:2024-10-24 10:21:53
本文介绍了在指定其他响应类型的同时自动生成默认的200 OK响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

根据#216 ,Swashbuckle将自动生成200成功响应作为默认行为,否则期望指定所有响应类型.

According to #216, Swashbuckle will automatically generate a 200 success response as the default behavior, or otherwise expects all response types to be specified.

我希望能够向XML注释中的某些端点添加404/400错误响应,同时为所有端点(包括具有404/400错误响应的端点)保留200个成功响应.

I'd like to be able to add a 404/400 error response to some endpoints in the XML comments while keeping the 200 success responses for all endpoints, including those with a 404/400 error response.

即使指定了错误响应,是否也可以使Swashbuckle继续为所有端点自动生成200成功响应?

Is it possible to make Swashbuckle continue to automatically generate a 200 success response for all endpoints even when an error response is specified?

我添加了一个操作过滤器,以编程方式添加了缺失的200个响应,从而解决了缺失的模型模式,但是仍然无法真正回答我最初的问题,即是否有一种方法可以使Swashbuckle自动生成200个成功响应

I added an operation filter to programmatically add the missing 200 responses, which resolved the missing model schemas, but still doesn't really answer my original question about whether there's a way to keep Swashbuckle's automatic generation of 200 success responses

class CustomOperationFilter : IOperationFilter { public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) { var responses = operation.responses; if (responses.ContainsKey("404") || responses.ContainsKey("400")) { responses.Add("200", new Response() { description = "OK", schema = (schemaRegistry.GetOrRegister(apiDescription.ActionDescriptor.ReturnType)) }); } } }

推荐答案

以下是一种变通方法,可将缺少的200个成功响应添加到已指定错误响应的端点:

Here is a workaround to add missing 200 success responses to endpoints that have specified error responses:

class CustomOperationFilter : IOperationFilter { public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) { var responses = operation.responses; if (!responses.ContainsKey("200")) { if (apiDescription.ActionDescriptor.ReturnType != null) { responses.Add("200", new Response() { description = "OK", schema = (schemaRegistry.GetOrRegister(apiDescription.ActionDescriptor.ReturnType)) }); } } } }

更多推荐

在指定其他响应类型的同时自动生成默认的200 OK响应

本文发布于:2023-10-30 22:34:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544203.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自动生成   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!