[英]Swashbuckle Swagger UI: How to remove required from parameters in xml commenting
I would like to change the required attribute on a some of the parameters in my controllers. 我想在控制器中的某些参数上更改所需的属性。 I used XML comments in order to link to Swagger. 我使用XML注释来链接到Swagger。
Before proceeding, think carefully about your parameter. 在继续之前,请仔细考虑您的参数。 Is the parameter truly required, and does your typing reflect that? 该参数是否确实需要,您的键入内容是否反映出来? Is there a sane default value, and what would be the expected behavior in that case? 是否有合理的默认值?在这种情况下,预期的行为是什么? Depending on your answer, you may prefer one of these two solutions: 根据您的答案,您可能更喜欢以下两种解决方案之一:
If accessTokenID
has a sane default value, you can specify that on the API signature and Swashbuckle will stop identifying the parameter as required. 如果accessTokenID
具有合理的默认值,则可以在API签名上指定该值,并且Swashbuckle将根据需要停止标识该参数。
For example, id
in this example would resolve as optional in Swagger UI: 例如,此示例中的id
在Swagger UI中将解析为可选:
public HttpResponseMessage Get(int id = 0)
If your parameter is truly not required, a nullable type might make more sense (for example, if you list all values on null input): 如果确实不需要参数,则可为空的类型可能更有意义(例如,如果您在空输入中列出所有值):
public HttpResponseMessage Get(int? id = null)
A solution in the Swashbuckle GitHub created an IOperationFilter to process SwaggerDefaultValue attributes and apply them to Swagger UI. Swashbuckle GitHub中的一个解决方案创建了一个IOperationFilter来处理SwaggerDefaultValue属性并将其应用于Swagger UI。 You can use this solution if you would prefer to require the parameter, but would like to set some default in Swagger UI. 如果您希望使用此参数,但是想在Swagger UI中设置一些默认值,则可以使用此解决方案。
For example, this would show "0" in the Swagger UI text field instead of " (required) ": 例如,这将在Swagger UI文本字段中显示“ 0”,而不是“ ((必需) ”):
[SwaggerDefaultValue("id", "0")]
public HttpResponseMessage Get(int id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.