diff --git a/Resources/config/view.xml b/Resources/config/view.xml index 42bc76e3a..9c2cc1f11 100644 --- a/Resources/config/view.xml +++ b/Resources/config/view.xml @@ -23,6 +23,15 @@ %fos_rest.serialize_null% %fos_rest.force_redirects% %fos_rest.default_engine% + + %fos_rest.serializer.exclusion_strategy.groups% + + + %fos_rest.serializer.exclusion_strategy.version% + + + %fos_rest.serializer.serialize_null% + diff --git a/Tests/View/ViewHandlerTest.php b/Tests/View/ViewHandlerTest.php index 62979a353..3d2c635ca 100644 --- a/Tests/View/ViewHandlerTest.php +++ b/Tests/View/ViewHandlerTest.php @@ -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)); @@ -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); @@ -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')) @@ -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() @@ -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); @@ -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); @@ -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()); + } } diff --git a/View/ConfigurableViewHandlerInterface.php b/View/ConfigurableViewHandlerInterface.php new file mode 100644 index 000000000..6ed72e74c --- /dev/null +++ b/View/ConfigurableViewHandlerInterface.php @@ -0,0 +1,41 @@ + + * + * 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 + */ +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); +} diff --git a/View/ViewHandler.php b/View/ViewHandler.php index 07c59bb65..a022c5d15 100644 --- a/View/ViewHandler.php +++ b/View/ViewHandler.php @@ -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 @@ -33,7 +32,7 @@ * @author Jordi Boggiano * @author Lukas K. Smith */ -class ViewHandler extends ContainerAware implements ViewHandlerInterface +class ViewHandler extends ContainerAware implements ConfigurableViewHandlerInterface { /** * @var array key format, value a callable that returns a Response instance @@ -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 * @@ -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 * @@ -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;