【问题标题】:Hide Model Property from being documented in Swagger在 Swagger 中隐藏模型属性
【发布时间】:2019-12-01 22:10:11
【问题描述】:

我有一些属性,我不想在 swagger UI 中公开,但如果它们被传递给控制器​​,我仍然允许它们被反序列化。 Json ignore 不起作用,因为它仍然出现在 swagger 中,并且不允许对属性进行序列化和反序列化。

我创建了一个排除属性,并将其应用于属性,创建了一个模式过滤器并将其添加到启动中。但该属性仍然大摇大摆地出现。

[AttributeUsage(AttributeTargets.Property)]
public class SwaggerExcludeAttribute : Attribute
{
}

添加到属性:

 [SwaggerExcludeAttribute]
 public int? ContentType { get; set; }

过滤器:

 public class SwaggerExcludeSchemaFilter : ISchemaFilter
 {
    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
    {
        if (schema?.Properties.Count == 0)
        {
            return;
        }
        const BindingFlags bindingFlags = BindingFlags.Public |
                                     BindingFlags.NonPublic |
                                     BindingFlags.Instance;

        var memberList = context.Type
                       .GetFields(bindingFlags).Cast<MemberInfo>()
                       .Concat(context.Type
                       .GetProperties(bindingFlags));
        var excludedList = memberList.Where(m =>
                                       m.GetCustomAttribute<SwaggerExcludeAttribute>()
                                       != null)
                                 .Select(m =>
                                     (m.GetCustomAttribute<JsonPropertyAttribute>()
                                      ?.PropertyName
                                      ?? m.Name.ToCamelCase()));

        foreach (var excludedName in excludedList)
        {
            if (schema.Properties.ContainsKey(excludedName))
                schema.Properties.Remove(excludedName);
        }
        var excludedProperties = context.Type.GetProperties().Where(
            t => t.GetCustomAttribute<SwaggerExcludeAttribute>() != null);

        foreach (var excludedProperty in excludedProperties)
        {
            var propertyToRemove =
                schema.Properties.Keys.SingleOrDefault(
                    x => x.ToLower() == excludedProperty.Name.ToLower());

            if (propertyToRemove != null)
            {
                schema.Properties.Remove(propertyToRemove);
            }
        }
    }

}

在启动中:

services.AddSwaggerGen(c => { c.SchemaFilter<SwaggerExcludeSchemaFilter>(); });

内容类型仍然出现在 swagger 文档中。有没有人有其他建议或方法来实现这一点?

【问题讨论】:

    标签: c# asp.net-core swagger swashbuckle


    【解决方案1】:

    使用最新版本:5.4.1

      public void Apply(OpenApiSchema schema, SchemaFilterContext context)
            {
                if (schema?.Properties == null)
                {
                    return;
                }
    
                var excludedProperties = context.Type.GetProperties().Where(t => t.GetCustomAttribute<SwaggerExcludeAttribute>() != null);
    
                foreach (var excludedProperty in excludedProperties)
                {
                    var propertyToRemove = schema.Properties.Keys.SingleOrDefault(x => x.ToLower() == excludedProperty.Name.ToLower());
    
                    if (propertyToRemove != null)
                    {
                        schema.Properties.Remove(propertyToRemove);
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-15
      相关资源
      最近更新 更多