swagger-api/swagger-codegen

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

Open

#1932 aperta il 21 gen 2016

Vedi su GitHub
 (9 commenti) (0 reazioni) (0 assegnatari)HTML (5474 fork)batch import
Issue: BugServer: Nodejshelp wanted

Metriche repository

Star
 (12.701 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

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

Guida contributor