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

时间:2018-11-15 12:31:45

标签: c# asp.net swagger

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

这就是为什么我想知道是否可以在不使用ASP.NET的Required属性的情况下将属性标记为强制性的原因。但是,草率的文档表明它是强制性的。这可能吗?

3 个答案:

答案 0 :(得分:2)

是的,有可能。添加实现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
                    }
                );
            }
        }
    }
}

干杯!

答案 1 :(得分:0)

多亏莫辛,我解决了我的问题。在提出以下代码之后,我创建了一个名为SwaggerRequired的属性。该属性可以放置在任何模型上。然后,AddSwaggerRequiredSchemaFilter确保Swagger文档被修改。 参见下面我为此编写的代码

随机模型:

public class Foo
{
    [SwaggerRequired]
    public string FooBar{ get; set; }
}

SwaggerRequiredAttribute:

[AttributeUsage(AttributeTargets.Property)] 
public class SwaggerRequiredAttribute : Attribute
{
}

和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);
                }
            }
        }
    }
}

答案 2 :(得分:0)

我知道太晚了。但是其他人可能会得到帮助。

我们可以在所需的属性上添加 data = [ { 'server': 'Network 1', 'data': [ { download: 12, dateStart: "2020-09-20 05:40:01" }, { download: 0, dateStart: "2020-09-20 05:50:01" }, { download: 0, dateStart: "2020-09-20 06:00:01" }, { download: 0, dateStart: "2020-09-20 06:10:01" } ] }, { 'server': 'Network 1', 'data': [ { download: 12, dateStart: "2020-09-20 05:40:01" }, { download: 12, dateStart: "2020-09-20 05:50:01" }, { download: 12, dateStart: "2020-09-20 06:00:01" }, { download: 12, dateStart: "2020-09-20 06:10:01" } ] } ] 。此外,添加以下代码以消除验证错误。

[JsonRequired]

总的来说,就像这样:

    [OnError]
    internal void OnError(StreamingContext context, ErrorContext errorContext)
    {
        errorContext.Handled = true;
    }