csharp Swashbuckle(Swagger)中的默认模型示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Swashbuckle(Swagger)中的默认模型示例相关的知识,希望对你有一定的参考价值。

/// <summary>
/// cf. https://github.com/domaindrivendev/Swashbuckle/issues/69
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class SwaggerDefaultValueAttribute : Attribute
{
    public string ParameterName { get; set; }

    public object Value { get; set; }

    public SwaggerDefaultValueAttribute(string paramName, string value)
    {
        this.ParameterName = paramName;
        this.Value = value;
    }

    public SwaggerDefaultValueAttribute(string paramName, int value)
    {
        this.ParameterName = paramName;
        this.Value = value;
    }
}

public class SwaggerDefaultValueFilter : IOperationFilter
{
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        foreach (var param in operation.parameters)
        {
            var actionParam = apiDescription.ActionDescriptor
                .GetParameters()
                .FirstOrDefault(p => p.ParameterName == param.name);

            if (actionParam != null)
            {
                var customAttribute = actionParam.ActionDescriptor
                    .GetCustomAttributes<SwaggerDefaultValueAttribute>()
                    .FirstOrDefault(o => o.ParameterName == actionParam.ParameterName);

                if (customAttribute != null)
                {
                    param.@default = customAttribute.Value;
                }
            }
        }
    }
}
/// <summary>
/// swagger에서 API 호출에 필요한 매개변수(모델) 기본값을 생성해서 보여주는 필터 클래스.
/// </summary>
public class SwaggerSchemaExample : ISchemaFilter
{
    public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
    {
        object example = null;

        if (type == typeof(Product))
        {
            example = new Product
            {
                Id = "Abc",
                Name = "GoGo"
            };
        }

        schema.example = ToCamelCaseObject(example); // 객체 그대로 넣으면 PascalCase로 보이는 문제가 있음.
    }

    public static object ToCamelCaseObject(object obj)
    {
        var json = JsonConvert.SerializeObject(obj, new JsonSerializerSettings()
        {
            ContractResolver = new CamelCasePropertyNamesContractResolver(),
            NullValueHandling = NullValueHandling.Ignore
        });

        return JsonConvert.DeserializeObject(json);
    }
}
public class SwaggerConfig
{
    public static void Register()
    {
        var thisAssembly = typeof(SwaggerConfig).Assembly;

        GlobalConfiguration.Configuration
            .EnableSwagger(c =>
                {
                    c.SchemaFilter<SwaggerSchemaExample>();
                    c.OperationFilter<SwaggerDefaultValueFilter>();
                });
    }
}

以上是关于csharp Swashbuckle(Swagger)中的默认模型示例的主要内容,如果未能解决你的问题,请参考以下文章

csharp SwashBuckle配置

csharp Swashbuckle(Swagger)中的默认模型示例

csharp 在Swashbuckle Swagger中,此片段允许按字母顺序显示操作。

ABP/Swashbuckle - 使用 Swashbuckle CLI 生成 swagger 文档

ASP.NET 核心:NSwag 与 Swashbuckle

Swashbuckle 无法正确显示 API