【问题标题】:Swagger UI configuration with swagger-config.yaml使用 swagger-config.yaml 配置 Swagger UI
【发布时间】:2019-04-24 04:22:45
【问题描述】:

根据 swagger 文档,

Swagger-UI 在四个位置接受配置参数。

从最低到最高优先级:

  1. 项目根目录下的 swagger-config.yaml,如果存在,则烘焙到应用程序中
  2. 配置对象作为参数传递给 Swagger-UI (SwaggerUI({ ... }))
  3. 从指定的 configUrl 获取配置文档
  4. 配置项作为 URL 查询字符串中的键/值对传递

我尝试将 swagger-config.yaml 放在应用程序的根目录中,但它不起作用。

我已经按照 swagger 安装步骤操作,并且工作正常。 但招摇自定义配置的步骤不起作用。我保存了如下文件,

 swagger-ui
   |--swagger-config.yaml
   |--index.html

swagger-config.yaml

url: "https://petstore.swagger.io/v2/swagger.json"
dom_id: "#swagger-ui"
validatorUrl: "https://online.swagger.io/validator"
oauth2RedirectUrl: "http://localhost:3200/oauth2-redirect.html"

index.html

// Begin Swagger UI call region
      const ui = SwaggerUIBundle({
        //url: "https://petstore.swagger.io/v2/swagger.json",
        //dom_id: '#swagger-ui',
        deepLinking: true,
        presets: [
          SwaggerUIBundle.presets.apis,
          SwaggerUIStandalonePreset
        ],
        plugins: [
          SwaggerUIBundle.plugins.DownloadUrl
        ],
        layout: "StandaloneLayout"
      })

知道我是否遗漏了什么吗?

【问题讨论】:

  • 如何运行 Swagger UI - 从文件系统、Web 服务器或使用 npm start 打开页面?
  • 我在 JBoss 和文件系统上都试过了。
  • 你说它不起作用。你能详细说明一下吗?当您查看 Web 浏览器的控制台时,您是否看到任何错误消息?当您查看浏览器的网络控制台时,您是否看到任何成功的下载 petstore.swagger.io/v2/swagger.json 的尝试?
  • @MahendraKapadne 你的问题解决了吗?我遇到了完全相同的问题 - swagger-ui 没有提取我放入 swagger-ui 根目录的 swagger-config.yaml。
  • @Bernd 目前,我在 url 中硬编码了相对 URI。

标签: javascript swagger swagger-ui swagger-2.0 openapi


【解决方案1】:

我也有这个问题。从文档来看,如果使用 swagger-config.xml,似乎我们不需要在 index.html 中配置任何内容,实际上,它在我这边不起作用,我还没有找到原因。 但如果改用 configUrl,它就可以了。

// Begin Swagger UI call region
const ui = SwaggerUIBundle({
  //url: "https://petstore.swagger.io/v2/swagger.json",
  //dom_id: '#swagger-ui',
  configUrl: "../swagger-config.yaml",
  deepLinking: true,
  presets: [
    SwaggerUIBundle.presets.apis,
    SwaggerUIStandalonePreset
  ],
  plugins: [
    SwaggerUIBundle.plugins.DownloadUrl
  ],
  layout: "StandaloneLayout"
})

并且可以配置支持数组。

---
urls:
 - url: "https://petstore.swagger.io/v2/swagger.json"
   name: "url1"
   
 - url: "https://petstore.swagger.io/v2/swagger.json"
   name: "url2"

【讨论】:

    【解决方案2】:

    正如Github's issue thread 中提到的,现在只接受.json 文件用于conf。可能为时已晚,但它有助于在 2021 年 11 月寻找答案。

    【讨论】: