Default model example in Swashbuckle (Swagger)

前端 未结 5 1801
再見小時候
再見小時候 2020-12-08 05:22

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?

For example, on the S

5条回答
  •  执笔经年
    2020-12-08 05:59

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

提交回复
热议问题