Coder Social home page Coder Social logo

Comments (6)

drGrove avatar drGrove commented on June 27, 2024 16

@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);

from swagger-jsdoc.

drGrove avatar drGrove commented on June 27, 2024

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!

from swagger-jsdoc.

sitamailing avatar sitamailing commented on June 27, 2024

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?

from swagger-jsdoc.

sitamailing avatar sitamailing commented on June 27, 2024

Thank you. It works for me.

from swagger-jsdoc.

aayushis12 avatar aayushis12 commented on June 27, 2024

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":[]}

from swagger-jsdoc.

kalinchernev avatar kalinchernev commented on June 27, 2024

@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.

from swagger-jsdoc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.