如何用快递设置Swagger?

时间:2015-01-28 15:49:35

标签: coffeescript swagger swagger-ui

我正在使用{swagger-express}库,我的代码全部在CoffeeScript。根据我的定义,我有:

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

routes中,我有:

  ###
   * @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
  ###

并且工作正常。我的model.yml文件是:

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.

但这并没有出现在api-docs.json中。我想在一个文件中定义models,在另一个文件中定义paths。可以这样做吗?

1 个答案:

答案 0 :(得分:2)

我认为它不会起作用,因为每个格式都是单独读取并保存在使用resourcePath作为键的哈希中。

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

同一resourcePath的任何进一步声明都将覆盖之前的声明。

我使用resourcePath更新了yml格式,名称为models而不是definitions,并为模型提供了唯一的id。这使模型显示但我没有其他信息:/

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.

他们的github上的示例使它看起来会起作用,但我无法使其正常工作。