Skip to content

Commit

Permalink
Merge pull request #10 from barbarah/circular-reference
Browse files Browse the repository at this point in the history
Fix circular reference error in one-relationships
  • Loading branch information
weilandia authored Jun 3, 2020
2 parents 802f3fc + f649b03 commit 8123643
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
7 changes: 4 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,12 @@ var mapRelationships = function mapRelationships(resource, included) {
})();
} else if (relData) {
var _deserializeIncluded3 = deserializeIncluded(relData, included),
_deserializeIncluded4 = _slicedToArray(_deserializeIncluded3, 1),
dRel = _deserializeIncluded4[0];
_deserializeIncluded4 = _slicedToArray(_deserializeIncluded3, 2),
dRel = _deserializeIncluded4[0],
filteredIncluded = _deserializeIncluded4[1];

if (dRel) {
deserializedRel = mapRelationships(dRel, included);
deserializedRel = mapRelationships(dRel, filteredIncluded);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/mapRelationships/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ export const mapRelationships = (resource, included) => {

if (includedRels.length) deserializedRel = includedRels;
} else if (relData) {
const [dRel] = deserializeIncluded(relData, included);
const [dRel, filteredIncluded] = deserializeIncluded(relData, included);
if (dRel) {
deserializedRel = mapRelationships(dRel, included);
deserializedRel = mapRelationships(dRel, filteredIncluded);
}
}

Expand Down
66 changes: 66 additions & 0 deletions src/mapRelationships/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,61 @@ const circularReferenceExpectedResponse = {
],
};

const circularReferenceWithOneRelationships = {
data: {
id: 1,
type: "profile",
relationships: {
favoriteMovie: {
data: {
id: "1046",
type: "movie",
},
},
},
},
included: [
{
id: "1046",
type: "movie",
relationships: {
mainActor: {
data: {
id: "93",
type: "actor",
},
},
},
},
{
id: "93",
type: "actor",
relationships: {
topMovie: {
data: {
id: "1046",
type: "movie",
},
},
},
},
],
};

const circularReferenceWithOneRelationshipsExpectedResponse = {
id: 1,
type: "profile",
favoriteMovie: {
id: "1046",
type: "movie",
mainActor: {
id: "93",
type: "actor",
topMovie: { id: "1046", type: "movie" },
}
},
};

const resourceMissingIncluded = {
data: {
id: 1,
Expand Down Expand Up @@ -293,6 +348,17 @@ describe("mapRelationships", () => {
expect(result).toEqual(circularReferenceExpectedResponse);
});

it("properly maps one-relationships with circular references in the includes", async () => {
expect.assertions(2);

const { data: resource, included } = circularReferenceWithOneRelationships;

const result = mapRelationships(resource, included);

expect(resource).not.toEqual(result);
expect(result).toEqual(circularReferenceWithOneRelationshipsExpectedResponse);
});

it("does not map unincluded relationships", async () => {
expect.assertions(2);

Expand Down

0 comments on commit 8123643

Please sign in to comment.