如何将架构过滤器添加到一个 Swashbuckle api 文档版本 [英] How to add a Schema Filter to just one Swashbuckle api document version

查看:31
本文介绍了如何将架构过滤器添加到一个 Swashbuckle api 文档版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我使用 Swashbuckle 4 但它不支持变形,我发现有两个过滤器可以做到这一点.一个 DocumentFilter 和一个 SchemaFilter.

I have an issue, I use Swashbuckle 4 and it doesn't support polymorph, I found two filters that do that. One DocumentFilter and one SchemaFilter.

他们的工作很有魅力.

但是现在我遇到了一个问题,我在我的 api 中添加了第二个版本.但是我有一个多态模型,它只被版本 2 支持.

But now I have an issue, I add a second version to my api. But I have a polymorph model which is just supported by version 2.

但我发现无法将这些过滤器仅添加到一个 api 版本,因为它们是在启动时添加到 SwaggerGenOptions 的.

But I found no way to add these filters just to one api version, because they are added at Startup to the SwaggerGenOptions.

我有点惊讶 Swashbuckle 似乎不支持每个版本的过滤器,因为版本正好在一个项目中分离两个代码世界.

I'm little bit shocked that Swashbuckle does not seem to support Filters per version, because versions are exactly to separate two code worlds in one project.

有什么想法吗?

推荐答案

我们需要一个例子的原因是因为有不同的方式来处理版本控制.我不确定你是怎么做的,但通常模型是用同名的新类进行版本控制的,但在不同的命名空间中,在这种情况下没有问题.注册所有过滤器.他们应该知道如何判断正在处理的对象是否是他们知道如何处理的对象.我不熟悉 IDocumentFilter,但对于 ISchemaFilter:

The reason we need an example is because there are different ways of approaching versioning. I'm not sure how you are doing it, but usually a Model is versioned with a new class with the same name, but in a different Namespace, in which case there is no issue. Register all your Filters. They should know how to tell if the object being processed is the one they know how to handle. I'm not familiar with IDocumentFilter, but for ISchemaFilter:

    public class MyV1SchemaFilter : ISchemaFilter
    {
        public void Apply(Schema schema, SchemaFilterContext context)
        {
            if (context.SystemType != MyProject.Models.v1.MyModel) return;
            /// configure v1 schema
        }
    }

    public class MyV2SchemaFilter : ISchemaFilter
    {
        public void Apply(Schema schema, SchemaFilterContext context)
        {
            if (context.SystemType != MyProject.Models.v2.MyModel) return;
            /// configure v2 schema
        }
    }

您也可以将其全部放在一个过滤器中:

You could also put it all in one Filter:

    public class MySchemaFilter : ISchemaFilter
    {
        public void Apply(Schema schema, SchemaFilterContext context)
        {
            if (context.SystemType == MyProject.Models.v1.MyModel)
            {
                /// configure v1 schema
            }
            else if (context.SystemType == MyProject.Models.v2.MyModel)
            {
                /// configure v2 schema
            }
        }
    }

如果您以某种方式由同一个类表示两个版本,那么您可能需要将它们分开.

If you are doing it in a way where both versions are somehow represented by the same Class, then maybe you need to separate them.

DocumentFilterContext 似乎有足够的数据,它也可以知道它传递的是哪个版本.

DocumentFilterContext seems like it has enough data in there for it to know which version it was passed, as well.

这篇关于如何将架构过滤器添加到一个 Swashbuckle api 文档版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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