bug #14540 [Serializer] Ignore \Traversable in default normalizers. (dunglas)

This PR was merged into the 2.7 branch.

Discussion
----------

[Serializer] Ignore \Traversable in default normalizers.

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #14495
| License       | MIT
| Doc PR        | n/a

Before the merge of #13500, default normalizers where never called for `\Traversable` objects. This PR restore the previous behavior in a cleaner way.
This is also convenient when dealing with Doctrine's ArrayCollection and serialization groups (see #14495).

ping @symfony/deciders

Commits
-------

c9bff46 [Serializer] Ignore \Traversable in default normalizers. Close #14495.
This commit is contained in:
Fabien Potencier 2015-05-05 03:26:23 +02:00
commit 473e3314d5
6 changed files with 18 additions and 3 deletions

View File

@ -127,7 +127,7 @@ class GetSetMethodNormalizer extends AbstractNormalizer
*/
public function supportsNormalization($data, $format = null)
{
return is_object($data) && $this->supports(get_class($data));
return is_object($data) && !$data instanceof \Traversable && $this->supports(get_class($data));
}
/**

View File

@ -43,7 +43,7 @@ class ObjectNormalizer extends AbstractNormalizer
*/
public function supportsNormalization($data, $format = null)
{
return is_object($data);
return is_object($data) && !$data instanceof \Traversable;
}
/**

View File

@ -127,7 +127,7 @@ class PropertyNormalizer extends AbstractNormalizer
*/
public function supportsNormalization($data, $format = null)
{
return is_object($data) && $this->supports(get_class($data));
return is_object($data) && !$data instanceof \Traversable && $this->supports(get_class($data));
}
/**

View File

@ -502,6 +502,11 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
$this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\PropertyDummy')
);
}
public function testNoTraversableSupport()
{
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
}
}
class GetSetDummy

View File

@ -429,6 +429,11 @@ class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
$this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\ObjectDummy')
);
}
public function testNoTraversableSupport()
{
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
}
}
class ObjectDummy

View File

@ -419,6 +419,11 @@ class PropertyNormalizerTest extends \PHPUnit_Framework_TestCase
$this->normalizer->normalize($obj, 'any');
}
public function testNoTraversableSupport()
{
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
}
}
class PropertyDummy