Surnet / swagger-jsdoc

Generates swagger/openapi specification based on jsDoc comments and YAML files.
MIT License
1.66k stars 224 forks source link

Is it possible to generate a swagger.json file? #380

Closed ronaldl29 closed 4 months ago

ronaldl29 commented 9 months ago

I am loading my *.routes.api.js files like this:


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

const options = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: 'Hello World',
      version: '1.0.0',
    },
  },
  apis: ['./src/routes*.js'], // files containing annotations as above
};

const openapiSpecification = swaggerJsdoc(options);

I would like to export this to a swagger.json file. Is it possible with this package?

allenhwkim commented 8 months ago

Check this out.

$ npx swagger-jsdoc     
Usage: swagger-jsdoc [options] <path ...>

Options:
  -V, --version                            output the version number
  -d, --definition <swaggerDefinition.js>  Input swagger definition.
  -o, --output [swaggerSpec.json]          Output swagger specification.
  -h, --help                               display help for command
# $ npx swagger-jsdoc -d <<definition-file>> -o swagger.json <<your-api-entry-point>>
$ npx swagger-jsdoc -d swagger.js -o swagger.json index.js
Schloemicher commented 7 months ago

while running it can be done like this:

import { writeFile } from 'fs/promises';

writeFile('swagger.json', JSON.stringify(openapiSpecification, null, 2), 'utf-8');
stale[bot] commented 5 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.