忽略 属性 来自 Swagger UI

Ignore Property From Swagger UI

我试图大摇大摆地忽略 属性 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);
        }
    }
}
  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; }
}

我在这里缺少什么

此致

您实际上不需要为请求模型定义自己的属性。如果您使用 Json.NET 则使用 [JsonIgnore]

对于所有想知道为什么 [JsonIgnore] 不起作用的人,这是因为您不能在 .net 核心 3.x 上使用 Newtonsoft 库版本。您需要使用 System.Text.Json nuget 包。 https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/README.md#systemtextjson-stj-vs-newtonsoft

AspNetCore 的 NSwag.Annotations 命名空间中有 [OpenApiIgnore] 属性。它似乎适用于 class 或方法,并将它们从生成的 Swagger 文档中排除。您需要包含来自 NuGet 的 NSwag.AspNetCore 包。

[JsonIgnore] 如果模型绑定的方式是[FromBody],将忽略属性 它不适用于 [FromQuery][FromForm] 使用此过滤器以所有绑定方式忽略属性

 public class IgnorePropertyFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        if (context.ApiDescription == null || operation.Parameters == null)
            return;

        if (!context.ApiDescription.ParameterDescriptions.Any())
            return;

        
        context.ApiDescription.ParameterDescriptions.Where(p => p.Source.Equals(BindingSource.Form)
                    && p.CustomAttributes().Any(p => p.GetType().Equals(typeof(JsonIgnoreAttribute))))
            .ForAll(p => operation.RequestBody.Content.Values.Single(v => v.Schema.Properties.Remove(p.Name)));



        context.ApiDescription.ParameterDescriptions.Where(p => p.Source.Equals(BindingSource.Query)
                      && p.CustomAttributes().Any(p => p.GetType().Equals(typeof(JsonIgnoreAttribute))))
            .ForAll(p => operation.Parameters.Remove(operation.Parameters.Single(w => w.Name.Equals(p.Name))));


        #region OldCode

        //var excludedProperties = context.ApiDescription.ParameterDescriptions.Where(p =>
        //    p.Source.Equals(BindingSource.Form));

        //if (excludedProperties.Any())
        //{

        //    foreach (var excludedPropertie in excludedProperties)
        //    {
        //        foreach (var customAttribute in excludedPropertie.CustomAttributes())
        //        {
        //            if (customAttribute.GetType() == typeof(JsonIgnoreAttribute))
        //            {
        //                for (int i = 0; i < operation.RequestBody.Content.Values.Count; i++)
        //                {
        //                    for (int j = 0; j < operation.RequestBody.Content.Values.ElementAt(i).Encoding.Count; j++)
        //                    {
        //                        if (operation.RequestBody.Content.Values.ElementAt(i).Encoding.ElementAt(j).Key ==
        //                            excludedPropertie.Name)
        //                        {
        //                            operation.RequestBody.Content.Values.ElementAt(i).Encoding
        //                                .Remove(operation.RequestBody.Content.Values.ElementAt(i).Encoding
        //                                    .ElementAt(j));
        //                            operation.RequestBody.Content.Values.ElementAt(i).Schema.Properties.Remove(excludedPropertie.Name);


        //                        }
        //                    }
        //                }

        //            }
        //        }
        //    }

        //}


        #endregion
    }
}

正在启动

services.AddSwaggerGen(options =>
{
    options.OperationFilter<IgnorePropertyFilter>();
});

现在 [JsonIgnore] 将适用于所有模型绑定

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