In the server
folder of my Node.js project, I have an app.js
file where I'm trying to configure swagger-jsdoc
and swagger-ui-express
. I've configured swagger like this:
const options = {
definition: {
openapi: "3.0.0",
info: {
title: "Test API",
version: "1.0.0",
description: "Test API in express",
},
servers: [
{
url: "http://localhost:9000",
description: "development server",
},
],
},
apis: [`./api/*.js`],
};
const specs = swaggerJsDoc(options);
const app = express(apiRoot, api);
app.use("/api-docs", swaggerUI.serve, swaggerUI.setup(specs));
In the server/api/users/index.js
file, I've defined my endpoints like this:
/**
* @swagger
* components:
* schemas:
* User:
* type: object
* required:
* - firstName
* - lastName
* - username
* properties:
* id:
* type: ObjectId
* description: Auto generated id for a user entry
* firstName:
* type: String
* lastName:
* type: String
* username:
* type: String
*/
/**
* @swagger
* /users:
* get:
* summary: Returns all users using the service
* responses:
* 200:
* description: The list of all users
* content:
* application/json:
* schema:
* type: array
* items:
* $ref: '#/components/schemas/User
*/
router.get("/", async (req, res, next) => {
try {
const users = await User.find();
res.status(200);
res.json(users);
} catch (err) {
next(err);
}
});
The issue is that the documentation for the API is not displayed when I visit http://localhost:9000/api-docs
; it simply displays, "No operations defined in spec!". Moreover, when I console.log
the specs
object above, the paths
and component
keys map to empty objects.
I've tried all the suggestions stated here: No operations defined in spec! - I get this error even though the swagger is setup and the end points are defined, but none of them has resolved my issue.