如何在没有 ASP.NET 模型验证的情况下在 Swagger 中根据需要标记属性?

Und*_*ade 5 c# asp.net swagger

我正在创建一个使用多个私有 API 的公共 API(无法从外部访问)。已经为私有 API 编写了业务验证,我不想为公共 API 重新编写它们。但我确实希望 swagger 文档是相同的。

这就是为什么我想知道是否可以将属性标记为强制属性,而不使用 ASP.NET 的 Required 属性。但是 swagger 文档表明它是强制性的。这可能吗?

Moh*_*sin 5

是的,这是可能的。添加实现 IOperationFilter 的自定义类

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
                    }
                );
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

干杯!


Und*_*ade 5

感谢 Mohsin,我解决了我的问题。我想出了以下内容,我创建了一个名为 SwaggerRequired 的属性。此属性可以放置在任何模型上。然后 AddSwaggerRequiredSchemaFilter 确保修改 Swagger 文档。请参阅下面我为此编写的代码

随机模型:

public class Foo
{
    [SwaggerRequired]
    public string FooBar{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)

SwaggerRequiredAttribute:

[AttributeUsage(AttributeTargets.Property)] 
public class SwaggerRequiredAttribute : Attribute
{
}
Run Code Online (Sandbox Code Playgroud)

和 AddSwaggerRequiredSchemaFilter 让它工作:

public class AddSwaggerRequiredSchemaFilter : ISchemaFilter
{
    public void Apply(Swashbuckle.Swagger.Schema schema, SchemaRegistry schemaRegistry, Type type)
    {
        PropertyInfo[] properties = type.GetProperties();
        foreach (PropertyInfo property in properties)
        {
            var attribute = property.GetCustomAttribute(typeof(SwaggerRequiredAttribute));

            if (attribute != null)
            {
                var propertyNameInCamelCasing = char.ToLowerInvariant(property.Name[0]) + property.Name.Substring(1);

                if (schema.required == null)
                {
                    schema.required = new List<string>()
                    {
                        propertyNameInCamelCasing
                    };
                }
                else
                {
                    schema.required.Add(propertyNameInCamelCasing);
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)