scottie1984 / swagger-ui-express

Adds middleware to your express app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app.
MIT License
1.42k stars 225 forks source link

Unable to render this definition #272

Closed Giovannisimo closed 2 years ago

Giovannisimo commented 2 years ago

Hi all,

I tried to implement the solution but I'm stuck with this error:

The provided definition does not specify a valid version field. Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: "2.0" and those that match openapi: 3.0.n (for example, openapi: 3.0.0).

My implementation is the following:

swagger.js

import swaggerAutogen from 'swagger-autogen';

var doc = {
    "swagger": "2.0",
    "info": {
      "title": "Blah",
      "description": "",
      "version": "1.0"
    },
    "produces": ["application/json"],
    "paths": { 
...
  const outputFile = './swagger-output.json';
  const endpointsFiles = ['./index.js'];

  swaggerAutogen()(outputFile, endpointsFiles, doc);

index.js

...
import * as oSwaggerFile from './swagger-output.json';
app.use('/', swaggerUi.serve, swaggerUi.setup(oSwaggerFile));

When I look into the delivered files from the webrequest, the file looks like this:

swagger-ui-init.js


window.onload = function() {
  // Build a system
  var url = window.location.search.match(/url=([^&]+)/);
  if (url && url.length > 1) {
    url = decodeURIComponent(url[1]);
  } else {
    url = window.location.origin;
  }
  var options = {
  "swaggerDoc": {
    "default": {
      "swagger": "2.0",
      "info": {
        "version": "1.0.0",
        "title": "REST API",
        "description": ""
...

This looks fine for me, maybe one of you has an idea what might be wrong?

Cheers Gio

scottie1984 commented 2 years ago

Please validate your swagger file here https://editor.swagger.io/. It doesn't look right as the root level should start with "swagger": "2.0"