Shamoon
Shamoon

Reputation: 43639

How to set up Swagger with express?

I am using the {swagger-express} library and my code is all in CoffeeScript. For my definition, I have:

app.use swagger.init app,
  apis: ['./src/routes.coffee', './src/models.yml']
  apiVersion: '0.1.0'
  basePath: "http://localhost:#{port}"
  info:
    title: 'My API'
    description: 'A complete listing of all API functions'
  swaggerUI: path.join __dirname, 'public'
  swaggerURL: '/swagger'

require('./src/routes') app

In routes, I have:

  ###
   * @swagger
   * path: /login
   * operations:
   *   -  httpMethod: POST
   *      summary: Login with username and password
   *      notes: Returns a user based on username
   *      responseClass: User
   *      nickname: login
   *      consumes:
   *        - text/html
   *      parameters:
   *        - name: username
   *          description: Your username
   *          paramType: query
   *          required: true
   *          dataType: string
   *        - name: password
   *          description: Your password
   *          paramType: query
   *          required: true
   *          dataType: string
  ###

and that works fine. My model.yml file is:

definitions:
  User:
    properties:
      user_id:
        type: string
        description: Unique ID to represent the user
      first_name:
        type: string
        description: First name of the Uber user.
      last_name:
        type: string
        description: Last name of the Uber user.
      email:
        type: string
        description: Email address of the Uber user
      picture:
        type: string
        description: Image URL of the Uber user.
      promo_code:
        type: string
        description: Promo code of the Uber user.

But that doesn't show up in the api-docs.json. I am trying to define the models in one file and the paths in another. Can that be done?

Upvotes: 2

Views: 566

Answers (1)

erik-e
erik-e

Reputation: 3891

I don't think it will work because each format is read separately and saved in a hash which is uses the resourcePath as the key.

https://github.com/fliptoo/swagger-express/blob/a5560af936e5398affe36d347af5be2a1bb64fc2/lib/swagger-express/index.js#L27

Any further declaration of the same resourcePath will override the previous declaration.

I tested with an updated yml format with a resourcePath, the name models instead of definitions and a unique id for the model. This made the models show up but none of my other information :/

resourcePath: /login
models:
  User:
    id: User
    properties:
      user_id:
        type: String
        description: Unique ID to represent the user
      first_name:
        type: String
        description: First name of the Uber user.
      last_name:
        type: String
        description: Last name of the Uber user.
      email:
        type: String
        description: Email address of the Uber user
      picture:
        type: String
        description: Image URL of the Uber user.
      promo_code:
        type: String
        description: Promo code of the Uber user.

The example on their github makes it look like this will work but I couldn't make it work.

Upvotes: 2

Related Questions