I am using Swashbuckle for APIDocumentation . I created a custom attribute by specifying Link (DefaultValue attribute) and one attribute (swaggerIgnore) . DefaultValue works fine, but another user attribute finds node and gives me the property that I set, but I edit the scheme in it, and the property that I manually delete, any property in it cannot update and show me all the samples in the user interface.
Here is my sample code.
public class SwaggerIgnore : Attribute
{
public string Name { get; set; }
}
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
if (operation.parameters == null)
return;
IDictionary<string, object> parameterValuePairs = GetParameterValuePairs(apiDescription.ActionDescriptor);
var retVal = GetParameters(apiDescription.ActionDescriptor);
foreach (var param in operation.parameters)
{
if (param.schema != null && param.schema.@ref != null)
{
string schemaName = param.schema.@ref.Split('/').LastOrDefault();
if (schemaRegistry.Definitions.ContainsKey(schemaName))
foreach (var props in schemaRegistry.Definitions[schemaName].properties)
{
if (parameterValuePairs.ContainsKey(props.Key))
props.Value.@default = parameterValuePairs[props.Key];
}
Code removal starts here ...
foreach (var item in schemaRegistry.Definitions[schemaName].properties)
{
if (retVal != null && item.Key == retVal)
{
schemaRegistry.Definitions[schemaName].properties.Remove(item);
break;
}
}
Code removal ends here.
}
var parameterValuePair = parameterValuePairs.FirstOrDefault(p => p.Key.IndexOf(param.name, StringComparison.InvariantCultureIgnoreCase) >= 0);
param.@default = parameterValuePair.Value;
}
}
private IDictionary<string, object> GetParameterValuePairs(HttpActionDescriptor actionDescriptor)
{
IDictionary<string, object> parameterValuePairs = new Dictionary<string, object>();
foreach (SwaggerDefaultValue defaultValue in actionDescriptor.GetCustomAttributes<SwaggerDefaultValue>())
{
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<SwaggerDefaultValue>().FirstOrDefault();
if (customAttribute != null)
{
return customAttribute.Value;
}
return null;
}
Using a custom attribute.
[SwaggerDefaultValue("uzair")]
public String StudentName { set; get; }
[SwaggerIgnore]
public String FatherName { set; get; }