From c3af397787ff298b07b11d1a0abd580a7840da35 Mon Sep 17 00:00:00 2001 From: Hanif Bali Date: Mon, 14 Sep 2015 05:09:54 +0200 Subject: [PATCH 1/2] Added support for JSON arrays Fixes the issue balderdashy/sails#2977, which sails would convert an array to an object and subsequently fail validation. --- lib/hooks/blueprints/actionUtil.js | 12 ++++++++++++ lib/hooks/blueprints/actions/create.js | 2 +- lib/hooks/pubsub/index.js | 21 ++++++++++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/hooks/blueprints/actionUtil.js b/lib/hooks/blueprints/actionUtil.js index 105b659116..cd9dc5f75e 100644 --- a/lib/hooks/blueprints/actionUtil.js +++ b/lib/hooks/blueprints/actionUtil.js @@ -270,6 +270,18 @@ var actionUtil = { throw new Error('Invalid `req.options.values.blacklist`. Should be an array of strings (parameter names.)'); } + //Special handling if the body is an array, does what it would do usally but on each array object. + if(_.isArray(req.body)){ + sails.log.silly("Reqest body is an array"); + var values = []; + _.each(req.body, function(element, key){ + values[key] = mergeDefaults(element, _.omit(req.options.values, 'blacklist')); + values[key] = _.omit(values[key], blacklist || []); + values[key] = _.omit(values[key], function (p){ if (_.isUndefined(p)) return true; }); + }); + return values; + } + // Merge params into req.options.values, omitting the blacklist. var values = mergeDefaults(req.params.all(), _.omit(req.options.values, 'blacklist')); diff --git a/lib/hooks/blueprints/actions/create.js b/lib/hooks/blueprints/actions/create.js index 3f0dc74ea5..ef0f97922d 100644 --- a/lib/hooks/blueprints/actions/create.js +++ b/lib/hooks/blueprints/actions/create.js @@ -40,7 +40,7 @@ module.exports = function createRecord (req, res) { Model.subscribe(req, newInstance); Model.introduce(newInstance); } - Model.publishCreate(newInstance.toJSON(), !req.options.mirror && req); + Model.publishCreate(newInstance, !req.options.mirror && req); } // Send JSONP-friendly response if it's supported diff --git a/lib/hooks/pubsub/index.js b/lib/hooks/pubsub/index.js index c87cb3bcc6..8edaa8cd26 100644 --- a/lib/hooks/pubsub/index.js +++ b/lib/hooks/pubsub/index.js @@ -1077,7 +1077,26 @@ module.exports = function(sails) { }, + /** + * Publish the creation of model or an array of models + * + * @param {[Object]|Object} models + * - the data to publish + * + * @param {Request|Socket} req - Optional request for broadcast. + * @api private + */ + publishCreate: function(models, req, options){ + var self = this; + // Pluralize so we can use this method regardless of it is an array or not + models = self.pluralize(models); + + //Publish all models + _.each(models, function(values){ + self.publishCreateSingle(values.toJSON(), req, options); + }) + }, /** * Publish the creation of a model * @@ -1088,7 +1107,7 @@ module.exports = function(sails) { * @api private */ - publishCreate: function(values, req, options) { + publishCreateSingle: function(values, req, options) { var self = this; var reverseAssociation; From eb62c9ba507d976e8171c112384cf5ac45fd6266 Mon Sep 17 00:00:00 2001 From: Hanif Bali Date: Sat, 3 Oct 2015 02:29:33 +0200 Subject: [PATCH 2/2] Added test for posting JSON arrays to create Added testing for JSON arrays --- test/integration/router.APIScaffold.test.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/integration/router.APIScaffold.test.js b/test/integration/router.APIScaffold.test.js index 0ebb5bc8c0..f374753ca7 100644 --- a/test/integration/router.APIScaffold.test.js +++ b/test/integration/router.APIScaffold.test.js @@ -183,6 +183,21 @@ describe('router :: ', function() { }); }); + describe('a post of JSON array request to /:controller/create', function() { + it('should return JSON array for a newly created instances of the test model', function (done) { + httpHelper.testRoute('post', { + url: 'empty/create', + json: true, + body: [{stuff: "bad"}, {stuff: "ghuud"}] + }, function (err, response) { + if (err) return done(new Error(err)); + + assert(response.body instanceof Array && response.body[1].stuff === "ghuud", Err.badResponse(response)); + done(); + }); + }); + }); + describe('with pluralize turned on', function() { before(function() { @@ -323,6 +338,7 @@ describe('router :: ', function() { }); + describe('`prefix` and `restPrefix` config options set together :: ', function() { before(function() { @@ -358,6 +374,7 @@ describe('router :: ', function() { }); + }); describe('API scaffold routes', function() {