Default model example in Swashbuckle (Swagger)

前端 未结 5 1808
再見小時候
再見小時候 2020-12-08 05:22

I\'m running ASP WebAPI 2 and successfully installed Swashbuckle. I am trying to figure out how one defines what the default schema values are?

For example, on the S

5条回答
  •  猫巷女王i
    2020-12-08 05:53

    I managed to get this working by following what's on this link:

    https://github.com/domaindrivendev/Swashbuckle/issues/69#issuecomment-53953785

    In short this is what needs to be done:

    1. Create the classes SwaggerDefaultValue and AddDefaultValues as described in the link. Some changes that I did:

      public class SwaggerDefaultValue : Attribute
      {
          public string Name { get; set; }
          public string Value { get; set; }
      
          public SwaggerDefaultValue(string value)
          {
              this.Value = value;
          }
      
          public SwaggerDefaultValue(string name, string value) : this(value)
          {
              this.Name = name;
          }
      }
      
      public class AddDefaultValues : IOperationFilter
      {
          public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
          {
              IDictionary parameterValuePairs =
              GetParameterValuePairs(apiDescription.ActionDescriptor);
      
              foreach (var param in operation.parameters)
              {
                  var parameterValuePair = parameterValuePairs.FirstOrDefault(p => p.Key.IndexOf(param.name, StringComparison.InvariantCultureIgnoreCase) >= 0);
                  param.@default = parameterValuePair.Value;
              }
          }
      
          private IDictionary GetParameterValuePairs(HttpActionDescriptor actionDescriptor)
          {
              IDictionary parameterValuePairs = new Dictionary();
      
              foreach (SwaggerDefaultValue defaultValue in actionDescriptor.GetCustomAttributes())
              {
                  parameterValuePairs.Add(defaultValue.Name, defaultValue.Value);
              }
      
              foreach (var parameter in actionDescriptor.GetParameters())
              {
                  if (!parameter.ParameterType.IsPrimitive)
                  {
                      foreach (PropertyInfo property in parameter.ParameterType.GetProperties())
                      {
                          var defaultValue = GetDefaultValue(property);
      
                          if (defaultValue != null)
                          {
                               parameterValuePairs.Add(property.Name, defaultValue);
                          }
                      }
                  }
              }
      
              return parameterValuePairs;
          }
      
          private static object GetDefaultValue(PropertyInfo property)
          {
              var customAttribute = property.GetCustomAttributes().FirstOrDefault();
      
              if (customAttribute != null)
              {
                  return customAttribute.Value;
              }
      
              return null;
          }
      }
      
      1. Edit your SwaggerConfig and add the AddDefaultValues class to the OperationFilters:

        GlobalConfiguration.Configuration
            .EnableSwagger(c => {
                  ...
                  c.OperationFilter()
                  ...
             });
        
      2. Now for the parameters I want default values I just add the following:

        public IHttpActionResult Put([FromBody]Pet pet)
        {
           ...
           return Ok();
        }
        
        public class Pet {
            [SwaggerDefaultValue("doggie")]
            public string Name { get; set; }
        
            [SwaggerDefaultValue("available")]
            public string Status;
        
            ...
        }
        

提交回复
热议问题