【问题标题】:How to configure Swashbuckle to ignore property on model for a specific api version only如何将 Swashbuckle 配置为仅忽略特定 api 版本的模型上的属性
【发布时间】:2021-01-07 06:16:07
【问题描述】:

我需要向模型添加一个属性,并且我已经实现了selected answer here 中的建议,它的工作原理是它删除了我用模型的SwaggerIgnorePropertyAttribute 属性标记的属性。 我的问题是,如果我的应用程序有多个 API 版本,如何从某些版本的 swagger doc/schema 中删除它?我只关心不要大摇大摆地看到添加的属性。尽管我有多个版本的应用程序,但我在整个应用程序中实际上只有 1 个版本的模型。

我添加了这样的版本:

            services.AddSwaggerGen(
                swaggerOptions =>
                {
                    swaggerOptions.SwaggerDoc(
                        "v1",
                        new Info
                        {
                            Title = "Titlebla1",
                            Description = "bla1",
                            Version = "v1"
                        });

                    swaggerOptions.SwaggerDoc(
                        "v2",
                        new Info
                        {
                            Title = "Titlebla2",
                            Description = "bla2",
                            Version = "v2"
                        });
 etc

我知道我可以通过使用 SwaggerDocument 并执行以下操作来找到版本:swaggerDoc.Info.Version 但是如何从下面的 SwaggerExcludePropertySchemaFilter 类中的 Apply 访问 SwaggerDocument?

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

                var excludedProperties = context.SystemType.GetProperties().Where(t => t.GetCustomAttribute<SwaggerIgnorePropertyAttribute>() != null);

                foreach (var excludedProperty in excludedProperties)
                {
                    var propertyToRemove = schema.Properties.Keys.SingleOrDefault(x => string.Equals(x, excludedProperty.Name, StringComparison.OrdinalIgnoreCase));

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

            }
        }

【问题讨论】:

    标签: asp.net-core swagger swashbuckle swashbuckle.aspnetcore


    【解决方案1】:

    尝试使用IDocumentFilter,见示例:

     public class CustomSwaggerFilter : IDocumentFilter
    {
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            var nonRequiredMYPropertyAPIs = swaggerDoc.Paths
                .Where(x => !x.Key.ToLower().Contains("v1") /*the version you want to remove the property */)
                .ToList();
            nonRequiredMYPropertyAPIs.ForEach(x => { 
                                                swaggerDoc.Components.Schemas["YOUR_CLASS_MODEL"]
                                                .Properties.Remove("PROPERTY_NAME_YOU_WANT_TO_IGNORE");
                                            });
        }
    }
    

    别忘了注册过滤器:

       c.DocumentFilter<CustomSwaggerFilter>();
    

    【讨论】:

    • 我要删除的定义的只读属性不在路径中。它在定义中。我想这意味着我可以这样做 swaggerDoc.Definitions["YOUR_CLASS_MODEL"].Properties.Remove("PROPERTY_NAME_YOU_WANT_TO_IGNORE") 。对吗?
    猜你喜欢
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多