First approach
According to one of the answers here you should be able to simply add the following to your model although I haven't verified this:
public class ExternalJobCreateViewModel
{
///<example>Bob</example>
public string CustomFilename { get; set; }
...etc
Second approach
In .net 6 I used the following:
public class MyFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (operation.OperationId.Equals("somecontroller_somepath", StringComparison.OrdinalIgnoreCase))
{
operation.Parameters.Clear();
operation.Parameters = new List<OpenApiParameter>
{
new OpenApiParameter()
{
Name = "document-name",
Schema = new OpenApiSchema()
{
Type = "string",
},
Example = new Microsoft.OpenApi.Any.OpenApiString("docName1"),
In = ParameterLocation.Query
},
new OpenApiParameter()
{
Name = "user-email",
Schema = new OpenApiSchema()
{
Type = "string",
},
Example = new Microsoft.OpenApi.Any.OpenApiString("elvis.presley@somemail.com"),
In = ParameterLocation.Query
},
new OpenApiParameter()
{
Name = "account-type-id",
Schema = new OpenApiSchema()
{
Type = "string",
},
Example = new Microsoft.OpenApi.Any.OpenApiString("2"),
In = ParameterLocation.Query
}
};
}
}
}
Then in Program.cs
builder.Services.AddSwaggerGen(options =>
{
... other stuf
options.OperationFilter<MyFilter>();
3rd Approach
I never used this code so not tested
In .net 6 and Piggybacking off of Sameh's answer...
To use multiple attributes decorate your attribute class like this:
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class SwaggerDefaultValueAttribute : Attribute
{
... etc
For 6.5.0 of swashbuckle I think it is something like this for properties:
public class AddDefaulValueFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (operation.Parameters == null || !operation.Parameters.Any())
{
return;
}
context.ApiDescription.TryGetMethodInfo(out var x);
if (x != null)
{
return;
}
var attributes = x!.GetCustomAttributes<SwaggerDefaultValueAttribute>().ToList();
if (!attributes.Any())
{
return;
}
foreach (var parameter in operation.Parameters)
{
var attr = attributes.FirstOrDefault(it => it.Parameter == parameter.Name);
if (attr != null)
{
parameter.Schema.Default = new OpenApiString(attr.Value);
}
}
}
}
Since I was trying to use this for body parameters on a multipart message I Had to do something like this but use at your own risk:
public class AddDefaulValueFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if(operation.RequestBody == null)
{
return;
}
var keys = operation.RequestBody.Content.Where(val => val.Key == "multipart/form-data").Take(1).ToList();
if(!keys.Any())
{
return;
}
var props = keys.FirstOrDefault().Value.Schema.Properties;
if (props == null || !props.Any())
{
return;
}
context.ApiDescription.TryGetMethodInfo(out var x);
if (x == null)
{
return;
}
var attributes = x!.GetCustomAttributes<SwaggerDefaultValueAttribute>().ToList();
if (!attributes.Any())
{
return;
}
foreach (var prop in props)
{
var attr = attributes.FirstOrDefault(it => it.Parameter == prop.Key);
if (attr != null)
{
prop.Value.Default = new OpenApiString(attr.Value);
}
}
}
}