Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Open
randomcatgamer opened this issue Aug 8, 2017 · 1 comment
Open

Comments

@randomcatgamer
Copy link

randomcatgamer commented Aug 8, 2017

Hello,

So in my swagger yaml i define that all errors are of this structure:
ResponseMessage: required: - message properties: message: type: string

And that the api produces application/json.

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
Copy link
Author

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant