简体   繁体   English

Swashbuckle Swagger UI:如何从xml注释中的参数中删除必需的

[英]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: 根据您的答案,您可能更喜欢以下两种解决方案之一:

Option 1: Optional Parameters 选项1:可选参数

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)

Option 2: SwaggerDefaultValue Attribute 选项2:SwaggerDefaultValue属性

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.

相关问题 如何在 swagger UI 中使用 Swashbuckle 设置内容类型 application/json 和 application/xml - How to set the content type application/json and application/xml using Swashbuckle in swagger UI 如何解决 generics 的 Swashbuckle/Swagger -UI model 命名问题? - How to resolve Swashbuckle/Swagger -UI model naming issue for generics? AspNetCore Swagger / Swashbuckle如何修改xml模式请求 - AspNetCore Swagger/Swashbuckle how to modify xml schema requests 路径中的 Swashbuckle 参数强制为必需 - Swashbuckle parameters in path forced to required 从 Swagger (Swashbuckle) 隐藏参数 - Hide parameter from Swagger (Swashbuckle) Swashbuckle 中的默认 Swagger UI URL 是什么? - What is the default Swagger UI URL in Swashbuckle? 如何使用 Swashbuckle 省略 WebAPI 上 Swagger 文档中的方法 - How to omit methods from Swagger documentation on WebAPI using Swashbuckle Swashbuckle - 从另一个项目的Model中向Swagger UI添加模型和示例值 - Swashbuckle - Add Model and Example values to Swagger UI from a Model from another project 我可以使用 Swashbuckle 从 Blazor 项目 c# 生成 Swagger UI - Can I use Swashbuckle to generate Swagger UI from Blazor project c# 将身份验证添加到/ swagger / ui / index页面 - Swagger | Web API | Swashbuckle - Add Authentication to /swagger/ui/index page - Swagger | Web API | Swashbuckle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM