如何为 Swagger UI 定义参数的默认值? [英] How to define default values for parameters for the Swagger UI?

查看:161
本文介绍了如何为 Swagger UI 定义参数的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have Swagger/Swashbuckle integrated into a .NET Core 2.2 API project. Everything works great and what I am asking is purely for convenience. Consider the following API method:

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

当我点击/swagger/index.html 并想试用这个 API 时,我总是必须输入 StreetAddress 和 Zip 值.

When I hit /swagger/index.html and want to try out this API, I always have to enter the StreetAddress and Zip values.

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

Is there a way to provide default values for StreetAddress and Zip? This answer suggested placing [DefaultValue("value here")] attribute each property of the SomeRequest class. It may have worked for the regular .NET, but not with .NET Core.

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

Is it possible to provide default values for parameters for the Swagger UI?

推荐答案

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

To define default values for parameters for Swagger UI in .NET Core, the following article defines a custom schema filter for your DefaultValue attribute in your Model class. The code shown below has been taken from this article and is purely to inform anyone else who has this question or has been facing a similar problem:

在模型中装饰所需的属性:

Decorate the desired properties in a model:

public class Test {
    [DefaultValue("Hello")]
    public string Text { get; set; }
}

主过滤器:

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

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

And finally registering it to your Swagger Options(In Startup.cs):

services.AddSwaggerGen(c => {
    // ...
    c.SchemaFilter<SchemaFilter>();
});

这篇关于如何为 Swagger UI 定义参数的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆