Search code examples
javascripttypescriptexpressswaggerjsdoc

Swagger documentation not working for Express server


So basically I have an Express API that I've been working on and recently I got the idea to implement a documentation for it. I chose Swagger and JSDoc to do this as it is quick and easy. However the documentation won't work. Here is how I've implemented it so far.

docs.ts:

import swaggerJSDoc, { Options } from "swagger-jsdoc";

const swaggerOptions: Options = {
   definition: {
      info: {
         title: "Project Nonya Documentation.",
         description: "Documentation for the Project Nonya API",
         contact: {
            name: "Joe Mama",
         },
         version: "6.9.0",
      },
   },
   apis: ["./routes/*.routes.ts"],
};

export const swaggerDocs = swaggerJSDoc(swaggerOptions);

router.ts:

...

import swaggerUi from "swagger-ui-express";
import { swaggerDocs } from "./docs";

...

app.use("/api/documentation", swaggerUi.serve, swaggerUi.setup(swaggerDocs));

...

When I got to the docs endpoint, the documentation does appear, however its keeps saying No Operations defined in spec!. Here is one of the files in the routes folder.

testing.routes.ts:

import express, { Router } from "express";
import { testing} from "../controllers/testing/testing.controller";

const router: Router = express.Router();

/**
 * @swagger
 * /:
 *   get:
 *     description: Why you no work?
 *     responses:
 *       200:
 *         description: Returns nothing cause this shit won't work.
 */
router.post("/testing/:id", testing);

export default router;

What am I doing wrong? I can't figure it out. The directories are like this: src/docs.ts, src/router.ts, and src/routes/testing.router.ts.

Side Question

Since I am using TypeScript, I obviously have to compile the code into JavaScript. However in the apis array in docs.ts, I specify files with a *.routes.ts. Would I have to change this to *routes.js.

Thank You!


Solution

  • Found this Github issue to be the solution to my problem.

    https://github.com/Surnet/swagger-jsdoc/issues/150