Swagger JSDoc does not loading API files from sub folders

5k Views Asked by At

I am integrating swagger doc for a project in node.js and express framework,

NPM: Swagger JSDoc - ^4.0.0 NPM

Below is folder structure: (Note: module-1, module-2 and module-3 are folders!)

 - app 

   - module-1 
      - module-1.controller.js
      - module-1.model.js
      - module-1.route.js
   - module-2 
      - module-2.controller.js
      - module-2.model.js
      - module-2.route.js
   - module-3 
      - module-3.controller.js
      - module-3.model.js
      - module-3.route.js

Working Example:

swaggerJSDoc({
    swaggerDefinition: {
        ...require('../swagger.json')
    },
    apis: [
        './app/module-1/*',
        './app/module-2/*'
        './app/module-3/*'
    ]
})

Node: I don't want to add module path every time when i create a new module like above example.

What i am expecting is below example: I want to set it one time like set main folder/file path and it will load API Doc data automatically from provided main folder/file path.

I tries below example but its not working with /app/*:

swaggerJSDoc({
    swaggerDefinition: {
        ...require('../swagger.json')
    },
    apis: [
        './app/*'
    ]
})

Please guide if is there any way to do this, Any kind of suggestion will be appreciated, Thanks.

3

There are 3 best solutions below

0
On BEST ANSWER

That should work

apis: ['./app/**/*.js'],
1
On

What worked for me:

Given the following project structure

- src
  - application
    - routes/
        admin.routes.js
        job.routes.js
  - app.js

in app.js I had my configuration as follows:

const specs = swaggerJsdoc({
  definition: {
    info: {
      title: 'Bank API',
      version: '1.0.0',
    },
  },
  apis: [`./application/routes/*.js`],
});

changing to:

const specs = swaggerJsdoc({
  definition: {
    info: {
      title: 'Bank API',
      version: '1.0.0',
    },
  },
  apis: [`${__dirname}/application/routes/*.js`],
});

made it work

0
On

I had a similar issue, because the operation definitions were located in a different folder.

  • Use __dirname to step on the current folder.
  • Use path.join to make a clear and easy-to-read path.
  • Move to the intended folder where the definitions live.
  • Use /** to grab all folders recursively.
  • And finally, use /*.js with the file extension

For example:

apis: [path.join(__dirname, '/../../src/controllers/**/*.js')