Swashbuckle (Swagger) 中的默认模型示例 [英] Default model example in Swashbuckle (Swagger)

查看:42
本文介绍了Swashbuckle (Swagger) 中的默认模型示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行 ASP WebAPI 2 并成功安装了 Swashbuckle.我想弄清楚如何定义默认架构值?

I'm running ASP WebAPI 2 and successfully installed Swashbuckle. I am trying to figure out how one defines what the default schema values are?

例如,在 Swagger 现场演示站点上,他们将 pet 的默认值更改为doggie".他们还定义了状态的允许值.(现场演示)

For example, on the Swagger live demo site they changed the default value of pet to "doggie". They also defined the allowable values for status. (Live Demo)

推荐答案

嗯,vgaspar.trivix 的代码对我来说没有完全工作,没有为架构设置默认值.我也得到了一个 NullPointerException.我设法通过编辑 Apply 方法使其按预期工作,并像这样操作 schemaRegistry:

Well the code of vgaspar.trivix did not work completly for me, the default values did not get set for the schema. Also i got an NullPointerException. I managed to get it working as intended by editing the Apply method and manipulated the schemaRegistry like this:

public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
    if (operation.parameters == null)
        return;
    IDictionary<string, object> parameterValuePairs =
    GetParameterValuePairs(apiDescription.ActionDescriptor);

    foreach (var param in operation.parameters)
    {
        if (param.schema != null && param.schema.@ref != null)
        {
            string schemaName = param.schema.@ref.Split('/').LastOrDefault();
            if (schemaRegistry.Definitions.ContainsKey(schemaName))
                foreach (var props in schemaRegistry.Definitions[schemaName].properties)
                {
                    if (parameterValuePairs.ContainsKey(props.Key))
                        props.Value.@default = parameterValuePairs[props.Key];
                }
        }
        var parameterValuePair = parameterValuePairs.FirstOrDefault(p => p.Key.IndexOf(param.name, StringComparison.InvariantCultureIgnoreCase) >= 0);
        param.@default = parameterValuePair.Value;
    }
}

这篇关于Swashbuckle (Swagger) 中的默认模型示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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