Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More Specific Ancestry Check #248

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
32 changes: 26 additions & 6 deletions browserify/jsonapi-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = function (jsonapi, data, opts) {
if (included) {
// To prevent circular references, check if the record type
// has already been processed in this thread
if (ancestry.indexOf(included.type) > -1) {
if (ancestry.indexOf(included.type + included.id) > -1) {
return Promise
.all([extractAttributes(included)])
.then(function (results) {
Expand All @@ -48,11 +48,19 @@ module.exports = function (jsonapi, data, opts) {
}

return Promise
.all([extractAttributes(included), extractRelationships(included, ancestry + ':' + included.type + included.id)])
.all([extractAttributes(included), extractRelationships(included, ancestry + ':' + included.type + included.id), extractLinks(included)])
.then(function (results) {
var attributes = results[0];
var relationships = results[1];
resolve(_extend(attributes, relationships));
var links = results[2];
const record = _extend(attributes, relationships);

// Links
if (links) {
record.links = links;
}

resolve(record)
});
} else {
return resolve(null);
Expand Down Expand Up @@ -145,17 +153,29 @@ module.exports = function (jsonapi, data, opts) {
});
}

function extractLinks(from) {
if (from.links) {
return from.links;
}
if (jsonapi.links) {
return jsonapi.links;
}
return undefined;
}

this.perform = function () {
return Promise
.all([extractAttributes(data), extractRelationships(data, data.type + data.id)])
.all([extractAttributes(data), extractRelationships(data, data.type + data.id), extractLinks(data)])
.then(function (results) {
var attributes = results[0];
var relationships = results[1];
var links = results[2];

var record = _extend(attributes, relationships);

// Links
if (jsonapi.links) {
record.links = jsonapi.links;
if (links) {
record.links = links;
}

// If option is present, transform record
Expand Down
32 changes: 26 additions & 6 deletions lib/deserializer-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = function (jsonapi, data, opts) {
if (included) {
// To prevent circular references, check if the record type
// has already been processed in this thread
if (ancestry.indexOf(included.type) > -1) {
if (ancestry.indexOf(included.type + included.id) > -1) {
return Promise
.all([extractAttributes(included)])
.then(function (results) {
Expand All @@ -47,11 +47,19 @@ module.exports = function (jsonapi, data, opts) {
}

return Promise
.all([extractAttributes(included), extractRelationships(included, ancestry + ':' + included.type + included.id)])
.all([extractAttributes(included), extractRelationships(included, ancestry + ':' + included.type + included.id), extractLinks(included)])
.then(function (results) {
var attributes = results[0];
var relationships = results[1];
resolve(_extend(attributes, relationships));
var links = results[2];
const record = _extend(attributes, relationships);

// Links
if (links) {
record.links = links;
}

resolve(record)
});
} else {
return resolve(null);
Expand Down Expand Up @@ -144,17 +152,29 @@ module.exports = function (jsonapi, data, opts) {
});
}

function extractLinks(from) {
if (from.links) {
return from.links;
}
if (jsonapi.links) {
return jsonapi.links;
}
return undefined;
}

this.perform = function () {
return Promise
.all([extractAttributes(data), extractRelationships(data, data.type + data.id)])
.all([extractAttributes(data), extractRelationships(data, data.type + data.id), extractLinks(data)])
.then(function (results) {
var attributes = results[0];
var relationships = results[1];
var links = results[2];

var record = _extend(attributes, relationships);

// Links
if (jsonapi.links) {
record.links = jsonapi.links;
if (links) {
record.links = links;
}

// If option is present, transform record
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "jsonapi-serializer",
"version": "3.6.7",
"name": "swards-jsonapi-serializer",
"version": "3.6.10",
"description": "A Node.js framework agnostic library for serializing your data to JSON API",
"main": "index.js",
"scripts": {
"test": "./node_modules/mocha/bin/mocha"
},
"author": "Sandro Munda <[email protected]>",
"license": "MIT",
"repository": "SeyZ/jsonapi-serializer",
"repository": "swards/jsonapi-serializer",
"engines": {
"node": ">=0.12"
},
Expand Down
204 changes: 122 additions & 82 deletions test/deserializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,88 +616,128 @@ describe('JSON API Deserializer', function () {
expect(json).to.be.an('object');

expect(json).to.be.be.eql({
name: 'Twin Pines Mall',
id: '1',
stores: [
{
name: 'Tasty Food',
id: '1',
deals: [
{
name: 'Free Drink with Snack Purchase',
id: '1',
stores: [
{ name: 'Tasty Food', id: '1' }
]
}, {
name: 'Free Samples of New Delicious Treat',
id: '2',
stores: [
{ name: 'Tasty Food', id: '1' }
]
}
]
}, {
name: 'Fashionable Clothes',
id: '2',
deals: [
{
name: 'Buy One Get One Off Shirts',
id: '3',
stores: [
{ name: 'Fashionable Clothes', id: '2' }
]
}
]
}, {
name: 'Readable Books',
id: '3'
}
],
deals: [
{
name: 'Free Drink with Snack Purchase',
id: '1',
stores: [
{
name: 'Tasty Food',
id: '1',
deals: [
{ name: 'Free Drink with Snack Purchase', id: '1' },
{ name: 'Free Samples of New Delicious Treat', id: '2' }
]
}
]
},
{
name: 'Free Samples of New Delicious Treat',
id: '2',
stores: [
{
name: 'Tasty Food',
id: '1',
deals: [
{ name: 'Free Drink with Snack Purchase', id: '1' },
{ name: 'Free Samples of New Delicious Treat', id: '2' }
]
}
]
},
{
name: 'Buy One Get One Off Shirts',
id: '3',
stores: [
{
name: 'Fashionable Clothes',
id: '2',
deals: [
{ name: 'Buy One Get One Off Shirts', id: '3' }
]
}
]
}
]
});
"name": "Twin Pines Mall",
"id": "1",
"stores": [
{
"name": "Tasty Food",
"id": "1",
"deals": [
{
"name": "Free Drink with Snack Purchase",
"id": "1",
"stores": [
{
"name": "Tasty Food",
"id": "1"
}
]
},
{
"name": "Free Samples of New Delicious Treat",
"id": "2",
"stores": [
{
"name": "Tasty Food",
"id": "1"
}
]
}
]
},
{
"name": "Fashionable Clothes",
"id": "2",
"deals": [
{
"name": "Buy One Get One Off Shirts",
"id": "3",
"stores": [
{
"name": "Fashionable Clothes",
"id": "2"
}
]
}
]
},
{
"name": "Readable Books",
"id": "3"
}
],
"deals": [
{
"name": "Free Drink with Snack Purchase",
"id": "1",
"stores": [
{
"name": "Tasty Food",
"id": "1",
"deals": [
{
"name": "Free Drink with Snack Purchase",
"id": "1"
},
{
"name": "Free Samples of New Delicious Treat",
"id": "2",
"stores": [
{
"name": "Tasty Food",
"id": "1"
}
]
}
]
}
]
},
{
"name": "Free Samples of New Delicious Treat",
"id": "2",
"stores": [
{
"name": "Tasty Food",
"id": "1",
"deals": [
{
"name": "Free Drink with Snack Purchase",
"id": "1",
"stores": [
{
"name": "Tasty Food",
"id": "1"
}
]
},
{
"name": "Free Samples of New Delicious Treat",
"id": "2"
}
]
}
]
},
{
"name": "Buy One Get One Off Shirts",
"id": "3",
"stores": [
{
"name": "Fashionable Clothes",
"id": "2",
"deals": [
{
"name": "Buy One Get One Off Shirts",
"id": "3"
}
]
}
]
}
]
}
);

done(null, json);
});
Expand Down