Skip to content

Commit

Permalink
Closes #35
Browse files Browse the repository at this point in the history
  • Loading branch information
neomerx committed Jun 25, 2015
1 parent 8e23219 commit 8898462
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/Document/Presenters/ElementPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ public function setRelationshipTo(
$name = $relation->getName();
$parentExists = isset($target[$parentType][$parentId]);

assert('$parentExists === true');
assert('isset($target[$parentType][$parentId][\''.Document::KEYWORD_RELATIONSHIPS.'\'][$name]) === false');

// parent object might be already fully parsed (with children) so
// - it won't exist in $target
// - it won't make any sense to parse it again (we'll got exactly the same result and it will be thrown away
// as duplicate relations/included resources are not allowed)
if ($parentExists === true) {
assert('isset($target[$parentType][$parentId][\''.Document::KEYWORD_RELATIONSHIPS.'\'][$name]) === false');

$representation = [];

if ($relation->isShowData() === true) {
Expand Down
112 changes: 112 additions & 0 deletions tests/Encoder/EncodeIncludedObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,4 +463,116 @@ public function testEncodeWithLinkWithPagination()

$this->assertEquals($expected, $actual);
}

/**
* Test encode deep duplicate hierarchies.
*
* Test for issue 35
*/
public function testEncodeDeepDuplicateHierarchies()
{
$actual = Encoder::instance([
Author::class => AuthorSchema::class,
Comment::class => CommentSchema::class,
Post::class => PostSchema::class,
Site::class => SiteSchema::class,
], $this->encoderOptions)->encode([$this->site, $this->site]);

$expected = <<<EOL
{
"data" : [{
"type" : "sites",
"id" : "2",
"attributes" : {
"name" : "site name"
},
"relationships" : {
"posts" : {
"data" : {
"type" : "posts",
"id" : "1"
}
}
},
"links" : {
"self" : "http://example.com/sites/2"
}
}, {
"type" : "sites",
"id" : "2",
"attributes" : {
"name" : "site name"
},
"relationships" : {
"posts" : {
"data" : {
"type" : "posts",
"id" : "1"
}
}
},
"links" : {
"self" : "http://example.com/sites/2"
}
}],
"included" : [{
"type" : "people",
"id" : "9",
"attributes" : {
"first_name" : "Dan",
"last_name" : "Gebhardt"
},
"relationships":{
"comments" : { "data":null }
}
}, {
"type" : "comments",
"id" : "5",
"attributes" : {
"body" : "First!"
},
"relationships" : {
"author" : {
"data" : { "type":"people", "id":"9" }
}
},
"links":{
"self" : "http://example.com/comments/5"
}
}, {
"type" : "comments",
"id" : "12",
"attributes" : {
"body" : "I like XML better"
},
"relationships":{
"author" : {
"data" : { "type":"people", "id":"9" }
}
},
"links":{
"self" : "http://example.com/comments/12"
}
}, {
"type" : "posts",
"id" : "1",
"attributes" : {
"title" : "JSON API paints my bikeshed!",
"body" : "Outside every fat man there was an even fatter man trying to close in"
},
"relationships" : {
"author" : { "data" : { "type" : "people", "id" : "9" } },
"comments" : { "data" : [
{ "type" : "comments", "id" : "5" },
{ "type" : "comments", "id" : "12" }
]}
}
}]
}
EOL;
// remove formatting from 'expected'
$expected = json_encode(json_decode($expected));

$this->assertEquals($expected, $actual);
}
}

0 comments on commit 8898462

Please sign in to comment.