api - 当提供支持多个API规范的预生成链接时,Swagger UI不会加载规范

标签 api go swagger-ui go-templates api-doc

我有一个用Go编写的服务,该服务还使用go模板作为前端。我们的外部第三方将此服务用作门户网站,以查找和搜索内容。还有另一种服务,它是用于处理订单的rest API。门户服务有一个页面,您可以在其中查找API文档。
我只有一个API版本,并且使用SwaggerUI来显示API文档。我必须创建一个新的端点,并将其作为新API版本的一部分。现在,我想展示一个新的API版本,但也要展示一个用于支持旧客户端的旧版本。像这样:
enter image description here

因此,当用户单击门户网站上的按钮以查看文档时,该请求由门户中的此功能处理(注意:我已经重构了此功能以支持多个URL):

func getDocs(c echo.Context) error {
    source := c.Get(auth.SourceName).(string)
    key := c.Get(auth.KeyName).(string)

    jsonURLs := []DocsURL{
        {
            url:         fmt.Sprintf("%s/0.1/docs?source=%s", config.baseURL, key),
            name:        "0.1"
        },
        {
            url:         fmt.Sprintf("%s/1.0/docs?source=%s", config.baseURL, key),
            name:        "0.1"
        },
    }

    return c.Render(http.StatusOK, "docs/index", map[string]interface{}{
        "source":   source,
        "key":      key,
        "pageType": "docs",
        "jsonURLs": jsonURLs,
    })
}

这是我使用SwaggerUI的模板中的脚本:

  window.onload = function() {
    const ui = SwaggerUIBundle({
      urls: {{ .jsonURLs }},
      dom_id: '#swagger-ui',
      deepLinking: true,
      presets: [
        SwaggerUIBundle.presets.apis,
        SwaggerUIStandalonePreset
      ],
      plugins: [
        SwaggerUIBundle.plugins.DownloadUrl
      ],
      layout: "StandaloneLayout",
      supportedSubmitMethods: []
    })
    window.ui = ui
    $('.tryitout').prop('disabled', true);
  }

我需要根据环境(生产,暂存,本地)生成链接。在后端生成数据并将其提供给模板进行显示也是有意义的。但是,这不起作用!

但是,如果我对SwaggerUIBundle中的链接进行硬编码,则它可以正常工作,并且门户网站会显示正确的下拉菜单,并允许在版本之间进行切换并为相应版本加载文档。

func getDocs(c echo.Context) error {
    source := c.Get(auth.SourceName).(string)
    key := c.Get(auth.KeyName).(string)

    return c.Render(http.StatusOK, "docs/index", map[string]interface{}{
        "source":   source,
        "key":      key,
        "pageType": "docs",
    })
}

在模板中:

window.onload = function() {
    const ui = SwaggerUIBundle({
      urls: [
        {
          url: "http://localhost:8088/0.1/docs?source=111111",
          name: "0.1"
        },
        {
          url: "http://localhost:8088/1.0/docs?source=111111",
          name: "1.0"
        }
      ],
      dom_id: '#swagger-ui',
      deepLinking: true,
      presets: [
        SwaggerUIBundle.presets.apis,
        SwaggerUIStandalonePreset
      ],
      plugins: [
        SwaggerUIBundle.plugins.DownloadUrl
      ],
      layout: "StandaloneLayout",
      supportedSubmitMethods: []
    })
    window.ui = ui
    $('.tryitout').prop('disabled', true);
  }

这是为什么?有没有办法使第一个版本的代码起作用?

我需要链接是动态的,最好是在处理程序中生成。谢谢!

最佳答案

似乎有两个错误:

第一的:

  urls: {{ .jsonURLs }},

这不会为您编写JSONt格式的jsonURLs。它将仅编写jsonURLs的字符串表示形式。您需要编写模板来迭代jsonURLs的元素并一一打印出来,或者将jsonURLs编码为json自己:
jsonText,_:=json.Marshal(jsonURLs)
return c.Render(http.StatusOK, "docs/index", map[string]interface{}{
        "source":   source,
        "key":      key,
        "pageType": "docs",
        "jsonURLs": string(jsonText),
    })

第二:您似乎没有导出DocsURL结构的成员字段。将字段名称大写并添加json标签。
type DocsURL struct {
  URL string `json:"url"`
  Name string `json:"name"`
}

关于api - 当提供支持多个API规范的预生成链接时,Swagger UI不会加载规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59505856/

相关文章:

go - 如何做 Mock Go-lang GRPC 方法

windows - 打开闪存驱动器的句柄给我一个 "Access Denied"错误代码

mysql - 如何从数据库中解析时间

php - 在页面上包含 API 的最有效方法是什么?

pointers - 尽管映射始终是引用类型,但如果它们是从非指针接收器返回的呢?

java - 来自现有 JSON 的 Springfox Swagger Ui

jakarta-ee - 将不记名 token 的字段添加到 Java EE/Jersey 中生成的 Swagger UI

Django 休息 : How to disable the POST api with {pk}

c# - 要连接到 Git,Ruby 会比 c# 有优势吗?

python - 使用 Python 更新媒体维基文章?