简体   繁体   English

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

[英]How to merge swagger multiple files in single swagger doc file?

I have different swagger files for multiple APIs, like swagger1.json for OpenStack API, swagger2.json for Users API etc and I was trying to merge these all swagger files in one single file using remote $ref method. 我为多个API使用了不同的swagger文件,例如OpenStack API的swagger1.json,Users API的swagger2.json等,我试图使用远程$ ref方法将所有这些swagger文件合并为一个文件。

Here is swagger1.json file for OpenStack api 这是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"
                }
            }
        }
    }

} }

And this is the swagger-merge.json where I want to add multiple swagger doc file using remote reference. 这是swagger-merge.json,我要在其中使用远程引用添加多个swagger文档文件。

{
"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"
}

} }

This isn't working for me. 这对我不起作用。 I am not able to see API methods I have written inside swagger1.json file. 我看不到我在swagger1.json文件中编写的API方法。 I have upload swaggerUI output. 我上传了swaggerUI输出。 Any idea about what I am doing wrong and how can I solve this issue? 关于我做错了什么以及如何解决此问题的任何想法? 在此处输入图片说明

You can't $ref the whole contents of paths , you can only refer individual path items: 您不能$ref paths的全部内容,而只能引用单个路径项:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM