Swagger UI 不会在控制器的 GET 操作中为我的复杂类型参数呈现主体参数字段

巴赫拉姆齐

我有一个 ASP.NET Web API 2 项目,我在其中添加了 Swagger - Swashbuckle v5.6.0。一切正常。Swagger UI 按预期呈现我的 API 的测试端点。

我在我的 API 中添加了一个新的控制器。有一个GET带有复杂类型参数的操作。对于复杂类型,Web API 尝试从消息正文中读取值。这是默认行为。

这是我的 GET 操作:

    [HttpGet]
    [Route("search")]
    [ResponseType(typeof(List<SearchModel>))]
    public IHttpActionResult Search(SearchModel searchOptions)
    {
        //....
        
        return Ok();
    }

她是我的复杂类型:

public class SearchModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    [DataType(DataType.EmailAddress)]
    [EmailAddress]
    public string Email { get; set; }

    public string AddressLine1 { get; set; }

    public string City { get; set; }

    public string Telephone { get; set; }

    public string MobilePhone { get; set; }
}

问题:

但是 Swagger UI 不会在GET操作中为我的复杂类型呈现 body 参数字段ForPOSTPUTactions Swagger UI 按预期呈现 body 参数字段,但不适用于我的GET操作中的复杂类型

Swagger UI 中我的 GET 操作的屏幕截图

从截图中可以看出,Swagger UI 为我的复杂类型中的属性呈现查询参数字段,而不是像在POSTand的情况下那样为我的类型呈现主体参数字段PUT

GET从 Postman 进行测试并在请求正文中填充 json 时,我的操作运行良好。通过在 Visual Studio 中的操作中设置断点,我可以看到值绑定到我的操作参数中的对象。

邮递员截图

I have tried to decorate the parameter in my action with [FromBody] (which is the default for complex type) but same result.

Is this a bug in Swagger? Or am I missing something?

G.Dimov

Sadly, you can't do what you want with Swagger. You can't send a request model in an HTTP GET method. You can however change the swagger UI to look like this:

用身体大摇大摆地获取

but you won't be able to receive the model in your controller.

This is a known issue within the Swagger developers and it was discussed in 2016 and the final decision is that swagger won't support a request body in an HTTP GET method. Here is the link to the already closed issue.

You have three options here:

  • Leave the method as it is, and test it in Postman, but not in Swagger.
  • 请按照以下步骤实现上图,但请注意,它只会修复 UI 部分,并且null SearchModel当您按下Try it out!swagger,您将始终在控制器中结束
  • 使它成为一种[HttpPost方法而不是[HttpGet].

如何使 swagger UI 显示带有请求正文的 GET 方法:

首先,创建一个Attribute类:

public class ModelInBodyAttribute : Attribute
{
    public ModelInBodyAttribute(string modelName, string description, bool isRequired)
    {
        this.ModelName = modelName;
        this.Description = description;
        this.IsRequired = IsRequired;
    }

    public string ModelName { get; set; }
    public bool IsRequired { get; set; }
    public string Description { get; set; }
}

然后你可以在控制器中装饰你的方法:

[ModelInBody(modelName: nameof(SearchModel), description: "My model description", isRequired: true)]
[HttpGet]
[Route("search")]
[ResponseType(typeof(List<SearchModel>))]
public IHttpActionResult Search(SearchModel searchOptions)
{
    //....

    return Ok(new List<SearchModel>());
}

之后创建IOperationFilter类(ModelInBodyOperationFilter):

public class ModelInBodyOperationFilter : IOperationFilter
{
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        var attribute = apiDescription.GetControllerAndActionAttributes<ModelInBodyAttribute>().FirstOrDefault();
        if (attribute == null)
        {
            return;
        }

        operation.parameters.Clear();
        operation.parameters.Add(new Parameter
        {
            name = attribute.ModelName,
            description = attribute.Description,
            @in = "body",
            required = attribute.IsRequired,
            schema = new Schema { @ref = $"#/definitions/{attribute.ModelName}" }
        });
    }
}

最后,不要忘记注册IOperationFilterin SwaggerConfig

c.OperationFilter<ModelInBodyOperationFilter>();

当您通过 swagger 发送请求时,您会注意到该Curl部分是绝对正确的,但仍然在您的控制器中没有任何内容。
卷曲示例

example of GET method with a body in controller

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Swagger UI不会在多部分请求中传递部分的内容类型

ngRoute不会在angularjs控制器中触发

OAS 3.0 中的 anyOf 用于 url 编码的表单数据的 requestBody 不会在 swagger UI 中呈现明显的输入

无法从swagger UI中的控制器获取我的端点列表

C# .NET 仅为特定的 api 控制器或模块(项目)中的 api 控制器启用 Swagger UI

Cookies不会在JMeter“循环控制器”中的迭代时重置吗?

Rails不会在FreeBSD 9.1中的每个请求上重载控制器,助手

ID不会在Rails 4中跨模型/控制器保存到DB

如何在 swagger-ui 中更改控制器的名称?

有没有办法在swagger-ui页面中更改控制器的名称?

添加控制器后,在Swagger UI中不允许使用该方法

Swagger Ui 不会在 asp.net mvc 上加载静态文件

ng-click 不会在表 td 单击上触发控制器功能,该表在控制器初始化后稍后呈现

ASP.NET Core MVC - 数据不会在发布请求中从视图传递到控制器

Spring MVC不会在HTTP POST中自动装配,而是在同一控制器上的Ajax中装配

Swagger中的重载控制器方法

Swagger:在Swagger UI中更改API路线

在Swagger文档中隐藏控制器参数

复杂类型的属性为null或AJAX POST调用时控制器操作参数中的默认值

Swagger参数和复杂类型

在Swagger UI中添加请求参数

如何定义Swagger 2.0 JSON以在Swagger UI中填充默认主体参数对象?

在Swagger UI中实施搜索

在 Swagger UI 中拦截消息

文本不会在快速UI中包裹

动态设置DropDown值不会在UI中刷新

Swagger-ui 在查询参数中展平对象,但生成的 Angular 客户端不会

球衣2:正确呈现Swagger静态内容而不会在斜杠(/)后面加上

Swagger-ui-列表参数编码