如何为Swagger UI定义参数的默认值?

Ang*_*ker 2 .net c# swagger .net-core swashbuckle

我已经将Swagger / Swashbuckle集成到.NET Core 2.2 API项目中。一切都很好,我要问的纯粹是为了方便。考虑以下API方法:

public Model SomeEstimate(SomeRequest request) {
    return Manager.GetSomeEstimate(request);
}
...
public class SomeRequest {
    public string StreetAddress { get; set; }
    public string Zip { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

当我点击/swagger/index.html并尝试使用该API时,我总是必须输入StreetAddress和Zip值。

有没有办法为StreetAddress和Zip提供默认值?该答案建议放置[DefaultValue(“ value here”))属性的SomeRequest类的每个属性。它可能适用于常规.NET,但不适用于.NET Core。

是否可以为Swagger UI提供参数的默认值?

小智 10

最初的功劳归功于Rahul Sharma,但如果有人对 .NET Core 3.0+ 感兴趣,Swashbuckle v5.0.0-rc4 会使SchemaFilter定义变得更加简单。也许有一种方法可以使用新属性或类似的东西添加示例值,但我还没有找到这样的方法。

public class SchemaFilter : ISchemaFilter
{
    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
    {
        if (schema.Properties == null)
        {
            return;
        }

        foreach (var property in schema.Properties)
        {
            if (property.Value.Default != null && property.Value.Example == null)
            {
                property.Value.Example = property.Value.Default;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Rah*_*rma 7

为了为.NET Core中的Swagger UI定义参数的默认值,下面的文章为Model类中的DefaultValue属性定义一个自定义架构过滤器。下面显示的代码摘自本文,纯粹是为了告知其他任何有此问题或遇到类似问题的人:

装饰模型中的所需属性:

public class Test {
    [DefaultValue("Hello")]
    public string Text { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

主过滤器:

using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Project.Swashbuckle {

    public class SchemaFilter : ISchemaFilter {

        public void Apply(Schema schema, SchemaFilterContext context) {
            if (schema.Properties == null) {
                return;
            }

            foreach (PropertyInfo propertyInfo in context.SystemType.GetProperties()) {

                // Look for class attributes that have been decorated with "[DefaultAttribute(...)]".
                DefaultValueAttribute defaultAttribute = propertyInfo
                    .GetCustomAttribute<DefaultValueAttribute>();

                if (defaultAttribute != null) {
                    foreach (KeyValuePair<string, Schema> property in schema.Properties) {

                        // Only assign default value to the proper element.
                        if (ToCamelCase(propertyInfo.Name) == property.Key) {
                            property.Value.Example = defaultAttribute.Value;
                            break;
                        }
                    }
                }
            }
        }

        private string ToCamelCase(string name) {
            return char.ToLowerInvariant(name[0]) + name.Substring(1);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

最后将其注册到您的Swagger选项(在Startup.cs中):

services.AddSwaggerGen(c => {
    // ...
    c.SchemaFilter<SchemaFilter>();
});
Run Code Online (Sandbox Code Playgroud)


xha*_*fan 5

Swashbuckle.AspNetCore 5.6.3 只需要DefaultValueAttribute.