-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Integrate the PropertyInfo Component (recursive denormalization and hardening) #17660
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,10 @@ | |
use Symfony\Component\Serializer\Exception\CircularReferenceException; | ||
use Symfony\Component\Serializer\Exception\LogicException; | ||
use Symfony\Component\Serializer\Exception\UnexpectedValueException; | ||
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; | ||
use Symfony\Component\PropertyInfo\Type; | ||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; | ||
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; | ||
|
||
/** | ||
* Base class for a normalizer dealing with objects. | ||
|
@@ -26,8 +30,16 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer | |
const ENABLE_MAX_DEPTH = 'enable_max_depth'; | ||
const DEPTH_KEY_PATTERN = 'depth_%s::%s'; | ||
|
||
private $propertyTypeExtractor; | ||
private $attributesCache = array(); | ||
|
||
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null) | ||
{ | ||
parent::__construct($classMetadataFactory, $nameConverter); | ||
|
||
$this->propertyTypeExtractor = $propertyTypeExtractor; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -76,7 +88,7 @@ public function normalize($object, $format = null, array $context = array()) | |
|
||
foreach ($stack as $attribute => $attributeValue) { | ||
if (!$this->serializer instanceof NormalizerInterface) { | ||
throw new LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $attribute)); | ||
throw new LogicException(sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer', $attribute)); | ||
} | ||
|
||
$data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $context)); | ||
|
@@ -173,12 +185,15 @@ public function denormalize($data, $class, $format = null, array $context = arra | |
$allowed = $allowedAttributes === false || in_array($attribute, $allowedAttributes); | ||
$ignored = in_array($attribute, $this->ignoredAttributes); | ||
|
||
if ($allowed && !$ignored) { | ||
try { | ||
$this->setAttributeValue($object, $attribute, $value, $format, $context); | ||
} catch (InvalidArgumentException $e) { | ||
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
if (!$allowed || $ignored) { | ||
continue; | ||
} | ||
|
||
$value = $this->validateAndDenormalize($class, $attribute, $value, $format, $context); | ||
try { | ||
$this->setAttributeValue($object, $attribute, $value, $format, $context); | ||
} catch (InvalidArgumentException $e) { | ||
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
|
@@ -210,6 +225,54 @@ protected function isAttributeToNormalize($object, $attributeName, &$context) | |
return !in_array($attributeName, $this->ignoredAttributes) && !$this->isMaxDepthReached(get_class($object), $attributeName, $context); | ||
} | ||
|
||
/** | ||
* Validates the submitted data and denormalizes it. | ||
* | ||
* @param string $currentClass | ||
* @param string $attribute | ||
* @param mixed $data | ||
* @param string|null $format | ||
* @param array $context | ||
* | ||
* @return mixed | ||
* | ||
* @throws UnexpectedValueException | ||
* @throws LogicException | ||
*/ | ||
private function validateAndDenormalize($currentClass, $attribute, $data, $format, array $context) | ||
{ | ||
if (null === $this->propertyTypeExtractor || null === $types = $this->propertyTypeExtractor->getTypes($currentClass, $attribute)){ | ||
return $data; | ||
} | ||
|
||
$expectedTypes = array(); | ||
foreach ($types as $type) { | ||
if (null === $data && $type->isNullable()) { | ||
return; | ||
} | ||
|
||
$builtinType = $type->getBuiltinType(); | ||
$class = $type->getClassName(); | ||
$expectedTypes[Type::BUILTIN_TYPE_OBJECT === $builtinType && $class ? $class : $builtinType] = true; | ||
|
||
if (Type::BUILTIN_TYPE_OBJECT === $builtinType) { | ||
if (!$this->serializer instanceof DenormalizerInterface) { | ||
throw new LogicException(sprintf('Cannot denormalize attribute "%s" for class "%s" because injected serializer is not a denormalizer', $attribute, $class)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [...] because the injected [...] |
||
} | ||
|
||
if ($this->serializer->supportsDenormalization($data, $class, $format)) { | ||
return $this->serializer->denormalize($data, $class, $format, $context); | ||
} | ||
} | ||
|
||
if (call_user_func('is_'.$builtinType, $data)) { | ||
return $data; | ||
} | ||
} | ||
|
||
throw new UnexpectedValueException(sprintf('The type of the "%s" attribute for class "%s" must be one of "%s" ("%s" given).', $attribute, $currentClass, implode('", "', array_keys($expectedTypes)), gettype($data))); | ||
} | ||
|
||
/** | ||
* Sets an attribute and apply the name converter if necessary. | ||
* | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this ever be executed (if I don't miss anything, the function will terminate above when
is_object()
evaluates totrue
, won't it?)?