File tree Expand file tree Collapse file tree 5 files changed +110
-1
lines changed
test/integration/controllers Expand file tree Collapse file tree 5 files changed +110
-1
lines changed Original file line number Diff line number Diff line change @@ -173,8 +173,37 @@ module.exports = {
173173 return data ;
174174 } ,
175175
176+
177+ /*
178+ * Call toJSON recursively for all objects in data
179+ *
180+ * @param {Object|array } input data
181+ * @return {Object|array } data with toJSON called for every included objects
182+ */
183+ _toJSON : function ( data ) {
184+ // Empty data
185+ if ( _ . isEmpty ( data ) ) {
186+ // Return [] or null
187+ return _ . isArray ( data ) ? data : null ;
188+ }
189+
190+ // Array data
191+ if ( _ . isArray ( data ) ) {
192+ return data . map ( obj => this . _toJSON ( obj ) ) ;
193+ }
194+
195+ // Single data
196+ if ( typeof data . toJSON === 'function' ) {
197+ return data . toJSON ( ) ;
198+ }
199+
200+ return data ;
201+ } ,
202+
176203 serialize : function ( modelName , data ) {
177204
205+ data = this . _toJSON ( data ) ;
206+
178207 var returnedValue = null ;
179208 if ( ( _ . isArray ( data ) && depthOf ( data ) > 2 ) || ( _ . isObjectLike ( data ) && _ . isArray ( data ) === false && depthOf ( data ) > 1 ) ) {
180209 returnedValue = Serializer . serialize ( modelName , data ) ;
Original file line number Diff line number Diff line change 11{
22 "name" : " sails-json-api-blueprints" ,
3- "version" : " 0.11.5 " ,
3+ "version" : " 0.11.6 " ,
44 "description" : " Blueprints to turn a Sails.js API into a JSON API" ,
55 "main" : " index.js" ,
66 "scripts" : {
Original file line number Diff line number Diff line change 1+ /**
2+ * JsonController
3+ *
4+ * @description :: Server-side logic for managing jsons
5+ * @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
6+ */
7+
8+ module . exports = {
9+
10+ } ;
11+
Original file line number Diff line number Diff line change 1+ /**
2+ * Json.js
3+ *
4+ * @description :: TODO: You might write a short summary of how this model works and what it represents here.
5+ * @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
6+ */
7+
8+ module . exports = {
9+
10+ attributes : {
11+ variable : {
12+ type : 'string'
13+ } ,
14+ invisible : {
15+ type : 'string'
16+ } ,
17+
18+ toJSON : function ( ) {
19+
20+ var obj = this . toObject ( ) ;
21+ delete obj . invisible ;
22+
23+ return obj ;
24+ }
25+ } ,
26+
27+ autoCreatedAt : false ,
28+ autoUpdatedAt : false
29+ } ;
Original file line number Diff line number Diff line change 1+ var request = require ( 'supertest' ) ;
2+ var JSONAPIValidator = require ( 'jsonapi-validator' ) . Validator ;
3+
4+ validateJSONapi = function ( res ) {
5+ var validator = new JSONAPIValidator ( ) ;
6+
7+ validator . validate ( res . body ) ;
8+ } ;
9+
10+ describe ( 'toJSON compatibility' , function ( ) {
11+
12+ it ( 'Should return record without the hidden field' , function ( done ) {
13+
14+ let toCreate = {
15+ 'data' : {
16+ 'attributes' : {
17+ variable : "test" ,
18+ invisible : "should not be displayed"
19+ } ,
20+ 'type' : 'jsons'
21+ }
22+ } ;
23+
24+ request ( sails . hooks . http . app )
25+ . post ( '/jsons' )
26+ . send ( toCreate )
27+ . expect ( 201 )
28+ . expect ( validateJSONapi )
29+ . expect ( {
30+ data : {
31+ id : '1' ,
32+ type : 'jsons' ,
33+ attributes : {
34+ variable : 'test'
35+ }
36+ }
37+ } )
38+ . end ( done ) ;
39+ } ) ;
40+ } ) ;
You can’t perform that action at this time.
0 commit comments