I'm trying to ignore property on swagger UI. based on this article I have implemented a Filter and tried
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class SwaggerExcludeAttribute : Attribute
{
}
public class SwaggerExcludeFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (schema?.Properties == null || context == null) return;
var excludedProperties = context.Type.GetProperties()
.Where(t => t.GetCustomAttribute(typeof(SwaggerExcludeAttribute), true) != null);
foreach (var excludedProperty in excludedProperties)
{
if (schema.Properties.ContainsKey(excludedProperty.Name))
schema.Properties.Remove(excludedProperty.Name);
}
}
}
- custom attribute seems not properly getting by reflection
excludedPropertiesalways empty. context.MemberInfodoes read the property but cannot remove fromschema.Propertiesbecause no properties there
my sample model is like
public class SequenceSetupListModel
{
public int Id { get; set; }
public int Sequence { get; set; }
public string Role { get; set; }
public string User { get; set; }
[SwaggerExclude]
public IList<Sequence> SequenceLists { get; set; }
}
What I'm missing here
Regards