swagger UI无法处理redoc能够的swagger.json

时间:2016-11-22 01:51:59

标签: swagger-ui swagger-2.0 go-swagger

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

我想用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页面显示如下 enter image description here

这是招摇文件

{
  "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": {}
}

1 个答案:

答案 0 :(得分:1)

来自SwaggerUI docs,似乎预计

  

描述OpenAPI规范的JSON对象

作为spec参数的值。 如果您想为其提供网址,请使用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();