#Adding swagger in my Express + Typescript project: No operations defined in spec!

6 messages · Page 1 of 1 (latest)

woeful quarry
#

I am building an Express app with TypeScript, but now I am struggling to add swagger to my project. Here is my index.ts

import UserRoutes from "./routers/UserRoutes";

import swaggerJsdoc from "swagger-jsdoc";
import swaggerUi from "swagger-ui-express";

const options: swaggerJsdoc.Options = // in the comment

const swaggerSpec = swaggerJsdoc(options);

this.app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
this.app.use("/api/v1/users", UserRoutes);

here is the UserRoutes.ts

class UserRoutes extends BaseRoutes {
  public routes(): void {
    this.router.get("/", UserController.index);
  }
}

export default new UserRoutes().router;

and here is my UserController

class UserController implements IUserController {
  index = async (req: Request, res: Response): Promise<Response> => {
    const service = new UserService(req);
    const users = await service.getAll();
    return res.send({ data: users, message: "success" });
  };

export default new UserController();

and finally here is my UserService.ts

class UserService {
  getAll = async () => {
    const users = await db.user.findAll({
      attributes: ["id", "username"],
    });
    return users;
  };
}

I don't really understand where do I need to put swagger docs like ```ts
/**
@swagger
blablabla
**/


I tried adding it in the routes and the service, and all I got is this in the swagger ui:

No operations defined in spec!

any idea/example online? I could not find example onlie which has the same architecture as what I made, and I am new to Node js as well. Thank you in advance,
#
const options: swaggerJsdoc.Options = {
  definition: {
    openapi: "3.0.0",
    info: {
      title: "REST API Docs",
      version: "1.0.0",
    },
    components: {
      securitySchemas: {
        bearerAuth: {
          type: "http",
          scheme: "bearer",
          bearerFormat: "JWT",
        },
      },
    },
    security: [
      {
        bearerAuth: [],
      },
    ],
  },
  servers: [{ url: "http://localhost:8080" }],
  apis: ["../src/routers/*.ts"],
};
serene belfry
#
class UserRoutes extends BaseRoutes {
  public routes(): void {
    /**
     * @swagger
     * blablabla
    **/
    this.router.get("/", UserController.index);
  }
}

export default new UserRoutes().router;
woeful quarry
serene belfry
#

ngl, if your options are wrong, i can't see where that is. all i know is you haven't used the decorators