Skip to content

Commit 5247d44

Browse files
committed
Merge pull request #523 from fieg/serializer-max-depth
Added support for JMSSerializer MaxDepth exclusion strategy
2 parents d808b22 + 173cc14 commit 5247d44

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

Controller/Annotations/View.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class View extends Template
4040
*/
4141
protected $populateDefaultVars = true;
4242

43+
/**
44+
* @var bool
45+
*/
46+
protected $serializerEnableMaxDepthChecks;
47+
4348
/**
4449
* Returns the annotation alias name.
4550
*
@@ -118,4 +123,20 @@ public function isPopulateDefaultVars()
118123
{
119124
return $this->populateDefaultVars;
120125
}
126+
127+
/**
128+
* @param bool $serializerEnableMaxDepthChecks
129+
*/
130+
public function setSerializerEnableMaxDepthChecks($serializerEnableMaxDepthChecks)
131+
{
132+
$this->serializerEnableMaxDepthChecks = $serializerEnableMaxDepthChecks;
133+
}
134+
135+
/**
136+
* @return bool
137+
*/
138+
public function getSerializerEnableMaxDepthChecks()
139+
{
140+
return $this->serializerEnableMaxDepthChecks;
141+
}
121142
}

EventListener/ViewResponseListener.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
9696
$context->setGroups($configuration->getSerializerGroups());
9797
$view->setSerializationContext($context);
9898
}
99+
if ($configuration->getSerializerEnableMaxDepthChecks()) {
100+
$context = $view->getSerializationContext() ?: new SerializationContext();
101+
$context->enableMaxDepthChecks();
102+
$view->setSerializationContext($context);
103+
}
99104
$populateDefaultVars = $configuration->isPopulateDefaultVars();
100105
} else {
101106
$populateDefaultVars = true;

Resources/doc/3-listener-support.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,20 @@ public function getUsersAction()
141141
}
142142
```
143143

144+
Enabling the MaxDepth exclusion strategy support for the serializer can be configured as follows:
145+
146+
```php
147+
<?php
148+
149+
/**
150+
* @View(serializerEnableMaxDepthChecks=true)
151+
*/
152+
public function getUsersAction()
153+
{
154+
//...
155+
}
156+
```
157+
144158
See the following example code for more details:
145159
https://github.com/liip/LiipHelloBundle/blob/master/Controller/ExtraController.php
146160

Tests/EventListener/ViewResponseListenerTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,56 @@ public function testStatusCode($annotationCode, $viewCode, $expectedCode)
265265
$this->assertSame($expectedCode, $response->getStatusCode());
266266
}
267267

268+
public static function serializerEnableMaxDepthChecksProvider()
269+
{
270+
return array(
271+
array(false, get_class(null)),
272+
array(true, 'JMS\Serializer\Exclusion\DepthExclusionStrategy'),
273+
);
274+
}
275+
276+
/**
277+
* @dataProvider serializerEnableMaxDepthChecksProvider
278+
*/
279+
public function testSerializerEnableMaxDepthChecks($enableMaxDepthChecks, $expectedClass)
280+
{
281+
$viewAnnotation = new ViewAnnotation(array());
282+
$viewAnnotation->setSerializerEnableMaxDepthChecks($enableMaxDepthChecks);
283+
284+
$request = new Request();
285+
$request->setRequestFormat('json');
286+
$request->attributes->set('_view', $viewAnnotation);
287+
288+
$this->viewHandler = new ViewHandler(array('json' => true));
289+
$this->viewHandler->setContainer($this->container);
290+
291+
// This is why we avoid container dependencies!
292+
$that = $this;
293+
$this->container->expects($this->exactly(2))
294+
->method('get')
295+
->with($this->logicalOr('fos_rest.view_handler', 'fos_rest.templating'))
296+
->will($this->returnCallback(function ($service) use ($that) {
297+
return $service === 'fos_rest.view_handler' ?
298+
$that->viewHandler :
299+
$that->templating;
300+
}));
301+
302+
$this->templating->expects($this->any())
303+
->method('render')
304+
->will($this->returnValue('foo'));
305+
306+
$view = new View();
307+
308+
$event = $this->getResponseEvent($request, $view);
309+
310+
$this->listener->onKernelView($event);
311+
312+
$context = $view->getSerializationContext();
313+
$exclusionStrategy = $context->getExclusionStrategy();
314+
315+
$this->assertEquals($expectedClass, get_class($exclusionStrategy));
316+
}
317+
268318
public function getDataForDefaultVarsCopy()
269319
{
270320
return array(

0 commit comments

Comments
 (0)