【问题标题】:How to merge swagger multiple files in single swagger doc file?如何在单个 swagger doc 文件中合并 swagger 多个文件?
【发布时间】:2018-06-12 19:01:32
【问题描述】:

我对多个 API 有不同的 swagger 文件,例如用于 OpenStack API 的 swagger1.json,用于用户 API 的 swagger2.json 等,我试图使用远程 $ref 方法将这些所有 swagger 文件合并到一个文件中。

这里是 OpenStack api 的 swagger1.json 文件

{
    "stacks": {
        "get": {
            "tags": [
                "openstack"
            ],
            "summary": "Returns stacks from OpenStack",
            "description": "Returns all stacks from the OpenStack based on tenantId.",
            "consumes": [
                "application/json"
            ],
            "produces": [
                "application/json"
            ],
            "parameters": [
                {
                    "in": "query",
                    "type": "string",
                    "name": "tenantId",
                    "description": "search stacks for tenantId from OpenStack",
                    "required": false
                }
            ],
            "responses": {
                "200": {
                    "description": "OK"
                },
                "400": {
                    "description": "Unknown Error"
                },
                "401": {
                    "description": "Unauthorized"
                }
            }
        }
    }

}

这是我想使用远程引用添加多个 swagger doc 文件的 swagger-merge.json。

{
"swagger": "2.0",
"info": {
    "description": "something here",
    "version": "v0.7.0",
    "title": "The API Gateway",
    "contact": {
        "email": "dp@gmail.com"
    }
},
"host": "localhost",
"port":"9191",
"basePath": "/api/openstack",
"tags": [
    {
        "name": "OpenStackApi",
        "description": "Get stacks and running instance form OpenStack"
    }
],
"schemes": [
    "https"
],
"paths": {
    "$ref": "./swagger1.json#/stacks"
}

}

这对我不起作用。我看不到我在 swagger1.json 文件中编写的 API 方法。我已经上传了 swaggerUI 输出。关于我做错了什么以及如何解决这个问题的任何想法?

【问题讨论】:

    标签: json node.js swagger


    【解决方案1】:

    你不能$refpaths的全部内容,你只能引用个别路径项:

    "paths": {
       "/stacks": {    <--- endpoint path
         "$ref": "./swagger1.json#/stacks"
       }
    }
    

    【讨论】:

    • 谢谢,我只有一个项目是堆栈,我用你建议的 waht 替换了这个 "paths": {"$ref": "./swagger1.json#/stacks"} 但仍然是它不工作。
    • 不,浏览器控制台没有错误。输出正是我在上面发布的。并且 swagger1.json 和 swagger-merge.json 两个文件都在同一个目录中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-22
    • 2018-12-07
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多