'Auto generate swagger schema definitions

I'm automatically generating my api documentation using the swagger-ui-express and swagger-jsdoc libraries. Follow swagger setup code in app.js:

const swaggerJsDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');

const swaggerOptions = {
  swaggerDefinition: {
    info: {
      title: 'My API',
      version: '1.0.0',
    },
    servers: ['http://localhost:8000'],
  },
  apis: ['./routes/*.js'],
};
const swaggerDocs = swaggerJsDoc(swaggerOptions);

app.use('/api-docs',
  swaggerUi.serve,
  swaggerUi.setup(swaggerDocs),
);

In routes files is like that

/**
 * @swagger
 * /user/get/all:
 *  get:
 *   summary: Get all users
 *   responses:
 *    '200':
 *     description: successful operation
 */

I'm looking for a way to automatically generate the schema definitions of my models. If I were generating the swagger documentation with a json, I would do it like this:

"definitions": {
  "ApiResponse": {
    "type": "object",
    "properties": {
      "code": {
        "type": "integer",
        "format": "int32"
      },
      "type": {
        "type": "string"
      },
      "message": {
        "type": "string"
      }
    }
  },
  "Category": {
    "type": "object",
    "properties": {
      "id": {
        "type": "integer",
        "format": "int64"
      },
      "name": {
        "type": "string"
      }
    },
    "xml": {
      "name": "Category"
    }
  },
}

Does anyone know how I can do to auto generate these schema definitions?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source