krakenjs / swaggerize-express

Design-driven apis with swagger 2.0 and express.
Other
355 stars 81 forks source link

in:body failing #91

Closed skindc closed 8 years ago

skindc commented 8 years ago

Hi, I had the intention on using this library to sync validation rules of local express server with those on our production java application container. When testing all GET methods this seems to work fine once the handlers in place. Yet when I am testing POST methods it seems there is an error in /lib/expressroutes.js valueAccessor method on the block for (param.in === "body").

When I have the declaration below

{
...
"/v1/publications": {
        "post": {
            "tags": ["publications"],
            "summary": "Launch Publication",
            "description": "Launches a new publication",
            "operationId": "createPublication",
            "parameters": [{
                "in": "body",
                "name": "title",
                "description": "Magazine Title",
                "required": false,
                "schema": {
                    "type": "string"
                }
            }, {
                "in": "body",
                "name": "url",
                "description": "Unique URL",
                "required": false,
                "schema": {
                    "type": "string"
                }
            }
...
}

A post to the url host:port/v1/publications with body

{title: "Mag", url: "mag"}

Throws and error

ValidationError: "title" must be a string
at Object.exports.process (/Users/skin/Projects/activeProjects/0123_ST_STProjects/smt-account-management/node_modules/swaggerize-express/node_modules/swaggerize-routes/node_modules/enjoi/node_modules/joi/lib/errors.js:140:17)
at internals.Any.validate (/Users/skin/Projects/activeProjects/0123_ST_STProjects/smt-account-management/node_modules/swaggerize-express/node_modules/swaggerize-routes/node_modules/enjoi/node_modules/joi/lib/any.js:667:25)
at validateParameter (/Users/skin/Projects/activeProjects/0123_ST_STProjects/smt-account-management/node_modules/swaggerize-express/node_modules/swaggerize-routes/lib/validator.js:107:28)
at validateInput (/Users/skin/Projects/activeProjects/0123_ST_STProjects/smt-account-management/node_modules/swaggerize-express/lib/expressroutes.js:82:9)

I have inspected request and it is definitely as it should be and is definitely reaching express.

This if block in question in expressroutes.js at present is as below.

if (param.in === 'body') {
    return {
        get: function(req) {
            return req.body;
        },
        set: function(req, key, val) {
            req.body = val;
        }
    };
}

How ever when inspection of the code believe it should be...

    if (param.in === 'body') {
    return {
        get: function(req, key) {
            return req.body[key];
        },
        set: function(req, key, val) {
            req.body[key] = val;
        }
    };
}

When this patch is applied everything works as expected.

issue.txt

Please could you confirm that this finding is correct or that I am reading this wrong.

Gary

jsdevel commented 8 years ago

You should only have one body parameter defined, and in your case it should be type object.

skindc commented 8 years ago

Yes, sorry I see this in the specification now. Sorry to have wasted your time. Thanks