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.41k stars 225 forks source link

Defaulting to https://petstore.swagger.io/v2/swagger.json and CORS error #314

Closed flyfishMT closed 1 year ago

flyfishMT commented 1 year ago

I am loading my OpenAPI spec through openapi-comment-parser, based upon the suggestion in this issue. Here is my abbreviated code;

const express = require('express');
const cors = require('cors');
const swaggerUi = require('swagger-ui-express');
const openapi = require('openapi-comment-parser');

const specs = openapi();

const app = express();

app.use(cors());
app.use(express.json());
app.use('/swagger', swaggerUi.serve);
app.get(
  '/swagger',
  swaggerUi.setup(null, {
    swaggerOptions: {
      url: '/swagger/swagger.json',
    },
  })
);

app.get('/swagger/swagger.json', (req, res) => res.json(specs));

I have recently updated all my libs and not sure what changed but now the explorer bar is showing up and defaulting to https://petstore.swagger.io/v2/swagger.json, which tries to load this and causes a CORS error (even though I am using the express-cors module).

image

I would like it just to load my api definition and not have an explorer bar. I've tried many options - explorer:false, setting url, swaggerUrl, url in base yml file, etc. with no success.

Any suggestions would be appreciated!

scottie1984 commented 1 year ago

The get for '/swagger/swagger.json' needs to be before the app.use(‘/swagger’

flyfishMT commented 1 year ago

Thanks, that fixed. Strange because when it was "/api-docs" rather than "/swagger" it worked without moving.