8000 [Serializer] Throw exception when extra attributes are used during an object denor… by juliendidier · Pull Request #19958 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Throw exception when extra attributes are used during an object denor… #19958

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
Dec 5, 2016
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Exception;

/**
* ExtraAttributesException.
*
* @author Julien DIDIER <julien@didier.io>
*/
class ExtraAttributesException extends RuntimeException
{
public function __construct(array $extraAttributes, \Exception $previous = null)
{
$msg = sprintf('Extra attributes are not allowed ("%s" are unknown).', implode('", "', $extraAttributes));

parent::__construct($msg, 0, $previous);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\CircularReferenceException;
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use statements reorganization should reverted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright. done.

8000
Expand Down Expand Up @@ -171,8 +172,10 @@ public function denormalize($data, $class, $format = null, array $context = arra
if (!isset($context['cache_key'])) {
$context['cache_key'] = $this->getCacheKey($format, $context);
}

$allowedAttributes = $this->getAllowedAttributes($class, $context, true);
$normalizedData = $this->prepareForDenormalization($data);
$extraAttributes = array();

$reflectionClass = new \ReflectionClass($class);
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes, $format);
Expand All @@ -183,6 +186,10 @@ public function denormalize($data, $class, $format = null, array $context = arra
}

if (($allowedAttributes !== false && !in_array($attribute, $allowedAttributes)) || !$this->isAllowedAttribute($class, $attribute, $format, $context)) {
if (isset($context['allow_extra_attributes']) && !$context['allow_extra_attributes']) {
$extraAttributes[] = $attribute;
}

continue;
}

Expand All @@ -194,6 +201,10 @@ public function denormalize($data, $class, $format = null, array $context = arra
}
}

if (!empty($extraAttributes)) {
throw new ExtraAttributesException($extraAttributes);
}

return $object;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ public function testInstantiateObjectDenormalizer()
$normalizer = new AbstractObjectNormalizerDummy();
$normalizer->instantiateObject($data, $class, $context, new \ReflectionClass($class), array());
}

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

class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
Expand Down
0