How to organise/build a Swagger UI interface for a directory which contains many Swagger definition .json/.yml files

喜你入骨 提交于 2019-11-28 11:38:40

what you're looking for can be done very easily with the basic Swagger-ui tool.

Essentially what you have is a list of many swagger definitions. I'm guessing that you want to let the user click a button or a link, or choose a definition from a drop-down to select the API to view. Once that's done, you can do the following:

  • Let the user choose an API definition to show. You can get this easily from adding a HTML element to your index.html and trigging some javascript after selecting
  • A single swagger-ui container can be reloaded and reused. Get the URL to the swagger definition from the first step and supply it to the swagger-ui object, which is typically done like this:

    window.swaggerUi.updateSwaggerUi({url: 'http://your.spec.com/swagger.yaml'})

Now the container will reload with the spec you've specified.

Swagger UI 3.0.19 natively supports multiple specs via the urls parameter. When urls is used, the top bar displays a drop-down list of specs instead of an input box.

Usage

Edit dist\index.html and change

url: "http://petstore.swagger.io/v2/swagger.json",

to

urls: [
   {name: "petstore",  url: "http://petstore.swagger.io/v2/swagger.json"},
   {name: "instagram", url: "https://api.apis.guru/v2/specs/instagram.com/1.0.0/swagger.yaml"}
],
"urls.primaryName": "petstore",  // default spec


Now your Swagger UI top bar looks like this:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!