Skip to content

Commit

Permalink
Merge pull request #34 from dynamiccast/to-json-support
Browse files Browse the repository at this point in the history
Support sails's model.toJSON()
  • Loading branch information
dynamiccast authored Aug 17, 2016
2 parents f7e1b42 + 60d8080 commit 3870cef
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 1 deletion.
29 changes: 29 additions & 0 deletions lib/api/services/JsonApiService.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,37 @@ module.exports = {
return data;
},


/*
* Call toJSON recursively for all objects in data
*
* @param {Object|array} input data
* @return {Object|array} data with toJSON called for every included objects
*/
_toJSON: function(data) {
// Empty data
if (_.isEmpty(data)) {
// Return [] or null
return _.isArray(data) ? data : null;
}

// Array data
if (_.isArray(data)) {
return data.map(obj => this._toJSON(obj));
}

// Single data
if (typeof data.toJSON === 'function') {
return data.toJSON();
}

return data;
},

serialize: function(modelName, data) {

data = this._toJSON(data);

var returnedValue = null;
if ((_.isArray(data) && depthOf(data) > 2) || (_.isObjectLike(data) && _.isArray(data) === false && depthOf(data) > 1)) {
returnedValue = Serializer.serialize(modelName, data);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sails-json-api-blueprints",
"version": "0.11.5",
"version": "0.11.6",
"description": "Blueprints to turn a Sails.js API into a JSON API",
"main": "index.js",
"scripts": {
Expand Down
11 changes: 11 additions & 0 deletions tests/dummy/api/controllers/JsonController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* JsonController
*
* @description :: Server-side logic for managing jsons
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/

module.exports = {

};

29 changes: 29 additions & 0 deletions tests/dummy/api/models/Json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Json.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
*/

module.exports = {

attributes: {
variable: {
type: 'string'
},
invisible: {
type: 'string'
},

toJSON: function() {

var obj = this.toObject();
delete obj.invisible;

return obj;
}
},

autoCreatedAt: false,
autoUpdatedAt: false
};
40 changes: 40 additions & 0 deletions tests/dummy/test/integration/controllers/JsonController.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var request = require('supertest');
var JSONAPIValidator = require('jsonapi-validator').Validator;

validateJSONapi = function(res) {
var validator = new JSONAPIValidator();

validator.validate(res.body);
};

describe('toJSON compatibility', function() {

it('Should return record without the hidden field', function(done) {

let toCreate = {
'data': {
'attributes': {
variable: "test",
invisible: "should not be displayed"
},
'type': 'jsons'
}
};

request(sails.hooks.http.app)
.post('/jsons')
.send(toCreate)
.expect(201)
.expect(validateJSONapi)
.expect({
data: {
id: '1',
type: 'jsons',
attributes: {
variable: 'test'
}
}
})
.end(done);
});
});

0 comments on commit 3870cef

Please sign in to comment.