Sakib
Sakib

Reputation: 1543

swagger UI unable to process swagger.json that redoc is able to

I have the following simple swagger.json file. This is generated using go-swagger annotations for a golang service. I am able to get the UI page running with redoc.

I want to display it with swagger-ui but I cannot get it to work. It shows an error in console on the page load that says

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();

Not sure why that is happening

The redoc page displays as follows enter image description here

This is the swagger file

{
  "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": "[email protected]"
    },
    "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": {}
}

Upvotes: 2

Views: 1203

Answers (1)

RomanHotsiy
RomanHotsiy

Reputation: 5148

From SwaggerUI docs, it seems that it expects

A JSON object describing the OpenAPI Specification

as a value of spec parameter. You should use url if you want to provide it with 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();

Upvotes: 1

Related Questions