Skip to content

Commit b985d75

Browse files
BlairJBlairJ
BlairJ
authored and
BlairJ
committed
Fixes #16 JsonApi meta data should notbe serialized
1 parent 184ad73 commit b985d75

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

src/JsonApiSerializer.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import JsonApi = require('./JsonApi');
99
//var jsdata: Function = require('js-data');
1010

1111

12-
const JSONAPI_META: string = '$_JSONAPIMETA_';
12+
export const JSONAPI_META: string = '$_JSONAPIMETA_';
1313
const jsonApiContentType: string = 'application/vnd.api+json';
1414
export const JSONAPI_RELATED_LINK: string = 'related';
1515
export const JSONAPI_PARENT_LINK: string = 'parent';
@@ -487,7 +487,7 @@ export class JsonApiHelper {
487487
result.WithData(this.ObjectToJsonApiData(options, item));
488488
});
489489
} else {
490-
// Not sure this is really necessary could just always send an array ?
490+
// JsonAPI single object
491491
result.data = <any>this.ObjectToJsonApiData(options, contents);
492492
}
493493
return result;
@@ -876,7 +876,8 @@ export class JsonApiHelper {
876876

877877
for (var prop in contents) {
878878
// Skip id attribute as it has already been copied to the id field out side of the attributes collection
879-
if (prop === options.idAttribute) {
879+
// Todo skip any non-json api compliant tags
880+
if (prop === options.idAttribute || prop === JSONAPI_META || prop.indexOf('$') >= 0 ) {
880881
continue;
881882
}
882883

@@ -968,14 +969,16 @@ export class JsonApiHelper {
968969

969970
//Get each child relationship
970971
for (var relationName in data.relationships) {
971-
972972
if (data.relationships[relationName]) {
973973

974974
// Relation name should be the local field name of a relationship.
975975
var relationship = data.relationships[relationName];
976976

977977
// Data is truthy and is not an array or if an array is not empty
978-
var hasData = (relationship.data && (!DSUTILS.isArray(relationship.data) || (DSUTILS.isArray(relationship.data) && (<JsonApi.JsonApiData[]>relationship.data).length > 0)));
978+
var hasData = (relationship.data && (
979+
!DSUTILS.isArray(relationship.data) ||
980+
(DSUTILS.isArray(relationship.data) && (<JsonApi.JsonApiData[]>relationship.data).length > 0))
981+
);
979982

980983
if (hasData) {
981984
var joinTableFactory = null;

test/jsonApiSerializationSpec.js

+55
Original file line numberDiff line numberDiff line change
@@ -383,4 +383,59 @@
383383

384384
});
385385
});
386+
387+
describe('Meta Data', function () {
388+
var ds;
389+
var test = { config: {} };
390+
391+
beforeEach(function () {
392+
ds = new JSData.DS();
393+
var dsHttpAdapter = new DSJsonApiAdapter.JsonApiAdapter({
394+
queryTransform: queryTransform
395+
});
396+
ds.registerAdapter('jsonApi', dsHttpAdapter, { default: true });
397+
398+
test.config.User = ds.defineResource({
399+
name: 'user',
400+
idAttribute: 'id'
401+
});
402+
403+
test.jsonApiData = new DSJsonApiAdapter.JsonApi.JsonApiRequest();
404+
test.jsonApiData.WithData(
405+
new DSJsonApiAdapter.JsonApi.JsonApiData('user')
406+
.WithId('2')
407+
.WithAttribute('author', 'John')
408+
.WithAttribute('age', 31));
409+
410+
test.model = [{ Id: '2', author: 'John', age: 31 }]; //ISMODEL: true, type: 'testuser'
411+
});
412+
413+
it('should not serialize meta data', function () {
414+
var _this = this;
415+
416+
setTimeout(function () {
417+
assert.equal(1, _this.requests.length);
418+
_this.requests[0].respond(200, { 'Content-Type': 'application/vnd.api+json' }, JSON.stringify(test.jsonApiData));
419+
}, 30);
420+
421+
return test.config.User.create({ author: 'John', age: 32 }).then(function (data) {
422+
var user = test.config.User.get(2);
423+
var meta = DSJsonApiAdapter.TryGetMetaData(user);
424+
assert.isDefined(user, 'user should be in the store');
425+
assert.isDefined(meta, 'user should have meta data in the store');
426+
427+
setTimeout(function () {
428+
assert.equal(2, _this.requests.length, 'should make second request');
429+
430+
var request = JSON.parse(_this.requests[1].requestBody);
431+
assert.isUndefined( request.data.attributes['$_JSONAPIMETA_'], 'should not send meta data');
432+
433+
_this.requests[1].respond(200, { 'Content-Type': 'application/vnd.api+json' }, _this.requests[1].requestBody);
434+
}, 30);
435+
436+
user.name = 'bob';
437+
return test.config.User.save(user.id);
438+
});
439+
});
440+
});
386441
});

0 commit comments

Comments
 (0)