0

Here is Swagger has been setup in our backend and prior it was working just fine but today for certain unknown reasons I can't seem to figure out it stopped working and when we visit our endpoint locally we just get a blank page and the tab loader just keeps spinning

const options = {
definition: {
openapi: "3.0.0",
info: {
  title: "Docs",
  version,
},
compontents: {
  securitySchemas: {
    bearerAuth: {
      type: "http",
      scheme: "bearer",
      bearerFormat: "JWT",
    },
  },
},
security: [
  {
    bearerAuth: [],
  },
],
},

apis: ["./backend/api/V1/routes/*.routes.js"],
swaggerOptions: {
url: "/api-docs/swagger.json",
},
};

const openapiSpecification = swaggerJsdoc(options);
function swaggerDocs(app, port) {
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(openapiSpecification));
app.get("/api-docs.json", (req, res) => {
res.setHeader("Content-Type", "application/json");

res.send(openapiSpecification);
});
logger.info(`Swagger docs are running at http://localhost:${port}/api-docs`);
}

module.exports = {
swaggerDocs,
};

Here are the various package versions we are using

  • "swagger-jsdoc": "^6.2.1",
  • "swagger-ui-express": "^4.5.0",
Chowa C
  • 41
  • 5

2 Answers2

0

Look Like you are missing info of version here:

info: {
  title: "Docs",
  version,
}

You can fix that by adding version to it, for example version: "1.0.0"

Let me know if it works or not

mhung912
  • 12
  • 3
  • the `version` comes from the pacakage.json like so `const { version } = require("../../package.json");`. – Chowa C Feb 09 '23 at 02:10
  • So try change like this: `info: { ... , servers: ["http://localhost:${YOUR_PORT}"]}` – mhung912 Feb 09 '23 at 03:32
  • @mhung912 `servers` is a root-level key in OpenAPI, it's not part of `info`. Also, `servers` is an array of [Server Objects](https://spec.openapis.org/oas/v3.0.0.html#server-object) not an array of strings. – Helen Feb 09 '23 at 06:10
0

How i resolved the issue was move the entire code into my app.js. and everything works as expected

Chowa C
  • 41
  • 5