1

我有以下简单的 swagger.json 文件。这是使用 go-swagger 注释为 golang 服务生成的。我能够让 UI 页面使用 redoc 运行。

我想用 swagger-ui 显示它,但我无法让它工作。它在页面加载时的控制台中显示一个错误,上面写着

Uncaught TypeError: Cannot create property 'definitions' on string 'swagger.json'(…)

window.swaggerUi = new SwaggerUi({
        spec: "swagger.json",
        dom_id: "swagger-ui-container",
        supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
        onComplete: function(swaggerApi, swaggerUi){
          log("Loaded UI")
        },
        onFailure: function(data) {
          log("Unable to Load SwaggerUI");
        },
        docExpansion: "none",
        jsonEditor: false,
        defaultModelRendering: 'schema',
        showRequestHeaders: false
      });

window.swaggerUi.load();

不知道为什么会这样

redoc页面显示如下 在此处输入图像描述

这是招摇文件

{
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "schemes": [
    "http",
    "https"
  ],
  "swagger": "2.0",
  "info": {
    "description": "the purpose of this service is to do a health check",
    "title": "Health Check API.",
    "termsOfService": "TOS",
    "contact": {
      "name": "Backend",
      "email": "Backend@company.com"
    },
    "license": {
      "name": "Company Licence"
    },
    "version": "0.0.1"
  },
  "host": "host.com",
  "basePath": "/",
  "paths": {
    "/health": {
      "get": {
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "schemes": [
          "http",
          "https"
        ],
        "summary": "Health check route.",
        "operationId": "health",
        "responses": {}
      }
    }
  },
  "definitions": {}
}
4

1 回答 1

1

SwaggerUI docs看来,它期望

描述 OpenAPI 规范的 JSON 对象

作为spec参数的值。url如果你想为它提供 url,你应该使用:

window.swaggerUi = new SwaggerUi({
        url: "swagger.json", // <----------------- change to url here
        dom_id: "swagger-ui-container",
        supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
        onComplete: function(swaggerApi, swaggerUi){
          log("Loaded UI")
        },
        onFailure: function(data) {
          log("Unable to Load SwaggerUI");
        },
        docExpansion: "none",
        jsonEditor: false,
        defaultModelRendering: 'schema',
        showRequestHeaders: false
      });

window.swaggerUi.load();
于 2017-11-22T08:52:01.610 回答