Reputation: 27360
How do I define default value for property in swagger generated from following API?
public class SearchQuery
{
public string OrderBy { get; set; }
[DefaultValue(OrderDirection.Descending)]
public OrderDirection OrderDirection { get; set; } = OrderDirection.Descending;
}
public IActionResult SearchPendingCases(SearchQuery queryInput);
Swashbuckle generates OrderDirection as required parameter. I would like it to be optional and indicate to client the default value (not sure if swagger supports this).
I don't like making the property type nullable. Is there any other option? Ideally using built in classes...
I use Swashbuckle.AspNetCore - https://learn.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs=visual-studio
Upvotes: 34
Views: 80766
Reputation: 11753
This is how I do it (using .Net Core 7.0, would probably work for older version):
Controller declaration (example):
[HttpGet("DynValuesCycleGensetPerMicrogridAndDate")]
public async Task<ActionResult<DynamicValuesByMicrogridDto>> DynValuesCycleGensetPerMicrogridAndDate(
[SwaggerTryItOutDefaulValue("1")] int microgridId,
[SwaggerTryItOutDefaulValue("2023-10-07 15:00")] DateTimeOffset dateTimeStart,
[SwaggerTryItOutDefaulValue("2023-10-09 15:00")] DateTimeOffset dateTimeEnd)
{
//...
Program initialisation:
builder.Services.AddSwaggerGen(options =>
{
options.SchemaFilter<SwaggerTryItOutDefaulValue>();
//...
ISchemaFilter:
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Reflection;
namespace GeneralAsp
{
public class SwaggerTryItOutDefaulValue : ISchemaFilter
{
/// <summary>
/// Apply is called for each parameter
/// </summary>
/// <param name="schema"></param>
/// <param name="context"></param>
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (context.ParameterInfo != null)
{
var att = context.ParameterInfo.GetCustomAttribute<SwaggerTryItOutDefaulValueAttribute>();
if ((att != null))
{
schema.Example = new Microsoft.OpenApi.Any.OpenApiString(att.Value.ToString());
}
}
}
}
}
Attribute:
namespace GeneralAsp
{
public class SwaggerTryItOutDefaulValueAttribute : Attribute
{
public string Value { get; }
public SwaggerTryItOutDefaulValueAttribute(string value)
{
Value = value;
}
}
}
Upvotes: 4
Reputation: 16453
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
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("[email protected]"),
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>();
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);
}
}
}
}
Upvotes: 2
Reputation: 1418
This is applicable for ASP.net MVC5, code is not valid for .Net Core
1- Define a custom attribute as following
public class SwaggerDefaultValueAttribute: Attribute
{
public SwaggerDefaultValueAttribute(string param, string value)
{
Parameter = param;
Value = value;
}
public string Parameter {get; set;}
public string Value {get; set;}
}
2- Define a Swagger OperationFilter class
public class AddDefaulValue: IOperationFilter
{
void IOperationFilter.Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
if (operation.parameters == null || !operation.parameters.Any())
{
return;
}
var attributes = apiDescription.GetControllerAndActionAttributes<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.@default = attr.Value;
}
}
}
}
3- Register the OperationFilter at SwaggerConfig file
c.OperationFilter<AddDefaultValue>();
4- Decorate your controller method with attributes
[SwaggerDefaultValue("param1", "AnyValue")]
public HttpResponseMessage DoSomething(string param1)
{
return Request.CreateResponse(HttpStatusCode.OK);
}
Upvotes: 3
Reputation: 597
Setting the default parameter value works like this if you can do it in your controller
// GET api/products
[HttpGet]
public IEnumerable<Product> Get(int count = 50)
{
Conn mySqlGet = new Conn(_connstring);
return mySqlGet.ProductList(count);
}
Upvotes: 8
Reputation: 17664
I've always set the default on the param itself like this:
public class TestPostController : ApiController
{
public decimal Get(decimal x = 989898989898989898, decimal y = 1)
{
return x * y;
}
}
Here is how that looks like on the swagger-ui:
http://swashbuckletest.azurewebsites.net/swagger/ui/index#/TestPost/TestPost_Get
Following the discussion on the comments I updated Swagger-Net to read the DefaultValueAttribute
via reflection
Here is the sample class I'm using:
public class MyTest
{
[MaxLength(250)]
[DefaultValue("HelloWorld")]
public string Name { get; set; }
public bool IsPassing { get; set; }
}
and here is how the swagger json looks like:
"MyTest": {
"type": "object",
"properties": {
"Name": {
"default": "HelloWorld",
"maxLength": 250,
"type": "string"
},
"IsPassing": {
"type": "boolean"
}
},
"xml": {
"name": "MyTest"
}
},
The Source code of Swagger-Net is here:
https://github.com/heldersepu/Swagger-Net
And the source code for the test project is here:
https://github.com/heldersepu/SwashbuckleTest
Upvotes: 30
Reputation: 9463
In the YAML file, you can define which properties should be required. This example is from a NSwag configuration.
/SearchPendingCases:
post:
summary: Search pending cases
description: Searches for pending cases and orders them
parameters:
- in: body
name: SearchQuery
required: true
schema:
type: object
required:
- OrderBy
# do not include OrderDirection here because it is optional
properties:
OrderBy:
description: Name of property for ordering
type: string
# you can remove the following two lines
# if you do not want to check the string length
minLength: 1
maxLength: 100
OrderDirection:
description: Optional order direction, default value: Descending
type: string
enum: [Ascending, Descending] # valid enum values
default: Descending # default value
Swagger - Unlocking the Spec: The default keyword
Upvotes: -3