SHARE
TWEET

Untitled

a guest Jun 25th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public void Apply(Schema schema, SchemaFilterContext context)
  2. {
  3.     if (context.SystemType.IsEnum)
  4.     {
  5.         var names = Enum.GetNames(context.SystemType);
  6.         var values = Enum.GetValues(context.SystemType);
  7.         var desc = "";
  8.  
  9.         foreach (var value in values)
  10.         {
  11.             var intValue = Convert.ChangeType(value, Enum.GetUnderlyingType(value.GetType()));
  12.             desc += $"{intValue}={value},";
  13.         }
  14.         desc = desc.TrimEnd(',');
  15.         schema.Extensions.Add("x-enumNames", names);
  16.         schema.Extensions["description"] = desc;
  17.     }
  18. }
  19.      
  20. resolution: {
  21.     format: "int32",
  22.     enum: [
  23.         1,
  24.         2,
  25.         3,
  26.         4
  27.     ],
  28.     type: "integer",
  29.     x-enumNames: [
  30.         "Hour",
  31.         "Day",
  32.         "Month",
  33.         "Year"
  34.     ],
  35.     description: "1=Hour,2=Day,3=Month,4=Year"
  36. }
  37.      
  38. public async Task<ActionResult<ReturnType>> GetData(Models.ResolutionEnum resolution)
  39.      
  40. {
  41.     name: "resolution",
  42.     in: "query",
  43.     required: true,
  44.     type: "integer",
  45.     format: "int32",
  46.     enum: [
  47.         1,
  48.         2,
  49.         3,
  50.         4
  51.     ]
  52. }
  53.      
  54. public class InjectXStuff : IDocumentFilter
  55. {
  56.     public void Apply(SwaggerDocument s, DocumentFilterContext c)
  57.     {
  58.         PathItem path = s.Paths.Where(x => x.Key.Contains("Values")).First().Value;
  59.         path.Post.Parameters.FirstOrDefault().Extensions.Add("x-stuff", "123456");
  60.     }
  61. }
  62.      
  63. public class ModifyParametersFilter : IParameterFilter
  64. {
  65.     public void Apply(IParameter parameter, ParameterFilterContext context)
  66.     {
  67.         var type = context.ParameterInfo?.ParameterType;
  68.         if (type == null)
  69.             return;
  70.         if (type.IsEnum)
  71.         {
  72.             var names = Enum.GetNames(type);
  73.             var values = Enum.GetValues(type);
  74.             var desc = "";
  75.  
  76.             foreach (var value in values)
  77.             {
  78.                 var intValue = Convert.ChangeType(value, Enum.GetUnderlyingType(value.GetType()));
  79.                 desc += $"{intValue}={value},";
  80.             }
  81.             desc = desc.TrimEnd(',');
  82.             if (!parameter.Extensions.ContainsKey("x-enumNames"))
  83.                 parameter.Extensions.Add("x-enumNames", names);
  84.         }
  85.     }
  86. }
  87.      
  88. services.AddSwaggerGen(c =>
  89. {
  90.     ..
  91.     c.ParameterFilter<ModifyParametersFilter>();
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top