Skip to content
This repository has been archived by the owner on Sep 14, 2022. It is now read-only.

project creation for restify does not validate responses correctly #569

Open
hhellbusch opened this issue Jul 17, 2018 · 0 comments
Open

Comments

@hhellbusch
Copy link

I realize this isn't a fault of swagger-node per say, but I don't think the CLI should provide an option to create a project with broken features / emits errors / warnings that are not fault of the developer using it.


(generic) Steps to reproduce - see further down for my project as an example (you will be able to git clone / run it yourself)

npm install -g  swagger
swagger project create my-awesome-restify-proj

Move cursor w/ the arrow keys to select "restify"

Update the api/swagger/swagger.yaml to include a response that has a schema.
Create a controller to handle the request and generate the response
Start the runtime with DEBUG=swagger-tools* swagger project start
Execute the end point

Expected:
Validation of response to pass

Actual:
Validation of the response always fails - claiming the expected type is object, but in reality is undefined. Looking at the actual response, it is indeed an object.

swagger-tools:middleware:validator   Response validation: +2ms
  swagger-tools:middleware:validator     Response code: 200 +0ms
  swagger-tools:middleware:validator     Validation: failed +1ms
  swagger-tools:middleware:validator   Reason: Failed schema validation +0ms
  swagger-tools:middleware:validator   Errors: +0ms
  swagger-tools:middleware:validator     0: +0ms
  swagger-tools:middleware:validator       code: INVALID_TYPE +0ms
  swagger-tools:middleware:validator       message: Expected type object but found type undefined +0ms
  swagger-tools:middleware:validator       path: [] +0ms
  swagger-tools:middleware:validator   Stack: +1ms
  swagger-tools:middleware:validator       at throwErrorWithCode (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/swagger-tools/lib/validators.js:121:13) +0ms
  swagger-tools:middleware:validator       at Object.module.exports.validateAgainstSchema (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/swagger-tools/lib/validators.js:176:7) +0ms
  swagger-tools:middleware:validator       at /home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/swagger-tools/middleware/swagger-validator.js:141:22 +0ms
  swagger-tools:middleware:validator       at /home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/async/lib/async.js:356:13 +0ms
  swagger-tools:middleware:validator       at async.forEachOf.async.eachOf (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/async/lib/async.js:233:13) +0ms
  swagger-tools:middleware:validator       at _asyncMap (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/async/lib/async.js:355:9) +0ms
  swagger-tools:middleware:validator       at Object.map (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/async/lib/async.js:337:20) +0ms
  swagger-tools:middleware:validator       at validateValue (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/swagger-tools/middleware/swagger-validator.js:134:11) +0ms
  swagger-tools:middleware:validator       at ServerResponse.res.end (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/swagger-tools/middleware/swagger-validator.js:252:9) +0ms
  swagger-tools:middleware:validator       at _cb (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/restify/lib/response.js:337:14) +0ms
Error: Response validation failed: failed schema validation
    at throwErrorWithCode (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/swagger-tools/lib/validators.js:121:13)
    at Object.module.exports.validateAgainstSchema (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/swagger-tools/lib/validators.js:176:7)
    at /home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/swagger-tools/middleware/swagger-validator.js:141:22
    at /home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/async/lib/async.js:356:13
    at async.forEachOf.async.eachOf (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/async/lib/async.js:233:13)
    at _asyncMap (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/async/lib/async.js:355:9)
    at Object.map (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/async/lib/async.js:337:20)
    at validateValue (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/swagger-tools/middleware/swagger-validator.js:134:11)
    at ServerResponse.res.end (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/swagger-tools/middleware/swagger-validator.js:252:9)
    at _cb (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/restify/lib/response.js:337:14)

I troubleshooted the error down to the order of the parameters being passed by the callback -

In restify/lib/formatters/json.js - calls the callback like - return cb(null, data);

// Copyright 2012 Mark Cavage, Inc.  All rights reserved.

'use strict';

///--- Exports

/**
 * JSON formatter.
 * @public
 * @function formatJSON
 * @param    {Object} req  the request object
 * @param    {Object} res  the response object
 * @param    {Object} body response body
 * @param    {Function} cb cb
 * @returns  {String}
 */
function formatJSON(req, res, body, cb) {
    if (body instanceof Error) {
        // snoop for RestError or HttpError, but don't rely on
        // instanceof
        res.statusCode = body.statusCode || 500;

        if (body.body) {
            body = body.body;
        } else {
            body = {
                message: body.message
            };
        }
    } else if (Buffer.isBuffer(body)) {
        body = body.toString('base64');
    }

    var data = JSON.stringify(body);
    res.setHeader('Content-Length', Buffer.byteLength(data));

    return cb(null, data);
}

module.exports = formatJSON;

Where the validator is expected a different callback signature - (snippet below starts around line 170 in swagger-tools/middleware/swagger-validator.js)

var wrapEnd = function (req, res, next) {
  var operation = req.swagger.operation;
  var originalEnd = res.end;
  var vPath = _.cloneDeep(req.swagger.operationPath);
  var swaggerVersion = req.swagger.swaggerVersion;

  res.end = function (data, encoding) { // <-- this is the line with the different method signature
    var schema = operation;
    var val = data;
    var responseCode;
//...

I started to dig into the versioning / issue list for the other projects... The restify project that is generated is using a lot of old versions of swagger-related projects.

Main one I noticed is that there's plans to deprecate swagger-tools - apigee-127/swagger-tools#335.


I've uploaded my project that demonstrates this here - https://gitlab.com/hhellbusch/permits-node-restify-swagger

Steps to reproduce w/ my example project -

git clone https://gitlab.com/hhellbusch/permits-node-restify-swagger.git
cd permits-node-restify-swagger
npm install
node app.js

open web browser to http://localhost:10011/docs/
expand default, expand "POST" /electrical
click on the "Example Value" for the model - this will insert into the request
Click on "Try it out!"
Look at the console - will see an error like -

➜  permits git:(master) node app.js 
Error: Response validation failed: failed schema validation
    at throwErrorWithCode (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/swagger-tools/lib/validators.js:121:13)
    at Object.module.exports.validateAgainstSchema (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/swagger-tools/lib/validators.js:176:7)
    at /home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/swagger-tools/middleware/swagger-validator.js:141:22
    at /home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/async/lib/async.js:356:13
    at async.forEachOf.async.eachOf (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/async/lib/async.js:233:13)
    at _asyncMap (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/async/lib/async.js:355:9)
    at Object.map (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/async/lib/async.js:337:20)
    at validateValue (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/swagger-tools/middleware/swagger-validator.js:134:11)
    at ServerResponse.res.end (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/swagger-tools/middleware/swagger-validator.js:252:9)
    at _cb (/home/hhellbusch/git/hhellbusch/pam-solar/permits/node_modules/restify/lib/response.js:337:14)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant