如何从SwaggerResponse模型中隐藏/排除属性?不是请求而是响应

bybem2ql  于 8个月前  发布在  其他
关注(0)|答案(1)|浏览(66)

以下是需要从SwaggerResponseModel中排除VersionRange属性的Class:

public class Dependency
{       
    public string Name { get; set; }        

    [JsonConverter(typeof(VersionRangeConverter))]
    [SwaggerExclude]       
    public VersionRange VersionRange { get; set; }
}

我曾尝试使用属性与ISchemaFilter排除属性从SwaggerResponse模型,但它没有工作。

public class SwaggerExcludeFilter : ISchemaFilter
    {
        public void Apply(OpenApiSchema schema, SchemaFilterContext context)
        {
            if (schema?.Properties == null)
            {
                return;
            }

            var properties = context?.Type?.GetProperties().Where(x => x.GetCustomAttribute<SwaggerExcludeAttribute>() != null);

            foreach (PropertyInfo prop in properties ?? Enumerable.Empty<PropertyInfo>())
            {
                if (schema.Properties.ContainsKey(prop.Name))
                {
                    schema.Properties.Remove(prop.Name);
                }
            }
        }
    }

我不想使用受保护的内部访问说明符,因为我希望在序列化和反序列化期间考虑属性。
[System.Text.Json.Serialization.JsonIgnore]属性也没有效果。

xxe27gdn

xxe27gdn1#

我相信,你会在这里和这里找到答案。Brhav为此实现了[SwaggerIgnore]属性。

相关问题