[英]Swagger default value for parameter
How do I define default value for property in swagger generated from following API?如何在从以下 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 generates OrderDirection as required parameter. Swashbuckle 生成 OrderDirection 作为必需参数。 I would like to be it optional and indicate to client the default value (not sure if swagger supports this).我希望它是可选的,并向客户端指示默认值(不确定 swagger 是否支持这一点)。
I don't like making the property type nullable.我不喜欢使属性类型可以为空。 Is there any other option?还有其他选择吗? Ideally using built in classes...理想情况下使用内置类...
I use Swashbuckle.AspNetCore - https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs=visual-studio我使用 Swashbuckle.AspNetCore - https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs=visual-studio
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;
}
}
Here is how that looks like on the swagger-ui:这是 swagger-ui 上的样子:
http://swashbuckletest.azurewebsites.net/swagger/ui/index#/TestPost/TestPost_Get http://swashbuckletest.azurewebsites.net/swagger/ui/index#/TestPost/TestPost_Get
Following the discussion on the comments I updated Swagger-Net to read the DefaultValueAttribute
via reflection Here is the sample class I'm using:在对评论的讨论之后,我更新了Swagger-Net以通过反射读取DefaultValueAttribute
这是我正在使用的示例类:
public class MyTest
{
[MaxLength(250)]
[DefaultValue("HelloWorld")]
public string Name { get; set; }
public bool IsPassing { get; set; }
}
and here is how the swagger json looks like:这是 swagger json 的样子:
"MyTest": {
"type": "object",
"properties": {
"Name": {
"default": "HelloWorld",
"maxLength": 250,
"type": "string"
},
"IsPassing": {
"type": "boolean"
}
},
"xml": {
"name": "MyTest"
}
},
The Source code of Swagger-Net is here: Swagger-Net的源代码在这里:
https://github.com/heldersepu/Swagger-Net https://github.com/heldersepu/Swagger-Net
And the source code for the test project is here:测试项目的源代码在这里:
https://github.com/heldersepu/SwashbuckleTest https://github.com/heldersepu/SwashbuckleTest
Setting the default parameter value works like this if you can do it in your controller如果您可以在控制器中执行此操作,则设置默认参数值的工作方式如下
// GET api/products
[HttpGet]
public IEnumerable<Product> Get(int count = 50)
{
Conn mySqlGet = new Conn(_connstring);
return mySqlGet.ProductList(count);
}
This is applicable for ASP.net MVC5, code is not valid for .Net Core这适用于 ASP.net MVC5,代码对 .Net Core 无效
1- Define a custom attribute as following 1-定义自定义属性如下
public class SwaggerDefaultValueAttribute: Attribute
{
public SwaggerDefaultValueAttribute(string param, string value)
{
Parameter = param;
Value = value;
}
public string Parameter {get; set;}
public string Value {get; set;}
}
2- Define a Swagger OperationFilter class 2- 定义一个 Swagger OperationFilter 类
public class AddDefaulValue: IOperationFilter
{
void IOperationFilter.Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
if (operation.parameters == null || !operation.parameters.Any())
{
return;
}
var attributes = apiDescription.GetControllerAndActionAttributes<SwaggerDefaultValueAttribute>().ToList();
if (!attributes.Any())
{
return;
}
foreach(var parameter in operation.parameters)
{
var attr = attributes.FirstOrDefault(it => it.Parameter == parameter.name);
if(attr != null)
{
parameter.@default = attr.Value;
}
}
}
}
3- Register the OperationFilter at SwaggerConfig file 3- 在 SwaggerConfig 文件中注册 OperationFilter
c.OperationFilter<AddDefaultValue>();
4- Decorate your controller method with attributes 4- 用属性装饰你的控制器方法
[SwaggerDefaultValue("param1", "AnyValue")]
public HttpResponseMessage DoSomething(string param1)
{
return Request.CreateResponse(HttpStatusCode.OK);
}
In the YAML file, you can define which properties should be required.在 YAML 文件中,您可以定义应该需要哪些属性。 This example is from a NSwag configuration.此示例来自 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 - Unlocking the Spec: The default keyword Swagger - 解锁规范:默认关键字
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.