我正在尝试忽略swagger UI的属性。基于此article,我实现了一个过滤器并尝试了
[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);
}
}
}
excludedProperties
始终为空。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; }
}
我在这里缺少的内容
问候
您实际上不需要为请求模型定义自己的属性。如果您使用的是Json.NET,则使用[JsonIgnore]