swagger-api/swagger-node

Project generator installs old version of swagger (and upgrading breaks project)

jwalton opened this issue · 5 comments

swagger project create test, using express, installs swagger-express-mw@0.1.0.

$ swagger project create test
(Pick "express")
$ cd test
$ npm install --save swagger-express-mw@0.7.0
$ npm start

Then send a query to http://127.0.0.1:10010/hello?name=Scott, and you get back:

TypeError: Cannot read property 'name' of undefined
    at hello (/Users/jwalton/Development/swagger/test/api/controllers/hello_world.js:39:33)
    at swagger_router (/Users/jwalton/Development/swagger/test/node_modules/swagger-node-runner/fittings/swagger_router.js:104:13)
    at Runner.<anonymous> (/Users/jwalton/Development/swagger/test/node_modules/bagpipes/lib/bagpipes.js:171:7)
    at bound (domain.js:301:14)
    at Runner.runBound (domain.js:314:12)
    at Runner.pipeline (/Users/jwalton/Development/swagger/test/node_modules/pipeworks/pipeworks.js:72:17)
    at Runner.flow (/Users/jwalton/Development/swagger/test/node_modules/pipeworks/pipeworks.js:223:19)
    at Pipeworks.flow (/Users/jwalton/Development/swagger/test/node_modules/pipeworks/pipeworks.js:135:17)
    at Pipeworks.siphon (/Users/jwalton/Development/swagger/test/node_modules/pipeworks/pipeworks.js:186:19)
    at Runner.<anonymous> (/Users/jwalton/Development/swagger/test/node_modules/bagpipes/lib/bagpipes.js:98:22)

The offending line is:

function hello(req, res) {
  // variables defined in the Swagger document can be referenced using req.swagger.params.{parameter_name}
  var name = req.swagger.params.name.value || 'stranger'; // <===========
  var hello = util.format('Hello, %s!', name);

  // this sends back a JSON response which is a single string
  res.json(hello);
}

req.swagger.params does not exist. The only object on req.swagger is req.swagger.operation.

Aha! To make this work you need to add swagger_params_parser to your swagger_controllers:

    swagger_controllers:
      - onError: json_error_handler
      - cors
      - swagger_params_parser
      - swagger_security
      - _swagger_validate
      - express_compatibility
      - _router

Thanks. I just ran into this. Weird that the Hello World (or hello Scott?) example doesn't work right now

swagger-node devs - any chance of adding the small fix that jwalton (my hero now!) has suggested above.
You wouldn't believe how frustrating it is for a Swagger/Node n00b to blithely do an audit fix and then have all his (or hers I guess) code break in all kinds of fun ways.
Unless you've got shares in Advil of course...

@bluebob Haha. Thanks. My ultimate solution to this, though, was to write a whole new project that supports OpenAPI 3.x.x: https://github.com/exegesis-js/exegesis

I'd rather use express way to get http query parameter(the swagger api way is too tightly coupled), I changed this line in hello_world.js.

// var name = req.swagger.params.name.value ;
// change the above line to the following
var name = req.query.name;

And it just works. No need to configure - swagger_params_parser.