Skip to content

Commit 1a52c16

Browse files
committed
Create SwaggerDefaultValues.cs
added Swagger Default values
1 parent 5e955bf commit 1a52c16

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Swashbuckle.AspNetCore.Swagger;
2+
using Swashbuckle.AspNetCore.SwaggerGen;
3+
using System.Linq;
4+
5+
namespace Supermarket.Swagger
6+
{
7+
public class SwaggerDefaultValues : IOperationFilter
8+
{
9+
/// <summary>
10+
/// Applies the filter to the specified operation using the given context.
11+
/// </summary>
12+
/// <param name="operation">The operation to apply the filter to.</param>
13+
/// <param name="context">The current operation filter context.</param>
14+
public void Apply(Operation operation, OperationFilterContext context)
15+
{
16+
if (operation.Parameters == null)
17+
{
18+
return;
19+
}
20+
foreach (var parameter in operation.Parameters.OfType<NonBodyParameter>())
21+
{
22+
var description = context.ApiDescription.ParameterDescriptions.First(p => p.Name == parameter.Name);
23+
var routeInfo = description.RouteInfo;
24+
25+
if (parameter.Description == null)
26+
{
27+
parameter.Description = description.ModelMetadata?.Description;
28+
}
29+
30+
if (routeInfo == null)
31+
{
32+
continue;
33+
}
34+
35+
if (parameter.Default == null)
36+
{
37+
parameter.Default = routeInfo.DefaultValue;
38+
}
39+
40+
parameter.Required |= !routeInfo.IsOptional;
41+
}
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)