Благодаря Мохсину я решил свою проблему.В следующем примере я создал атрибут SwaggerRequired.Этот атрибут можно разместить на любой модели.Затем AddSwaggerRequiredSchemaFilter обеспечивает изменение документации Swagger.Ниже приведен код, который я написал для этого
Случайная модель:
public class Foo
{
[SwaggerRequired]
public string FooBar{ get; set; }
}
Атрибут SwaggerRequiredAttribute:
[AttributeUsage(AttributeTargets.Property)]
public class SwaggerRequiredAttribute : Attribute
{
}
И фильтр AddSwaggerRequiredSchemaFilter для его работы:
public class AddSwaggerRequiredSchemaFilter : ISchemaFilter
{
public void Apply(Swashbuckle.Swagger.Schema schema, SchemaRegistry schemaRegistry, Type type)
{
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
var attribute = property.GetCustomAttribute(typeof(SwaggerRequiredAttribute));
if (attribute != null)
{
var propertyNameInCamelCasing = char.ToLowerInvariant(property.Name[0]) + property.Name.Substring(1);
if (schema.required == null)
{
schema.required = new List<string>()
{
propertyNameInCamelCasing
};
}
else
{
schema.required.Add(propertyNameInCamelCasing);
}
}
}
}
}