public class DocumentOperationRoleRequirementsFilters : IOperationFilter
{
public void Apply( Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription )
{
//Get AuthorizeAttribute Roles
var filterPipeline = apiDescription.ActionDescriptor.GetFilterPipeline().Where( f => f.Scope == FilterScope.Action );
var roles = filterPipeline.Select(f => f.Instance).OfType<AuthorizeAttribute>().Select(a => a.Roles).FirstOrDefault();
//Get the operation description (<Remarks> tag)
var originalDescription = operation.description;
var roleRequirementBuilder = new StringBuilder();
if ( !string.IsNullOrEmpty(roles) )
{
roleRequirementBuilder.Append( "**Roles Required:** " );
foreach ( var role in roles.Split(',') )
{
roleRequirementBuilder.Append( $"<code>{role.ToString()}</code>, " );
}
roleRequirementBuilder.Remove( roleRequirementBuilder.Length - 2, 2 );
roleRequirementBuilder.Append( "<br/>" );
//Readd original operation description
roleRequirementBuilder.AppendLine( originalDescription );
Set newly created description as operation description
operation.description = roleRequirementBuilder.ToString();
}
}
}