No operations defined in spec! - I get this error even though the swagger is setup and the end points are defined

96.4k views Asked by At

I am trying to setup swagger on top of my node application using the swagger npm package. I have my end points and swagger setup perfect(atleast almost perfect), I did do quiet a lot of research on whats going wrong but I couldn't find the trace. My swagger setup file:

const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const swaggerJSDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const abc= require('./routes/abc');
var app = express();

const swaggerDefinition = {
    info: {
        title: 'Analytics Project',
        version: '1.0.0',
        description: 'Analytics API swagger documentation'
    }
};
const options = {
    swaggerDefinition,
    apis: ['./routes/abc.js']
};

const swaggerSpec = swaggerJSDoc(options);

var api = require('./routes/abc');
app.use('/', api);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/api/v1/abc', abc);
app.use('/api/v1/abc/scatter', abc);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
module.exports = app;

My end points are defined in ./routes/abc.js:

var express = require('express');
var router = require('express').Router();
const request = require('request');

/**
 * @swagger
 * /:
 *   get:
 *     description:  Endpoint for everything
 */
router.get('/', function(req, res, next) { //End point1
    res.send('hello from dataservice');
  });

/**
 * @swagger
 * /scatter:
 *   post:
 *    description:  Endpoint for all variations of scatter plots
 */
router.post('/scatter', function(req, res, next) { //end point2
    res.json({users: [{name: 'Timmy'}]});
  });
module.exports = router;

I was expecting the 2 end points to show up on the page. But instead I get the 'No operations defined in spec!' error. What am I missing? Any help is appreciated.

14

There are 14 answers

5
Wisdom On

It may be that there is an issue with how you are referencing your routes. The referencing must always start from the root of your application. So './routes/abc.js' must be changed to 'the-folder-name-in-root/routes/abc.js'

0
Abdulrhman Adel On

make apis full path like this apis: [__filename], it work for me

0
3bu1 On

For me it worked when i added paths like below

 * @swagger
 * paths:
 *  /auth/login:
 *    post:
0
Jiyan On

I solved this by changing URL from "../api/controllers/userController.js" to "./src/api/controllers/userController.js". So, you have to enter URL starting from root folder.

1
klee1611 On

Try change apis path from apis: ['./routes/abc.js'] to apis: [`${__dirname}/routes/abc.js`] to make it the full path from the root folder. That works for me.

1
Nishant Joshi On

I was getting same issue for fastify. try to require() your routes after you register swagger. And magically, it worked for me. My case was:

When I registered routes before swagger, it was showing 'no operations defined in specs':

fastify.register(require("./routes/route")); //registered routes BEFORE swagger
fastify.register(require("fastify-swagger"), {

exposeRoute: true,

routePrefix: "/docs",

swagger: {

info: { title: "SWAGGER API" },

}

});
//gave that issue with no warnings / errors in terminal or browser console

This worked: registering routes after swagger

fastify.register(require("fastify-swagger"), {

exposeRoute: true,

routePrefix: "/docs",

swagger: {

info: { title: "SWAGGER API" },

}

});

fastify.register(require("./routes/route")); //registered routes after swagger
0
Ricardo_Ortiz On

maybe if you use a similar configuration you need specify the root package of project "my.root.package"

@Bean
public Docket productApi(){
return new Docket(DocumentationType.SWAGGER_2).select()
           .apis(RequestHandlerSelectors.basePackage("my.root.package")).build();
        }
0
Claudio Tejada On

What helped me in .NET 6 was that I forgot to specify that I'm using controllers. This little piece of code fixed my issue in Program.cs:

builder.Services.AddControllers();
0
Enrico On

In .NET 6 don't forget to add

app.MapControllers();

along with

builder.Services.AddControllers();

(as mentioned by Syscall)

0
Md. Jahid Hossain On

Always start referring from the root of the application.

path

const path = require('path');

const options = {
    definition: {},
    apis: [path.join(process.cwd(), '/routes/*.routes.js')], 
};
0
Salami  Korede On

I got this same error, I was able to solve it immediately I realise that I was referencing the wrong package.

@Bean
public Docket api() {
   return new Docket(DocumentationType.SWAGGER_2)
      .select()
      .paths(PathSelectors.any())
      .apis(RequestHandlerSelectors.basePackage("com.korede.liberations"))
      .build()
      .apiInfo(apiDetails());
}

Just in case anybody is having this same issue, check the package you're referencing in your

RequestHandlerSelectors.basePackage()

0
Júlio César Ferreira On

In my case, the problem was on the packages. Swagger was not reading the controllers. I was using springBoot and eclipse STS as the IDE.

I had to:

  1. Recreate each package from the base package.
  2. Pull the right button over the base package, and select new packages one by one.
  3. move each class to its package.

After that swagger shows the endpoints correctly.

0
Haridas Dhulgande On

"No operations defined in spec!"

  1. Please review the package versions in the Dependencies/Packages folder or in the .csproj file.

  2. Update them to versions compatible with the current C# version.

0
Hassan Moradi Fard On

If your code has this line of code, remove it :

[ApiExplorerSettings(GroupName = "Name")]