Reub
Reub

Reputation: 785

express-swagger-generator: defining models for swagger

According to the docs to define a model for swagger, the code look

/**
 * @typedef Product
 * @property {integer} id
 * @property {string} name.required - Some description for product
 * @property {Array.<Point>} Point
 */

/**
 * @typedef Point
 * @property {integer} x.required
 * @property {integer} y.required - Some description for point - eg: 1234
 * @property {string} color
 * @property {enum} status - Status values that need to be considered for filter - eg: available,pending
 */

/**
 * @typedef Error
 * @property {string} code.required
 */

/**
 * @typedef Response
 * @property {[integer]} code
 */


/**
 * This function comment is parsed by doctrine
 * sdfkjsldfkj
 * @route POST /users
 * @param {Point.model} point.body.required - the new point
 * @group foo - Operations about user
 * @param {string} email.query.required - username or email
 * @param {string} password.query.required - user's password.
 * @param {enum} status.query.required - Status values that need to be considered for filter - eg: available,pending
 * @operationId retrieveFooInfo
 * @produces application/json application/xml
 * @consumes application/json application/xml
 * @returns {Response.model} 200 - An array of user info
 * @returns {Product.model}  default - Unexpected error
 * @returns {Array.<Point>} Point - Some description for point
 * @headers {integer} 200.X-Rate-Limit - calls per hour allowed by the user
 * @headers {string} 200.X-Expires-After -  date in UTC when token expires
 * @security JWT
 */

is there an way to seperate the models in a different file? like i could import it to my route. I would like to avoid populating my routes with models for swagger defintion.

Upvotes: 0

Views: 2080

Answers (1)

arolux
arolux

Reputation: 11

In the expressSwagger options, you can define multiple locations, from where documentation will be read:

(...)
files: ['./routes/**/*.js', './yourModelPath/*.js'] //Path to the API handle folder

You can then reference a model which is defined in another file, as in the example with Product referencing Point, which may be defined elsewhere:

File A:

 /**
 * @typedef Product
 * @property {integer} id
 * @property {string} name.required - Some description for product
 * @property {Array.<Point>} Point
 */

File B:

/**
 * @typedef Point
 * @property {integer} x.required
 * @property {integer} y.required - Some description for point - eg: 1234
 * @property {string} color
 * @property {enum} status - Status values that need to be considered for filter - eg: available,pending
 */

Upvotes: 1

Related Questions