带有多个 URL 的 Swagger UI

Swagger UI with Multiple Urls

我在 swagger ui 文档中看到您可以提供一个 urls 参数,它是:

An array of API definition objects ({url: "", name: ""}) used by Topbar plugin. When used and Topbar plugin is enabled, the url parameter will not be parsed. Names and URLs must be unique among all items in this array, since they're used as identifiers.

我希望这会给我一个选择器,我可以从中选择要处理的 yaml 文件。不幸的是,它似乎没有做任何事情。

这是我的代码:

window.onload = function() {
  // Build a system
  const ui = SwaggerUIBundle({
    urls: [
    {url:"http://test.dev/documentation/microservices/microservices.yaml",name:"All Microservices"},
    {url:"http://test.dev/documentation/microservices/plans.yaml",name:"Plans"},
    ],
    dom_id: '#swagger-ui',
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: "StandaloneLayout"
  })

  window.ui = ui
}

我还想将 primaryName 设置为所有微服务。

关于我哪里出错的任何想法?

Swagger UI 3.0.18 及更高版本支持 urls 配置选项。

您可以像这样使用它代替 url

window.onload = function() {
  // Build a system
  const ui = SwaggerUIBundle({
    urls: [
      {url: "https://path/to/api1.yaml", name: "API One"},
      {url: "https://path/to/api2.yaml", name: "API Two"},
    ],
    "urls.primaryName": "API Two"  // default document (if other than the first)
    ...
  })

结果: