【问题标题】:swagger UI unable to process swagger.json that redoc is able toswagger UI 无法处理 redoc 能够处理的 swagger.json
【发布时间】:2016-11-22 01:51:59
【问题描述】:

我有以下简单的 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": {}
}

【问题讨论】:

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


    【解决方案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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-13
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多