Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Resources/config/view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
<argument>%fos_rest.serialize_null%</argument>
<argument>%fos_rest.force_redirects%</argument>
<argument>%fos_rest.default_engine%</argument>
<call method="setExclusionStrategyGroups">
<argument>%fos_rest.serializer.exclusion_strategy.groups%</argument>
</call>
<call method="setExclusionStrategyVersion">
<argument>%fos_rest.serializer.exclusion_strategy.version%</argument>
</call>
<call method="setSerializeNull">
<argument>%fos_rest.serializer.serialize_null%</argument>
</call>
<call method="setContainer">
<argument type="service" id="service_container" />
</call>
Expand Down
50 changes: 21 additions & 29 deletions Tests/View/ViewHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static function createResponseWithLocationDataProvider()
public function testCreateResponseWithLocationAndData()
{
$testValue = array('naviter' => 'oudie');
$container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get', 'getParameter'));
$container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get'));
$this->setupMockedSerializer($container, $testValue);

$viewHandler = new ViewHandler(array('json' => false));
Expand Down Expand Up @@ -203,9 +203,6 @@ public function testShouldReturnErrorResponseWhenDataContainsFormAndFormIsNotVal

$container->set('fos_rest.serializer', $serializer);
$container->set('fos_rest.view.exception_wrapper_handler', new ExceptionWrapperHandler());
$container->setParameter('fos_rest.serializer.exclusion_strategy.groups', 'foo');
$container->setParameter('fos_rest.serializer.exclusion_strategy.version', '1.0');
$container->setParameter('fos_rest.serializer.serialize_null', false);

//test
$viewHandler = new ViewHandler(null, $expectedFailedValidationCode = Codes::HTTP_I_AM_A_TEAPOT);
Expand Down Expand Up @@ -238,7 +235,7 @@ public function testCreateResponseWithoutLocation($format, $expected, $createVie
{
$viewHandler = new ViewHandler(array('html' => true, 'json' => false));

$container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get', 'getParameter'));
$container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get'));
if ('html' === $format) {
$templating = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\PhpEngine')
->setMethods(array('render'))
Expand Down Expand Up @@ -319,17 +316,6 @@ function ($method) use ($serializer) {
}
)
);

$map = array(
array('fos_rest.serializer.exclusion_strategy.groups', 'foo'),
array('fos_rest.serializer.exclusion_strategy.version', '1.0'),
array('fos_rest.serializer.serialize_null', false)
);

$container
->expects($this->any())
->method('getParameter')
->will($this->returnValueMap($map));
}

public static function createResponseWithoutLocationDataProvider()
Expand All @@ -348,7 +334,7 @@ public static function createResponseWithoutLocationDataProvider()
public function testSerializeNull($expected, $serializeNull)
{
$viewHandler = new ViewHandler(array('json' => false), 404, 200, $serializeNull);
$container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get', 'getParameter'));
$container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get'));

$viewHandler->setContainer($container);

Expand Down Expand Up @@ -396,20 +382,11 @@ public static function createSerializeNullDataProvider()
public function testSerializeNullDataValues($expected, $serializeNull)
{
$viewHandler = new ViewHandler(array('json' => false), 404, 200);
$container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get', 'getParameter'));

$viewHandler->setContainer($container);
$viewHandler->setSerializeNullStrategy($serializeNull);

$map = array(
array('fos_rest.serializer.exclusion_strategy.groups', 'foo'),
array('fos_rest.serializer.exclusion_strategy.version', '1.0'),
array('fos_rest.serializer.serialize_null', $serializeNull)
);
$container = $this->getMock('Symfony\Component\DependencyInjection\Container', array('get'));

$container
->expects($this->any())
->method('getParameter')
->will($this->returnValueMap($map));
$viewHandler->setContainer($container);

$view = new View();
$context = $viewHandler->getSerializationContext($view);
Expand Down Expand Up @@ -549,4 +526,19 @@ public function prepareTemplateParametersDataProvider()
'form is wrapped as form key' => array($form, array('form' => $formView, 'data' => $formView))
);
}

public function testConfigurableViewHandlerInterface()
{
//test
$viewHandler = new ViewHandler();
$viewHandler->setExclusionStrategyGroups('bar');
$viewHandler->setExclusionStrategyVersion('1.1');
$viewHandler->setSerializeNullStrategy(true);

$view = new View();
$context = $viewHandler->getSerializationContext($view);
$this->assertEquals(array('bar'), $context->attributes->get('groups')->getOrThrow(new \Exception('Serialization groups not set as expected')));
$this->assertEquals('1.1', $context->attributes->get('version')->getOrThrow(new \Exception('Serialization version not set as expected')));
$this->assertTrue($context->shouldSerializeNull());
}
}
41 changes: 41 additions & 0 deletions View/ConfigurableViewHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\RestBundle\View;

/**
* Specialized ViewInterface that allows dynamic configuration of JMS serializer context aspects
*
* @author Lukas K. Smith <smith@pooteeweet.org>
*/
interface ConfigurableViewHandlerInterface extends ViewHandlerInterface
{
/**
* Set the default serialization groups
*
* @param array $groups
*/
public function setExclusionStrategyGroups($groups);

/**
* Set the default serialization version
*
* @param string $version
*/
public function setExclusionStrategyVersion($version);

/**
* If nulls should be serialized
*
* @param Boolean $isEnabled
*/
public function setSerializeNullStrategy($isEnabled);
}
67 changes: 52 additions & 15 deletions View/ViewHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;

use FOS\RestBundle\Util\Codes;
use FOS\RestBundle\Util\ExceptionWrapper;

/**
* View may be used in controllers to build up a response in a format agnostic way
Expand All @@ -33,7 +32,7 @@
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Lukas K. Smith <smith@pooteeweet.org>
*/
class ViewHandler extends ContainerAware implements ViewHandlerInterface
class ViewHandler extends ContainerAware implements ConfigurableViewHandlerInterface
{
/**
* @var array key format, value a callable that returns a Response instance
Expand Down Expand Up @@ -70,6 +69,21 @@ class ViewHandler extends ContainerAware implements ViewHandlerInterface
*/
protected $defaultEngine;

/**
* @var array
*/
protected $exclusionStrategyGroups = array();

/**
* @var string
*/
protected $exclusionStrategyVersion;

/**
* @var Boolean
*/
protected $serializeNullStrategy;

/**
* Constructor
*
Expand All @@ -96,6 +110,36 @@ public function __construct(
$this->defaultEngine = $defaultEngine;
}

/**
* Set the default serialization groups
*
* @param array $groups
*/
public function setExclusionStrategyGroups($groups)
{
$this->exclusionStrategyGroups = (array) $groups;
}

/**
* Set the default serialization version
*
* @param string $version
*/
public function setExclusionStrategyVersion($version)
{
$this->exclusionStrategyVersion = $version;
}

/**
* If nulls should be serialized
*
* @param Boolean $isEnabled
*/
public function setSerializeNullStrategy($isEnabled)
{
$this->serializeNullStrategy = $isEnabled;
}

/**
* Verifies whether the given format is supported by this view
*
Expand Down Expand Up @@ -200,23 +244,16 @@ public function getSerializationContext(View $view)
{
$context = $view->getSerializationContext();

if ($context->attributes->get('groups')->isEmpty()) {
$groups = $this->container->getParameter('fos_rest.serializer.exclusion_strategy.groups');
if ($groups) {
$context->setGroups($groups);
}
if ($context->attributes->get('groups')->isEmpty() && $this->exclusionStrategyGroups) {
$context->setGroups($this->exclusionStrategyGroups);
}

if ($context->attributes->get('version')->isEmpty()) {
$version = $this->container->getParameter('fos_rest.serializer.exclusion_strategy.version');
if ($version) {
$context->setVersion($version);
}
if ($context->attributes->get('version')->isEmpty() && $this->exclusionStrategyVersion) {
$context->setVersion($this->exclusionStrategyVersion);
}

if (null === $context->shouldSerializeNull()) {
$serializeNull = $this->container->getParameter('fos_rest.serializer.serialize_null');
$context->setSerializeNull($serializeNull);
if (null === $context->shouldSerializeNull() && null !== $this->serializeNullStrategy) {
$context->setSerializeNull($this->serializeNullStrategy);
}

return $context;
Expand Down