'how can i use Description attribute work with swashbuckle
I am using the latest version of swashbuckle for .net core and I want swashbuckle to repspect the property Description so that for all parameters the description will be used for the swagger file
public class dtoClass
{
[System.ComponentModel.Description("desciption swagger Name")]
public string name {get; set; }
}
Is there an extention point/ filter in swashbuckle to be used?
my current soltion is to add the SwaggerSchema attribute.
public class dtoClass
{
[System.ComponentModel.Description("desciption swagger Name")]
[Swashbuckle.AspNetCore.Annotations("desciption swagger Name")]
public string name {get; set; }
}
Solution 1:[1]
You can use schema filter
builder.Services.AddSwaggerGen(c =>
{
c.SchemaFilter<DescriptionSchemaFilter>();
});
Where DescriptionSchemaFilter
is
internal class DescriptionSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (context.ParameterInfo != null)
{
var descriptionAttributes = context.ParameterInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (descriptionAttributes.Length > 0)
{
var descriptionAttribute = (DescriptionAttribute)descriptionAttributes[0];
schema.Description = descriptionAttribute.Description;
}
}
if (context.MemberInfo != null)
{
var descriptionAttributes = context.MemberInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (descriptionAttributes.Length > 0)
{
var descriptionAttribute = (DescriptionAttribute)descriptionAttributes[0];
schema.Description = descriptionAttribute.Description;
}
}
if (context.Type != null)
{
var descriptionAttributes = context.Type.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (descriptionAttributes.Length > 0)
{
var descriptionAttribute = (DescriptionAttribute)descriptionAttributes[0];
schema.Description = descriptionAttribute.Description;
}
}
}
}
And code:
in swagger look's like:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mike Dev |