public class UpdateParametersAsRequired : IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry s, ApiDescription a)
{
if (operation.OperationId == "ControllerName_Action")
{
if (operation.Parameters != null)
{
foreach (var parameter in operation.Parameters)
{
if (parameter.Name == "ParameterYouWantToEdit")
{
// You can edit the properties here
parameter.Required = true;
}
}
}
else
{
// Add parameters if doesn't exists any
operation.Parameters = new List<IParameter>();
operation.Parameters.Add(
new Parameter
{
name = "ParameterName",
@in = "body",
@default = "123",
type = "string",
description = "x y z",
required = true
}
);
}
}
}
}
4条答案
按热度按时间8dtrkrch1#
多亏了莫辛,我解决了我的问题。下面是我提出的,我创建了一个名为SwaggerRequired的属性。此属性可以放置在任何模型上。然后,AddSwaggerRequiredSchemaFilter确保修改Swagger文档。下面是我写的代码
随机模型:
字符串
SwaggerRequiredAttribute:
型
和AddSwaggerRequiredSchemaFilter来使其工作:
型
dfuffjeb2#
是的,有可能。添加实现IOperationFilter的自定义类
字符串
干杯!
but5z9lq3#
我知道很晚了。但其他人可能会得到帮助。
我们可以在需要的属性上添加
[JsonRequired]
。此外,添加以下代码以抑制验证错误。字符串
总的来说,它会是这样的:
型
bn31dyow4#
C#11
required
关键字使用C#11
required
关键字,可以使用以下过滤器轻松转换约束:字符串