NSwag 多文档端点

2023-12-22

是否可以像 swashbuckle 那样拥有多个文档端点?

options.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1");
options.SwaggerEndpoint("/swagger/v2/swagger.json", "API v2");

如果是,如何装饰 api 调用,以便一些属于一个版本,一些属于另一个版本?

所以根据 Rico Suter 的建议,我所做的有点像:

ApiVersionAttribute.cs

public class ApiVersionAttribute:Attribute
    {
        private List<string> _versions = new List<string>();

        public List<string> Versions { get { return _versions; } }

        public ApiVersionAttribute(string version) {
            Versions.Add(version);
        }
    }

MyApiVersionProcessor.cs

        public string Version { get; set; }

        public MyApiVersionProcessor(string version)
        {
            this.Version = version;
        }

        public new Task<bool> ProcessAsync(OperationProcessorContext context)
        {
            bool returnValue = true;

            var versionAttributes = context.MethodInfo.GetCustomAttributes()
                .Concat(context.MethodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes())
                .Where(a => a.GetType()
                                .IsAssignableTo("MapToApiVersionAttribute", TypeNameStyle.Name) 
                            || a.GetType()
                                .IsAssignableTo("ApiVersionAttribute", TypeNameStyle.Name)
                       )
                .Select(a => (dynamic)a)
                .ToArray();

            var versionAttribute = versionAttributes.FirstOrDefault();

            if (null == versionAttribute)
            {
                returnValue = false;
            }
            else
            {
                if (ObjectExtensions.HasProperty(versionAttribute, "Versions")
                    && Version.Equals(versionAttribute.Versions[0].ToString()))
                {
                    ReplaceApiVersionInPath(context.OperationDescription, versionAttribute.Versions);
                }
                else {
                    returnValue = false;
                }
            }

            return Task.FromResult(returnValue);
        }

        private void ReplaceApiVersionInPath(SwaggerOperationDescription operationDescription,
            dynamic versions)
        {
            operationDescription.Path = operationDescription.Path.Replace("{version:apiVersion}",
                versions[0].ToString());
        }
    }

在我的 Global.asax 中

                // NSwag
                // https://github.com/RSuter/NSwag/wiki/OwinGlobalAsax#integration                
                app.UseSwaggerUi(typeof(WebApiApplication).Assembly, new SwaggerUiSettings
                {                       
                    //TypeNameGenerator = new MyTypeNameGenerator(),
                    MiddlewareBasePath = "/swagger",
                    SwaggerRoute = "/swagger/v1/swagger.json",
                    Version = "1.0.0.0",
                    // https://github.com/RSuter/NSwag/wiki/Middlewares
                    OperationProcessors =
                    {
                        new MyApiVersionProcessor("v1")
                    },
                    PostProcess = document =>
                    {                        
                        document.BasePath = "/";
                        document.Produces
                            = new List<string> { "application/json"
                                                , "text/json"
                                                , "text/html"
                                                , "plain/text"
                                                , "application/xml"};
                        document.Consumes
                            = document.Produces;
                        document.Info.Title = "Document V1";                     
                    }

                });

                app.UseSwaggerUi(typeof(WebApiApplication).Assembly, new SwaggerUiSettings
                {
                    //TypeNameGenerator = new MyTypeNameGenerator(),
                    MiddlewareBasePath = "/swagger",
                    SwaggerRoute = "/swagger/v2/swagger.json",
                    Version = "2.0.0.0",
                    OperationProcessors =
                    {
                        new MyApiVersionProcessor("v2")
                    },
                    PostProcess = document =>
                    {
                        document.BasePath = "/";
                        document.Produces
                            = new List<string> { "application/json"
                                                , "text/json"
                                                , "text/html"};
                        document.Consumes
                            = document.Produces;
                        document.Info.Title = "Document V2";
                    }

                });

并装饰我的控制器方法

[ApiVersion("v2")]

[ApiVersion("v1")]

etc.


您可以定义app.UseSwagger两次并实现一个自定义操作处理器,该处理器根据您的需要仅过滤所需的 api 操作(即,在第一次调用中,您应该过滤所有版本 x,在第二次调用中过滤所有版本 y)。

The ApiVersionProcessor当前默认添加的仅将路由路径中的版本占位符替换为第一个声明的版本。我认为我们应该扩展这个处理器,以便您可以排除版本并插入正确的版本。

顺便说一句:我是 NSwag 的作者。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NSwag 多文档端点 的相关文章

  • 功能性香蕉旅行者 - 将行为与游戏状态结合在一起

    问题是我不知道如何创建类型的行为Behavior t GameState 我有更多代码 但我试图仅展示我认为讨论该问题所必需的内容 如果有空白需要填写 请告诉我 这是我所拥有的 data GameState GameState agent
  • 如何自定义 Android BLE(低功耗蓝牙)gatt 传输速度?

    我正在构建一个使用 BLE 技术传输图像的应用程序 图像不需要立即传输 因此在阅读一些文档后 我得出结论 1 3 Mbit s 是一个合理的选择 From Wikipedia Distance Range lt 100 m lt 330 f
  • php 文件上传时间创建

    我知道有一种方法可以使用 PHP 获取文件夹中的文件名列表 but 目前有什么方法可以获取使用 php 上传 创建文件的日期 时间吗 Unix 不跟踪文件的创建时间 只跟踪文件的更改 修改和访问时间 您可以使用文件时间 https www
  • 从命令行参数打开文件

    我正在尝试创建一个汇编程序 该程序创建在命令行 argv 1 上指定的文件 如果字符串已经存储在其中 它工作得很好 但是如果我按原样运行它 而 file name 没有移动到 rbx 它工作得很好 section text global s
  • dynamodb.put().promise() 不返回 put 对象

    我正在尝试利用 aws 和 dynamo db 的异步 等待功能 下面是如何在 async wait 之前放置对象的示例 正 如您在回调中看到的 您可以访问包含放置对象的数据 然而 在使用 async 和 Promise 的第二个代码块中
  • 编码 Katas 用于练习重构遗留代码

    近几个月来 我对 katas 编码非常感兴趣 我相信它们是磨练我的编程技能和提高我在工作中编写的代码质量的好方法 有很多地方都可以找到卡塔斯 喜欢 http codekata pragprog com http codekata pragp
  • 如何将宽字符串文字与 PRId32、PRIu64 等连接?

    假设我需要打印一个格式化的字符串int32 t使用 printf 格式说明符
  • java中new String[]{}和new String[]的区别

    I am fresher在java中 我对java有疑问 那是 String array new String 有什么用 here 有什么区别String array new String and String array new Stri
  • 模糊图像的特定部分(矩形、圆形)?

    我想模糊矩形或圆形图像 经过谷歌搜索后 我发现模糊整个图像很容易 但很难模糊图像的特定部分 矩形 圆形 那么怎么可能呢 提前致谢 只需设置你的UIImageView属性名称为 imageView 并在实现文件中以相同的顺序添加以下四个方法
  • ggvis 交互式图形无法按预期使用反应值工作

    我对以下示例有问题ggvis代码的目的是制作一个绘图 当您将鼠标悬停在该组的任何成员上时 突出显示整个点组 然后 我希望突出显示在您将鼠标悬停后立即消失 发生的情况是 突出显示最初起作用 但当您将鼠标悬停时 突出显示会保留下来 并且只有当您
  • 当填充大型可观察数组时,我可以加快淘汰速度吗?

    我有一个 Web 应用程序 它对某些数据执行 SOAP 请求 并使用结果填充淘汰视图模型 我目前收到了大约 1000 个订单项 必须将它们推送到我的淘汰视图模型上 在 Chrome 中对页面进行分析表明 大部分加载时间 CPU 都花费在 K
  • .htaccess 重定向到 HTTPS(子域除外)

    我想将所有非 https 请求重定向到 https 接受对子域的请求 例如 http example com gt https example com http example com page gt https example com p
  • Laravel:如何使用查询生成器添加 where 子句?

    我有这个查询 使用 Laravel 查询生成器进行 rows DB table elements gt where type 1 对应于 SELECT from elements WHERE type 1 现在 在某些情况下 我需要添加第二
  • iMacros 等待页面加载

    我过去一直使用 iMacros 让宏等待页面加载非常简单 设置 等待页面完成 是然而现在这不起作用 从 iMacros 网站看来http wiki imacros net 等待页面完成 http wiki imacros net WAITP
  • 使用 Volley 且不使用 HttpEntity 处理 POST 多部分请求

    这并不是一个真正的问题 但是 我想在这里分享一些我的工作代码 供您需要时参考 据我们所知HttpEntity从 API22 开始弃用 并从 API23 开始完全删除 目前 我们无法访问Android 开发人员上的 HttpEntity 参考

随机推荐