如何配置Swashbuckle仅忽略特定api版本的模型属性 [英] How to configure Swashbuckle to ignore property on model for a specific api version only

查看:41
本文介绍了如何配置Swashbuckle仅忽略特定api版本的模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要向模型中添加属性,并且已经实现了此处选择的答案中的建议,可以删除我用模型的 SwaggerIgnorePropertyAttribute 属性标记的属性.我的问题是,如果我的应用程序有多个API版本,如何从特定版本的swagger doc/schema中删除它?我只在乎不要大惊小怪地看到添加的属性.即使我有多个版本的应用程序,我在整个应用程序中实际上只有一个版本的模型.

I needed to add a property to a model and I have implemented what is suggested in the selected answer here and it's working in that it removes the property I have tagged with SwaggerIgnorePropertyAttribute attribute of the model. My question is , if I have several API versions of my application,how to remove it from the swagger doc/schema for certain versions? I only care not to see the added property in swagger. I only really have 1 version of the model across the application even though I have several version of the app.

我添加了这样的版本:

            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的访问权限?/p>

I know that I can find the version by using SwaggerDocument and doing something like this: swaggerDoc.Info.Version but how can I get access to SwaggerDocument from the Apply in my SwaggerExcludePropertySchemaFilter class below ?

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);
                    }
                }

            }
        }

推荐答案

尝试使用 IDocumentFilter ,请参见示例:

Try use IDocumentFilter , see example:

 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>();

这篇关于如何配置Swashbuckle仅忽略特定api版本的模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆