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

Fix halpager inlining issues #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 11 additions & 8 deletions Serializer/EventSubscriber/EmbedderEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ public function getOnPostSerializeData(Event $event)
{
$object = $event->getObject();
$context = $event->getContext();
$metadataStack = $context->getMetadataStack();
$visitingStack = $context->getVisitingStack();

$relationsContent = $this->contentFactory->create($object);

Expand Down Expand Up @@ -137,12 +135,7 @@ public function getOnPostSerializeData(Event $event)

$parentObjectInlining = $this->metadataHelper->getParentObjectInlining($object, $context);
if (null !== $parentObjectInlining) {
if ($this->deferredEmbeds->contains($parentObjectInlining)) {
$relationsData = array_merge($this->deferredEmbeds->offsetGet($parentObjectInlining), $relationsData);
}

// We need to defer the links serialization to the $parentObject
$this->deferredEmbeds->attach($parentObjectInlining, $relationsData);
$this->defer($parentObjectInlining, $relationsData);

return null;
}
Expand Down Expand Up @@ -188,4 +181,14 @@ protected function getContentType(RelationContentMetadataInterface $relationCont

return $this->typeParser->parse($relationContentMetadata->getSerializerType());
}

public function defer($parentObjectInlining, $relationsData)
{
if ($this->deferredEmbeds->contains($parentObjectInlining)) {
$relationsData = array_merge($this->deferredEmbeds->offsetGet($parentObjectInlining), $relationsData);
}

// We need to defer the links serialization to the $parentObject
$this->deferredEmbeds->attach($parentObjectInlining, $relationsData);
}
}
2 changes: 0 additions & 2 deletions Serializer/EventSubscriber/LinkEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ public function getOnPostSerializeData(Event $event)
{
$object = $event->getObject();
$context = $event->getContext();
$metadataStack = $context->getMetadataStack();
$visitingStack = $context->getVisitingStack();

$links = $this->linkFactory->createLinks($object);
if ($this->deferredLinks->contains($object)) {
Expand Down
25 changes: 16 additions & 9 deletions Serializer/Handler/HalPagerfantaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,28 @@ public function serializeToArray(GenericSerializationVisitor $visitor, HalPagerf
'total' => $pager->getNbResults(),
);

if (null !== ($links = $this->linkEventSubscriber->getOnPostSerializeData(new ObjectEvent($context, $pager, $type)))) {
$data[$this->linksJsonKey] = $links;
}

if (null !== ($relations = $this->embedderEventSubscriber->getOnPostSerializeData(new ObjectEvent($context, $pager, $type)))) {
$data[$this->relationsJsonKey] = $relations;
}

$resultsType = array(
'name' => 'array',
);
if (isset($type['params'])) {
$resultsType['params'] = $type['params'];
}
$data[$this->relationsJsonKey][$halPager->getRel()] = $visitor->getNavigator()->accept($pager->getCurrentPageResults(), $resultsType, $context);

// make sure the pager links/embeds are deffered to the halpager
$this->linkEventSubscriber->getOnPostSerializeData(new ObjectEvent($context, $pager, $type));
$this->embedderEventSubscriber->getOnPostSerializeData(new ObjectEvent($context, $pager, $type));

$this->embedderEventSubscriber->defer($halPager, array(
$halPager->getRel() => $visitor->getNavigator()->accept($pager->getCurrentPageResults(), $resultsType, $context),
));

if (null !== ($links = $this->linkEventSubscriber->getOnPostSerializeData(new ObjectEvent($context, $halPager, $type)))) {
$data[$this->linksJsonKey] = $links;
}

if (null !== ($relations = $this->embedderEventSubscriber->getOnPostSerializeData(new ObjectEvent($context, $halPager, $type)))) {
$data[$this->relationsJsonKey] = $relations;
}

if ($shouldSetRoot) {
$visitor->setRoot($data);
Expand Down
17 changes: 15 additions & 2 deletions Serializer/MetadataHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ public function getParentObjectInlining($object, SerializationContext $context)
}

if (
$metadataStack->count() > 0 && $metadataStack[0]->inline
&& $this->serializerMetadataFactory->getMetadataForClass(get_class($parentObject)) === $metadataStack[1]
(
$metadataStack->count() > 0 && $metadataStack[0]->inline
&& $this->serializerMetadataFactory->getMetadataForClass(get_class($parentObject)) === $metadataStack[1]
)
|| (
$this->isSubclassOf(get_class($object), 'Pagerfanta\Pagerfanta')
&& $this->isSubclassOf(get_class($parentObject), 'FSC\HateoasBundle\Model\HalPagerfanta')
)
) {
return $parentObject;
}
Expand All @@ -43,4 +49,11 @@ public function getXmlRootName($object)

return $classMetadata->xmlRootName;
}

private function isSubclassOf($class1, $class2)
{
$class = new \ReflectionClass($class1);

return $class1 === $class2 || $class->isSubclassOf($class2);
}
}
91 changes: 86 additions & 5 deletions Tests/Functional/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function testHalPagerInXML()
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals(<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<collection page="1" limit="10" total="2">
<posts page="1" limit="10" total="2" id="1">
<link rel="self" href="http://localhost/api/pager?_format=xml&amp;limit=10&amp;page=1"/>
<link rel="first" href="http://localhost/api/pager?_format=xml&amp;limit=10&amp;page=1"/>
<link rel="last" href="http://localhost/api/pager?_format=xml&amp;limit=10&amp;page=1"/>
Expand All @@ -148,7 +148,35 @@ public function testHalPagerInXML()
<entry>
<entry><![CDATA[value]]></entry>
</entry>
</collection>
<first_name><![CDATA[Adrien]]></first_name>
<last_name><![CDATA[Brault]]></last_name>
<link rel="self" href="http://localhost/api/users/1"/>
<link rel="alternate" href="http://localhost/profile/1"/>
<link rel="users" href="http://localhost/api/users"/>
<link rel="last-post" href="http://localhost/api/users/1/last-post"/>
<link rel="posts" href="http://localhost/api/users/1/posts"/>
<link rel="alternate" href="http://localhost/api/users/1/alternate"/>
<link rel="dynamic_href" href="this/is/a/href/from/a/property_path"/>
<post rel="last-post" id="2">
<title><![CDATA[How to create awesome symfony2 application]]></title>
<link rel="self" href="http://localhost/api/posts/2"/>
</post>
<collection rel="posts" page="1" limit="1" total="2">
<link rel="self" href="http://localhost/api/users/1/posts?limit=1&amp;page=1"/>
<link rel="first" href="http://localhost/api/users/1/posts?limit=1&amp;page=1"/>
<link rel="last" href="http://localhost/api/users/1/posts?limit=1&amp;page=2"/>
<link rel="next" href="http://localhost/api/users/1/posts?limit=1&amp;page=2"/>
<post id="2">
<title><![CDATA[How to create awesome symfony2 application]]></title>
<link rel="self" href="http://localhost/api/posts/2"/>
</post>
</collection>
<link rel="create" href="http://localhost/api/posts/create"/>
<form rel="create" method="POST" action="http://localhost/api/posts">
<link rel="self" href="http://localhost/api/posts/create"/>
<input type="text" name="post[title]" required="required"/>
</form>
</posts>

XML
, $response->getContent());
Expand All @@ -166,16 +194,69 @@ public function testHalPager()
"page":1,
"limit":10,
"total":2,
"id":1,
"first_name":"Adrien",
"last_name":"Brault",
"_links":{
"self":{"href":"http:\/\/localhost\/api\/pager?_format=json&limit=10&page=1"},
"self":[
{"href":"http:\/\/localhost\/api\/pager?_format=json&limit=10&page=1"},
{"href":"http:\/\/localhost\/api\/users\/1"}
],
"first":{"href":"http:\/\/localhost\/api\/pager?_format=json&limit=10&page=1"},
"last":{"href":"http:\/\/localhost\/api\/pager?_format=json&limit=10&page=1"}
"last":{"href":"http:\/\/localhost\/api\/pager?_format=json&limit=10&page=1"},
"alternate":[
{"href":"http:\/\/localhost\/profile\/1"},
{"href":"http:\/\/localhost\/api\/users\/1\/alternate"}
],
"users":{
"href":"http:\/\/localhost\/api\/users"
},
"last-post":{
"href":"http:\/\/localhost\/api\/users\/1\/last-post"
},
"posts":{
"href":"http:\/\/localhost\/api\/users\/1\/posts"
},
"dynamic_href":{
"href":"this\/is\/a\/href\/from\/a\/property_path"
},
"create":{
"href":"http:\/\/localhost\/api\/posts\/create"
}
},
"_embedded":{
"test-rel":[
{"first":"value"},
{"second":"value"}
]
],
"last-post":{
"id":2,
"title":"How to create awesome symfony2 application",
"_links":{
"self":{"href":"http:\/\/localhost\/api\/posts\/2"}
}
},
"posts":{
"page":1,
"limit":1,
"total":2,
"results":[
{
"id":2,
"title":"How to create awesome symfony2 application",
"_links":{
"self":{"href":"http:\/\/localhost\/api\/posts\/2"}
}
}
],
"_links":{
"self":{"href":"http:\/\/localhost\/api\/users\/1\/posts?limit=1&page=1"},
"first":{"href":"http:\/\/localhost\/api\/users\/1\/posts?limit=1&page=1"},
"last":{"href":"http:\/\/localhost\/api\/users\/1\/posts?limit=1&page=2"},
"next":{"href":"http:\/\/localhost\/api\/users\/1\/posts?limit=1&page=2"}
}
},
"create":null
}
}
JSON;
Expand Down
8 changes: 7 additions & 1 deletion Tests/Functional/TestBundle/Controller/MixedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FSC\HateoasBundle\Tests\Functional\TestBundle\Controller;

use FSC\HateoasBundle\Tests\Functional\TestBundle\Model\PostsCollection;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -35,6 +36,11 @@ public function pagerAction(Request $request)

$halPager = HalPagerfanta::create($pager, 'test-rel');

return new Response($this->get('serializer')->serialize($halPager, $request->get('_format')));
$postsCollection = new PostsCollection(
$halPager,
$this->get('test.provider.user')->getUser(1)
);

return new Response($this->get('serializer')->serialize($postsCollection, $request->get('_format')));
}
}