Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Serializer] Fix extra attributes when no group specified #24816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 5, 2017
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
[Serializer] Fix extra attributes when no group specified
  • Loading branch information
ogizanagi committed Nov 5, 2017
commit d1b343c015d4e13d669fda6a54efbf6d5a267c78
12 changes: 10 additions & 2 deletions 12 src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
const OBJECT_TO_POPULATE = 'object_to_populate';
const GROUPS = 'groups';
const ATTRIBUTES = 'attributes';
const ALLOW_EXTRA_ATTRIBUTES = 'allow_extra_attributes';

/**
* @var int
Expand Down Expand Up @@ -201,7 +202,14 @@ protected function handleCircularReference($object)
*/
protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
{
if (!$this->classMetadataFactory || !isset($context[static::GROUPS]) || !is_array($context[static::GROUPS])) {
if (!$this->classMetadataFactory) {
return false;
}

$groups = false;
if (isset($context[static::GROUPS]) && is_array($context[static::GROUPS])) {
$groups = $context[static::GROUPS];
} elseif (!isset($context[static::ALLOW_EXTRA_ATTRIBUTES]) || $context[static::ALLOW_EXTRA_ATTRIBUTES]) {
return false;
}

Expand All @@ -210,7 +218,7 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu
$name = $attributeMetadata->getName();

if (
count(array_intersect($attributeMetadata->getGroups(), $context[static::GROUPS])) &&
(false === $groups || count(array_intersect($attributeMetadata->getGroups(), $groups))) &&
$this->isAllowedAttribute($classOrObject, $name, null, $context)
) {
$allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
{
const ENABLE_MAX_DEPTH = 'enable_max_depth';
const DEPTH_KEY_PATTERN = 'depth_%s::%s';
const ALLOW_EXTRA_ATTRIBUTES = 'allow_extra_attributes';

private $propertyTypeExtractor;
private $attributesCache = array();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

namespace Symfony\Component\Serializer\Tests\Normalizer;

use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;

class AbstractObjectNormalizerTest extends TestCase
Expand Down Expand Up @@ -51,6 +54,21 @@ public function testDenormalizeWithExtraAttributes()
array('allow_extra_attributes' => false)
);
}

/**
* @expectedException \Symfony\Component\Serializer\Exception\ExtraAttributesException
* @expectedExceptionMessage Extra attributes are not allowed ("fooFoo", "fooBar" are unknown).
*/
public function testDenormalizeWithExtraAttributesAndNoGroupsWithMetadataFactory()
{
$normalizer = new AbstractObjectNormalizerWithMetadata();
$normalizer->denormalize(
array('fooFoo' => 'foo', 'fooBar' => 'bar', 'bar' => 'bar'),
Dummy::class,
'any',
array('allow_extra_attributes' => false)
);
}
}

class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
Expand Down Expand Up @@ -85,3 +103,24 @@ class Dummy
public $bar;
public $baz;
}

class AbstractObjectNormalizerWithMetadata extends AbstractObjectNormalizer
{
public function __construct()
{
parent::__construct(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())));
}

protected function extractAttributes($object, $format = null, array $context = array())
{
}

protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
{
}

protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
{
$object->$attribute = $value;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.