apigee-127 / swagger-node-runner

The heart of Swagger-Node
MIT License
102 stars 123 forks source link

Swagger response validation fails on .json(jsonObj) in Express #108

Open randomcatgamer opened 7 years ago

randomcatgamer commented 7 years ago

Hello,

So in my swagger yaml i define that all errors are of this structure: `ResponseMessage: required:

Also, I have an error middleware plugged into express: app.use(errorHandler);

// and a response validation listener (that should only be called if there actually are any errors or warnings) swaggerExpress.runner.on('responseValidationError', function(validationResponse, request, response) { config.logger.error('swagger validation error', validationResponse.errors); });

export function errorHandler(err, req, res, next) { res.status(500).json({message: err.message}); res.end(); }

The problem I encounter is that when I trigger an error, it goes to the errorHandler(), to the .json({message: err.message}) -> res.send (from express) -> which at some point triggers the this.end() -> which triggers hookEnd from connect_middleware.js (from swagger-node-runner) -> ...-> convertValue (from sway) which simply returns the raw JSON (which is a Buffer value) and then tries to validate the schema against that -> which obviously fails.

What am I missing here?

randomcatgamer commented 7 years ago

So noticed some other things inside the sway/lib/helpers.js -> convertValue that should actually convert the raw JavascriptObject to a proper Object, the line 173: var type = _.isPlainObject(schema) ? schema.type : undefined; seems to be the troublemaker since it evaluates the schema: image as type undefined which leads to line 193 getting triggered if (_.isUndefined(value)) { return value; }

instead of line 202 which would trigger if the undefined check was not done before