apigee-127 / swagger-restify

Other
4 stars 10 forks source link

using @0.7.0, gives 500: {"message":"req.query must be provided for 'query' parameters"} #2

Closed osher closed 8 years ago

osher commented 8 years ago

Original title:

app.on('after', ...) does not get called

Hi.

I'm using a swagger project create -f restify boilerplate, and I've been moving on from there.

I found that connecting the restify.auditLogger(..) middleware by the book does not work. It appears that the 'after' event does not get fired.

app.js:

'use strict';
var name = require('./package').name;
var log = require('bunyan').createLogger({
  application:  name,
  name:         name,
  stream:       process.stdout
});
log.info('starting web server process');

var SwaggerRestify = require('swagger-restify-mw');
var restify = require('restify');
var app = restify.createServer({ log: log });

//HACK: reveal all events emitted by app
var emit = app.emit;
app.emit = function(name) { 
    console.log('restify server emits: %s(%s)', name, arguments.length - 1);
    emit.apply(app, arguments)
}
//HACKEnd  

module.exports = app; // for testing

var config = {
  appRoot: __dirname // required config
};

SwaggerRestify.create(config, function(err, swaggerRestify) {
  if (err) { throw err; }

  app.pre(restify.pre.userAgentConnection());  

  app.use(restify.requestLogger({properties: { type: 'request-runtime' } }));
  app.use(restify.dateParser());
  app.use(restify.acceptParser(app.acceptable));

  swaggerRestify.register(app);

  app.use(restify.gzipResponse());  
  app.on('after', function(req, res, route, err) {
      console.log("after fired")
  });

  var port = process.env.PORT || 10010;
  app.listen(port);

  if (swaggerRestify.runner.swagger.paths['/hello']) {
    console.log('try this:\ncurl http://127.0.0.1:' + port + '/hello?name=Scott');
  }
});

Output:

C:\ws\evals\swagger\swagger-project-hello-restify>node app.js | bunyan
[2016-09-01T15:42:16.605Z]  INFO: swagger-project-restify/17932 on Osher-PC: starting web server process (application=swagger-project-restify)
WARNING: NODE_ENV value of 'dev' did not match any deployment config file names.
WARNING: See https://github.com/lorenwest/node-config/wiki/Strict-Mode
try this:
curl http://127.0.0.1:10010/hello?name=Scott
restify server emits: request(2)
restify server emits: preDone(2)
Terminate batch job (Y/N)?

C:\ws\evals\swagger\swagger-project-hello-restify>

First, I thought it's because the generated controllers do not call next. So I added a call to next:

api/controllers/hello_world.js

'use strict';
var util = require('util');

module.exports = {
  hello: hello
};

function hello(req, res, next) {
  var name = req.swagger.params.name.value || 'stranger';
  var hello = util.format('Hello, %s!', name);

  res.json(hello);
  next()
}

It did not do the trick: kept working the same.

Any help?

osher commented 8 years ago

perhaps the problem is with swagger project generate it gave me swagger-restify-mw@0.1.0

osher commented 8 years ago

I upgraded to @0.7.0, same code as above. and now, for the /hello?name=scott I get:

{"message":"req.query must be provided for 'query' parameters"}

hu...?

well. at least the 'after' is fired... :stuck_out_tongue:

theganyo commented 8 years ago

Did you add "swagger_params_parser" to your swagger_controllers? See: https://github.com/theganyo/swagger-node-runner/releases/tag/v0.6.0.

osher commented 8 years ago

I see. Done :)

osher commented 8 years ago

By the way - there are 2 small error in the instructions on the link you provided:

_swagger_params_parser:                    # <= Add this definition
  name: swagger_params_parser
    jsonOptions: {}
    urlencodedOptions:
      extended: false
    multerOptions:
      inMemory: true
    textOptions:
      type: */*
  1. - type: */* - does not compile for me. type: "*/*" does.
  2. - All the entries bellow name: swagger_params_parser are indented one level too much, and thast does not compile too (complains at jsonOption: {}).

here's the part that works for me:

    openapi_params_parser:
      name:                 swagger_params_parser
      jsonOptions:          {}
      urlencodedOptions:
        extended:           false
      multerOptions:
        inMemory:           true
      textOptions:
        type:               "*/*"

ah, right. I name it openapi_params_parser instead of _swagger_params_parser, but that's just semantics.

FYI.