Advertisement
Guest User

Untitled

a guest
Dec 1st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.03 KB | None | 0 0
  1. using System.Linq;
  2. using Swashbuckle.AspNetCore.Swagger;
  3. using Swashbuckle.AspNetCore.SwaggerGen;
  4.  
  5. namespace Infrastructure.Web.Mvc.Swagger.Filters
  6. {
  7.     public class SwaggerDefaultValues : IOperationFilter
  8.     {
  9.         public void Apply(Operation operation, OperationFilterContext context)
  10.         {
  11.             if (operation.Parameters == null) return;
  12.  
  13.             foreach (var parameter in operation.Parameters.OfType<NonBodyParameter>())
  14.             {
  15.                 var description = context.ApiDescription
  16.                     .ParameterDescriptions
  17.                     .First(p => p.Name == parameter.Name);
  18.                 var routeInfo = description.RouteInfo;
  19.  
  20.                 if (parameter.Description == null) parameter.Description = description.ModelMetadata?.Description;
  21.  
  22.                 if (routeInfo == null) continue;
  23.  
  24.                 if (parameter.Default == null) parameter.Default = routeInfo.DefaultValue;
  25.  
  26.                 parameter.Required |= !routeInfo.IsOptional;
  27.             }
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement