swagger-api/swagger-codegen

Generated nodejs server does not include `x-swagger-router-controller` or `operationId` attributes in `swagger.yaml`

Open

#1,932 建立於 2016年1月21日

在 GitHub 查看
 (9 留言) (0 反應) (0 負責人)HTML (5,474 fork)batch import
Issue: BugServer: Nodejshelp wanted

倉庫指標

Star
 (12,701 star)
PR 合併指標
 (30 天內沒有已合併 PR)

描述

From @kmd-jamesgauld on January 20, 2016 13:59

Swagger Router middleware requires that a x-swagger-router-controller attribute be present to help route requests to the correct controller, and an operationId attribute to route to a specific method.

Currently, when exporting a nodejs server app, the editor does not include these attributes in the api/swagger.yaml file which, when making a request to the nodejs server, throws the following error:

Error: Cannot resolve the configured swagger-router handler: undefined
    at swaggerRouter (/var/www/mock-api/node_modules/swagger-tools/middleware/swagger-router.js:414:18)
    at call (/var/www/mock-api/node_modules/connect/index.js:239:7)
    at next (/var/www/mock-api/node_modules/connect/index.js:183:5)
    at /var/www/mock-api/node_modules/swagger-tools/middleware/swagger-validator.js:350:30
    at /var/www/mock-api/node_modules/async/lib/async.js:52:16
    at /var/www/mock-api/node_modules/async/lib/async.js:361:13
    at /var/www/mock-api/node_modules/async/lib/async.js:52:16
    at async.forEachOf.async.eachOf (/var/www/mock-api/node_modules/async/lib/async.js:236:30)
    at _asyncMap (/var/www/mock-api/node_modules/async/lib/async.js:355:9)
    at Object.map (/var/www/mock-api/node_modules/async/lib/async.js:337:20)

Partial yaml config:

...
paths:
  /users:
    get:
      summary: "Get Users."
...

There exists a controllers/User.js file with relevant content:

...
module.exports.usersGet = function usersGet (req, res, next) {
  User.usersGet(req.swagger.params, res, next);
};
...

Adding the missing attributes fixes the problem:

...
paths:
  x-swagger-router-controller: User
  /users:
    get:
      operationId: usersGet
      summary: "Get Users."
...

Copied from original issue: swagger-api/swagger-editor#784

貢獻者指南