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

Schema validation for XML responses #573

Description

@ilaif

Hey guys,
I hope it's the right repo to open an issue in, let me know if not.

I'm currently running a restify server with the swagger-restify-mw package to have my routes validated by a swagger api doc.
All was well until I needed to support XML requests/response. So I've done the following:

  • Added application/xml to consumes, produces.
  • Used express-xml-bodyparser parser as a middleware to parse XML for requests (with some customer formatter I added to normalize to how a js object would look like with json request) - requests part works fine.
  • In response, I'm sending the response like this:
res.header('Content-Type', 'application/xml');
res.send({
    data
});
res.end();

while I have a restify formatter to transform the response into XML:

const xml2js = require('xml2js');
const XMLBuilder = new xml2js.Builder();

const app = restify.createServer({
    ...
    formatters: {
        'application/xml': (req, res, body, cb) => {
            if (!body) {
                res.setHeader('Content-Length', 0);
                return (null);
            }

            if (body instanceof Error) {
                if ((body.restCode || body.httpCode) && body.body) {
                    body = body.body;
                } else {
                    body = {
                        message: body.message
                    };
                }
            }

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

            const data = XMLBuilder.buildObject(obj);

            res.setHeader('Content-Length', Buffer.byteLength(data));
            return cb(null, data);
        }
    }
});

Since swagger-restify-mw is working with res.json(...) and not res.send(...) (Which I need for the formatter to hook), then I get the following error after the response is sent back to the client successfully:

Error: Response validation failed: failed schema validation
    at throwErrorWithCode (/Users/ilaif/git/location-notifier/node_modules/swagger-tools/lib/validators.js:121:13)
    at Object.module.exports.validateAgainstSchema (/Users/ilaif/git/location-notifier/node_modules/swagger-tools/lib/validators.js:176:7)
    at /Users/ilaif/git/location-notifier/node_modules/swagger-tools/middleware/swagger-validator.js:141:22
    at /Users/ilaif/git/location-notifier/node_modules/swagger-tools/node_modules/async/lib/async.js:356:13
    at async.forEachOf.async.eachOf (/Users/ilaif/git/location-notifier/node_modules/swagger-tools/node_modules/async/lib/async.js:233:13)
    at _asyncMap (/Users/ilaif/git/location-notifier/node_modules/swagger-tools/node_modules/async/lib/async.js:355:9)
    at Object.map (/Users/ilaif/git/location-notifier/node_modules/swagger-tools/node_modules/async/lib/async.js:337:20)
    at validateValue (/Users/ilaif/git/location-notifier/node_modules/swagger-tools/middleware/swagger-validator.js:134:11)
    at ServerResponse.res.end (/Users/ilaif/git/location-notifier/node_modules/swagger-tools/middleware/swagger-validator.js:252:9)
    at _cb (/Users/ilaif/git/location-notifier/node_modules/restify/lib/response.js:337:14)
    at ServerResponse.formatJSON (/Users/ilaif/git/location-notifier/node_modules/restify/lib/formatters/json.js:37:12)
    at ServerResponse.format (/Users/ilaif/git/location-notifier/node_modules/restify/lib/response.js:152:23)
    at ServerResponse.send (/Users/ilaif/git/location-notifier/node_modules/restify/lib/response.js:345:14)
    at Object.exports.sendResult (/Users/ilaif/git/location-notifier/src/api/libs/response.lib.js:18:9)
    at Promise.resolve.then.then.then.results (/Users/ilaif/git/location-notifier/src/api/controllers/compute.controller.js:101:22)
    at bound (domain.js:301:14)
    at runBound (domain.js:314:12)
    at tryCatcher (/Users/ilaif/git/location-notifier/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/ilaif/git/location-notifier/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/Users/ilaif/git/location-notifier/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/Users/ilaif/git/location-notifier/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/Users/ilaif/git/location-notifier/node_modules/bluebird/js/release/promise.js:693:18)

So, my responses aren't being validated by the swagger-tools module, and I get an annoying error in the background. Diving into the code, I see that the validation occurs, after my "xml formatter" output, and therefore, the module is trying to parse an xml text string instead of a JS object. I suppose that XML should be supported, since swagger also supports that.
What am I doing wrong? Or how can this be fixed?

My versions:
"swagger-restify-mw": "^0.1.0",
which uses:
"swagger-node-runner": "^0.7.0"

Thanks!
Ilai

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions