忽略 Swagger UI 中的属性

Gay*_*yan 4 c# swagger swagger-ui asp.net-core

我试图忽略 swagger UI 上的属性。根据这篇文章我已经实现了一个过滤器并尝试

[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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 自定义属性似乎无法通过反射正确获取excludedProperties始终为空。
  2. context.MemberInfo确实读取了该属性,但无法从中删除,schema.Properties因为那里没有属性

我的样本模型就像

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; }
}
Run Code Online (Sandbox Code Playgroud)

我在这里缺少什么

问候

Iro*_*ean 8

如果您将该属性标记为internal公共而不是公共,那么它将被 Swashbuckle 忽略。