Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

[WIP] make it possible to set both a group and a version exclusion strategy #321

Merged
merged 1 commit into from

1 participant

@lsmith77
Owner

havent really tested this so it would be good if someone could submit a test case but it should fix #282

@lsmith77
Owner

@gimler ping .. this one was for you :)

@lsmith77 lsmith77 merged commit 39f1b79 into master

1 check passed

Details default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
9 DependencyInjection/FOSRestExtension.php
@@ -58,11 +58,10 @@ public function load(array $configs, ContainerBuilder $container)
}
if (!empty($config['serializer']['version'])) {
- $container->setParameter($this->getAlias().'.serializer.exclusion_strategy.type', 'version');
- $container->setParameter($this->getAlias().'.serializer.exclusion_strategy.value', $config['serializer']['version']);
- } elseif (!empty($config['serializer']['groups'])) {
- $container->setParameter($this->getAlias().'.serializer.exclusion_strategy.type', 'groups');
- $container->setParameter($this->getAlias().'.serializer.exclusion_strategy.value', $config['serializer']['groups']);
+ $container->setParameter($this->getAlias().'.serializer.exclusion_strategy.version', $config['serializer']['version']);
+ }
+ if (!empty($config['serializer']['groups'])) {
+ $container->setParameter($this->getAlias().'.serializer.exclusion_strategy.groups', $config['serializer']['groups']);
}
$container->setParameter($this->getAlias().'.formats', $formats);
View
4 Resources/config/view.xml
@@ -5,8 +5,8 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
- <parameter key="fos_rest.serializer.exclusion_strategy.type" />
- <parameter key="fos_rest.serializer.exclusion_strategy.value"/>
+ <parameter key="fos_rest.serializer.exclusion_strategy.version" />
+ <parameter key="fos_rest.serializer.exclusion_strategy.groups"/>
</parameters>
<services>
View
11 Tests/View/ViewHandlerTest.php
@@ -150,7 +150,10 @@ public function testCreateResponseWithoutLocation($format, $expected, $createVie
->with('fos_rest.templating')
->will($this->returnValue($templating));
} else {
- $serializer = $this->getMock('\stdClass', array('serialize', 'setVersion'));
+ $serializer = $this->getMockBuilder('\JMS\SerializerBundle\Serializer\Serializer')
+ ->setMethods(array('serialize', 'setExclusionStrategy'))
+ ->disableOriginalConstructor()
+ ->getMock();
$serializer
->expects($this->once())
->method('serialize')
@@ -158,8 +161,8 @@ public function testCreateResponseWithoutLocation($format, $expected, $createVie
$serializer
->expects($this->once())
- ->method('setVersion')
- ->will($this->returnValue('1.0'));
+ ->method('setExclusionStrategy')
+ ->will($this->returnValue(null));
$container
->expects($this->once())
@@ -170,7 +173,7 @@ public function testCreateResponseWithoutLocation($format, $expected, $createVie
$container
->expects($this->any())
->method('getParameter')
- ->will($this->onConsecutiveCalls('version', '1.0'));
+ ->will($this->onConsecutiveCalls('foo', '1.0'));
}
$viewHandler->setContainer($container);
View
40 View/GroupsVersionExclusionStrategy.php
@@ -0,0 +1,40 @@
+<?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;
+
+use JMS\SerializerBundle\Metadata\ClassMetadata;
+use JMS\SerializerBundle\Metadata\PropertyMetadata;
+use JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyInterface;
+use JMS\SerializerBundle\Serializer\Exclusion\VersionExclusionStrategy;
+use JMS\SerializerBundle\Serializer\Exclusion\GroupsExclusionStrategy;
+
+class GroupsVersionExclusionStrategy implements ExclusionStrategyInterface
+{
+ private $groupExclusion;
+ private $versionExclusion;
+
+ public function __construct($groups, $version)
+ {
+ $this->groupExclusion = new GroupsExclusionStrategy((array) $groups);
+ $this->versionExclusion = new VersionExclusionStrategy($version);
+ }
+
+ public function shouldSkipClass(ClassMetadata $metadata, $object = null)
+ {
+ return false;
+ }
+
+ public function shouldSkipProperty(PropertyMetadata $metadata, $object = null)
+ {
+ return $this->groupExclusion->shouldSkipProperty($metadata) || $this->versionExclusion->shouldSkipProperty($metadata);
+ }
+}
View
19 View/View.php
@@ -174,7 +174,6 @@ public function setStatusCode($code)
public function setSerializerVersion($serializerVersion)
{
$this->serializerVersion = $serializerVersion;
- $this->serializerGroups = null;
return $this;
}
@@ -187,7 +186,6 @@ public function setSerializerVersion($serializerVersion)
public function setSerializerGroups($serializerGroups)
{
$this->serializerGroups = $serializerGroups;
- $this->serializerVersion = null;
return $this;
}
@@ -428,23 +426,6 @@ public function getSerializerGroups()
}
/**
- * get the serializer exclusion strategy
- *
- * @return string|null serializer groups
- */
- public function getSerializerExclusionStrategy()
- {
- if ($this->serializerVersion) {
- return 'version';
- }
- if ($this->serializerGroups) {
- return 'groups';
- }
-
- return null;
- }
-
- /**
* get the serializer callback
*
* @return calllable|null serializer callback
View
18 View/ViewHandler.php
@@ -12,6 +12,7 @@
namespace FOS\RestBundle\View;
use Symfony\Component\HttpFoundation\RedirectResponse;
+use JMS\SerializerBundle\Serializer\Serializer;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
@@ -160,17 +161,22 @@ protected function getRouter()
*
* @param View $view view instance from which the serializer should be configured
*
- * @return JMS\SerializerBundle\Serializer\SerializerInterface
+ * @return \JMS\SerializerBundle\Serializer\SerializerInterface
*/
protected function getSerializer(View $view = null)
{
$serializer = $this->container->get('fos_rest.serializer');
- if ($view) {
- $type = $view->getSerializerExclusionStrategy() ?: $this->container->getParameter('fos_rest.serializer.exclusion_strategy.type');
- if ($type) {
- $value = $view->{'getSerializer'.ucfirst($type)}() ?: $this->container->getParameter('fos_rest.serializer.exclusion_strategy.value');
- $serializer->{'set'.ucfirst($type)}($value);
+ if ($view && $serializer instanceof Serializer) {
+ $groups = $view->getSerializerGroups() ?: $this->container->getParameter('fos_rest.serializer.exclusion_strategy.groups');
+ $version = $view->getSerializerVersion() ?: $this->container->getParameter('fos_rest.serializer.exclusion_strategy.version');
+
+ if ($groups && $version) {
+ $serializer->setExclusionStrategy(new GroupsVersionExclusionStrategy($groups, $version));
+ } elseif ($groups) {
+ $serializer->setGroups($groups);
+ } elseif ($version) {
+ $serializer->setVersion($version);
}
$callback = $view->getSerializerCallback();
Something went wrong with that request. Please try again.