如何在单个swagger doc文件中合并swagger多个文件?

时间:2018-06-12 19:01:32

标签: json node.js swagger

我为多个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-merge.json,我想使用远程引用添加多个swagger doc文件。

{
"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输出。关于我做错了什么以及如何解决这个问题的任何想法?enter image description here

1 个答案:

答案 0 :(得分:0)

您不能$ref paths的全部内容,您只能引用单个路径项:

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