挥杆参数的默认值 [英] Swagger default value for parameter

查看:76
本文介绍了挥杆参数的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何定义通过以下API生成的特征的默认值?

How do I define default value for property in swagger generated from following API?

public class SearchQuery
{
        public string OrderBy { get; set; }

        [DefaultValue(OrderDirection.Descending)]
        public OrderDirection OrderDirection { get; set; } = OrderDirection.Descending;
}


public IActionResult SearchPendingCases(SearchQuery queryInput);

Swashbuckle生成OrderDirection作为必需参数.我想将其设置为可选,并向客户端指示默认值(不确定swagger是否支持此值).

Swashbuckle generates OrderDirection as required parameter. I would like to be it optional and indicate to client the default value (not sure if swagger supports this).

我不喜欢将属性类型设置为可为空.还有其他选择吗?理想情况下,使用内置类...

I don't like making the property type nullable. Is there any other option? Ideally using built in classes...

我使用Swashbuckle.AspNetCore- https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs=visual-studio

I use Swashbuckle.AspNetCore - https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs=visual-studio

推荐答案

我总是像这样设置param的默认值:

I've always set the default on the param itself like this:

public class TestPostController : ApiController
{
    public decimal Get(decimal x = 989898989898989898, decimal y = 1)
    {
        return x * y;
    }
}

这是在swagger-ui上的外观:
http://swashbuckletest.azurewebsites.net/swagger/ui/index#/TestPost/TestPost_Get

Here is how that looks like on the swagger-ui:
http://swashbuckletest.azurewebsites.net/swagger/ui/index#/TestPost/TestPost_Get

在对评论进行讨论之后,我更新了 Swagger-Net 来阅读通过反射DefaultValueAttribute 这是我正在使用的示例类:

Following the discussion on the comments I updated Swagger-Net to read the DefaultValueAttribute via reflection Here is the sample class I'm using:

public class MyTest
{
    [MaxLength(250)]
    [DefaultValue("HelloWorld")]
    public string Name { get; set; }
    public bool IsPassing { get; set; }
}

这是大张旗鼓的json的样子:

and here is how the swagger json looks like:

"MyTest": {
  "type": "object",
  "properties": {
    "Name": {
      "default": "HelloWorld",
      "maxLength": 250,
      "type": "string"
    },
    "IsPassing": {
      "type": "boolean"
    }
  },
  "xml": {
    "name": "MyTest"
  }
},

Swagger-Net 的源代码在这里:
https://github.com/heldersepu/Swagger-Net

The Source code of Swagger-Net is here:
https://github.com/heldersepu/Swagger-Net

测试项目的源代码在这里:
https://github.com/heldersepu/SwashbuckleTest

And the source code for the test project is here:
https://github.com/heldersepu/SwashbuckleTest

这篇关于挥杆参数的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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