在指定其他响应类型的同时自动生成默认的200 OK响应 [英] Automatically generate default 200 OK response while specifying other response types

查看:77
本文介绍了在指定其他响应类型的同时自动生成默认的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响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆