My Code:
public IEnumerable
But I didn't see the default value in the genereated Swagger file. Am I missing anything?
Same issue here. I guess this is not supported yet.
I decided to use a fix which i found in this discussion https://github.com/domaindrivendev/Swashbuckle/issues/69#issuecomment-53953785. Works well, though it would be nice with implicit support from method parameters.
Previously I used 4.1.0-rc1, it natively supports default value of method parameter. Don't need to use IOperationFilter. But for 5.1.4, seems it is removed.
It doesn't look like it gets set yet, extending the example from #69, here's what I came up with.
It'll use the C# default value defined by the parameter first, otherwise fall back to the [SwaggerDefaultValue(...)] attribute on the method.
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
class SwaggerDefaultValueAttribute : Attribute
{
public SwaggerDefaultValueAttribute(string parameterName, object value)
{
this.ParameterName = parameterName;
this.Value = value;
}
public string ParameterName { get; private set; }
public object Value { get; set; }
}
class AddDefaultValues : IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
if (operation.parameters == null)
return;
var actionParams = apiDescription.ActionDescriptor.GetParameters();
var customAttributes = apiDescription.ActionDescriptor.GetCustomAttributes<SwaggerDefaultValueAttribute>();
foreach (var param in operation.parameters)
{
var actionParam = actionParams.FirstOrDefault(p => p.ParameterName == param.name);
if (actionParam != null)
{
if (actionParam.DefaultValue != null)
{
param.@default = actionParam.DefaultValue;
}
else
{
var customAttribute = customAttributes.FirstOrDefault(p => p.ParameterName == param.name);
if (customAttribute != null)
{
param.@default = customAttribute.Value;
}
}
}
}
}
}
Merging this with #283 (which I've renamed to also incorporate parameter defaults).
As pointed out in that ticket, I'll introduce support for "per-action" and "per-type" filters which will make both cases a little easier
I know this is old, but I'm trying to come up with a solution whereby we can set this default value but not on the Model. I'd rather set it on the method as to not pollute the model.
// pseudo code!
[HttpGet]
[Route("{foo}")]
[SwaggerDefaultValue("foo", "MyFooValue")]
[SwaggerDefaultValue("bar", -1)]
public Task<MySpecialDto> Get(
string foo,
int bar,
string baz){
return //
}
In doing so, we should be able to populate the entry fields on the swagger test view
Parameters
| Parameter | Value | Description | Parameter Type | Data Type |
| --------- | ------------ | ------------ | -------------- | --------- |
| foo | | MyFooValue | | | path | string |
| bar | | -1 | | | query | int |
| baz | | (required) | | | query | string |
In 5.3.0, still has this issue. The query parameters with default values are not propagate to swagger.json, thus the default values are not rendered in UI.
If the parameter is enum type, then it would be better to let the dropdown list auto select the default option.
Most helpful comment
I know this is old, but I'm trying to come up with a solution whereby we can set this default value but not on the Model. I'd rather set it on the method as to not pollute the model.
In doing so, we should be able to populate the entry fields on the swagger test view
Parameters
| Parameter | Value | Description | Parameter Type | Data Type |
| --------- | ------------ | ------------ | -------------- | --------- |
| foo |
| MyFooValue || | path | string || bar |
| -1 || | query | int || baz |
| (required) || | query | string |