Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swagger doc is not creating. #47

Closed
sitamailing opened this issue Dec 19, 2016 · 6 comments
Closed

swagger doc is not creating. #47

sitamailing opened this issue Dec 19, 2016 · 6 comments

Comments

@sitamailing
Copy link

Swagger doc is not creating. Just following is displaying.
{"info":{"title":"Node Swagger API","version":"1.0.0","description":"Demonstrating how to desribe a RESTful API with Swagger"},"host":"localhost:3000","basePath":"/","swagger":"2.0","paths":{},"definitions":{},"responses":{},"parameters":{},"securityDefinitions":{},"tags":[]}

@drGrove
Copy link
Contributor

drGrove commented Dec 20, 2016

Hi @sitamailing,

Can you please provide some more information, as well as check other open bugs to see if you're using something that is part of a known issue?

Thanks!

@sitamailing
Copy link
Author

My app.js exist in another folder, not in the root directory. If I put my app.js in the root folder, it works. Do I need to change the path of swagger.json or something else?

@drGrove
Copy link
Contributor

drGrove commented Dec 20, 2016

@sitamailing,

It may have something to do with the way your calling your routes to be processed by swagger-jsdoc. Relative routes should work, but what I tend to do in the base of my applications is get the PWD of the base file or if you have libraries outside of that folder that need referencing generate the PROJECT_ROOT (below are app.rootDir) to base all my globs from. This should allow the routes to generate properly. The reference below can be seen here in a koa generator I built with built-in swagger-jsdoc support.

let swaggerOptions =
      { swaggerDefinition:
          { swagger: '2.0'
          , info:
              { title: 'API Explorer' // Title (required)
              , version: '1.0.0' // Version (required)
              , contact:
                  { name: ''
                  , url: ''
                  }
              }
          , host: `${config.app.domain || config.app.host}:${config.app.port}`
          , basePath: config.app.namespace
          }
      , apis:
          [ app.rootDir + '/routes/**/*.js'
          , app.rootDir + '/lib/**/*.js'
          , app.rootDir + '/models/**/*.js'
          ]
      };

    // Initialize swagger-jsdoc -> returns validated swagger spec in json format
    let swaggerSpec = swaggerJSDoc(swaggerOptions);

@sitamailing
Copy link
Author

Thank you. It works for me.

@aayushis12
Copy link

So, my server configuration looks something like this -

const options = {
  swaggerDefinition: {
    // Like the one described here: https://swagger.io/specification/#infoObject
    info: {
      title: 'Test API',
      version: '1.0.0',
      description: 'Test Express API with autogenerated swagger doc',
    },
  },
  apis: ['./server/abc.ts']
}

const swaggerJsdoc = require('swagger-jsdoc')
const specs = swaggerJsdoc(options);

app.get('/swagger.json', (_req, res) => {
  res.send(specs)
})

app.use('/swagger', swaggerUi.serve, swaggerUi.setup(specs));


/**
 * @swagger
 * /loginUser:
 *   post:
 *     tags:
 *       - Users
 *     name: Login
 *     summary: Logs in a user
 *     consumes:
 *       - application/json
 *     parameters:
 *       - name: body
 *         in: body
 *         schema:
 *           $ref: '#/definitions/User'
 *           type: object
 *           properties:
 *             username:
 *               type: string
 *             password:
 *               type: string
 *               format: password
 *         required:
 *           - username
 *           - password
 *     responses:
 *       200:
 *         description: User found and logged in successfully
 *       401:
 *         description: Bad username, not found in db
 *       403:
 *         description: Username and password don't match
 */
app.get('/test', (_req, res, _next) => {
  res.send(200)
})

But after running the project, nothing is getting generated in swagger.json and the page looks like this -
{"info":{"title":"Test API","version":"1.0.0","description":"Test Express API with autogenerated swagger doc"},"swagger":"2.0","paths":{},"definitions":{},"responses":{},"parameters":{},"securityDefinitions":{},"tags":[]}

@kalinchernev
Copy link
Contributor

@aayushis12 please open a new issue with your code samples. If you have tried the suggestions in this issue and they do not work for you, you might want to include details what you've tried and what you've received as feedback. Otherwise, if suggestions here are not relevant to your issue: create a new issue in order to keep the topic separate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants