首页 文章

Swagger参数的默认值

提问于
浏览
7

如何在以下API生成的swagger中定义属性的默认值?

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.AspNetCore - https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs=visual-studio

3 回答

  • -1

    我总是在param上设置默认值,如下所示:

    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

    更新

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

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

    这是swagger json的样子:

    "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

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

  • 3

    如果您可以在控制器中执行此操作,则设置默认参数值的方式与此类似

    // GET api/products
    [HttpGet]
    public IEnumerable<Product> Get(int count = 50)
    {
        Conn mySqlGet = new Conn(_connstring);
        return mySqlGet.ProductList(count);
    }
    
  • 2

    在YAML文件中,您可以定义应该需要哪些属性 . 此示例来自NSwag配置 .

    /SearchPendingCases:
        post:
          summary: Search pending cases
          description: Searches for pending cases and orders them
          parameters:
            - in: body
              name: SearchQuery 
              required: true
              schema:
                type: object
                required:
                  - OrderBy
                  # do not include OrderDirection here because it is optional
                properties:
                  OrderBy:
                    description: Name of property for ordering
                    type: string
                    # you can remove the following two lines 
                    # if you do not want to check the string length
                    minLength: 1    
                    maxLength: 100
                  OrderDirection:
                    description: Optional order direction, default value: Descending
                    type: string
                    enum: [Ascending, Descending] # valid enum values
                    default: Descending           # default value
    

    Swagger - Enums

    Swagger - Unlocking the Spec: The default keyword

相关问题