-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Add Support of recursive denormalization on object_to_populate #30607
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
namespace Symfony\Component\Serializer\Normalizer; | ||
|
||
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException; | ||
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; | ||
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; | ||
use Symfony\Component\PropertyInfo\Type; | ||
use Symfony\Component\Serializer\Encoder\JsonEncoder; | ||
|
@@ -38,6 +39,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer | |
const SKIP_NULL_VALUES = 'skip_null_values'; | ||
const MAX_DEPTH_HANDLER = 'max_depth_handler'; | ||
const EXCLUDE_FROM_CACHE_KEY = 'exclude_from_cache_key'; | ||
const DEEP_OBJECT_TO_POPULATE = 'deep_object_to_populate'; | ||
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. in #30888 i am adding phpdoc to these configuration constants to explain what they do and what values they accept. 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. as this is already merged, i will rebase #30888 and document it there. and extract the test for this into a trait like we do the other tests there. |
||
|
||
private $propertyTypeExtractor; | ||
private $typesCache = []; | ||
|
@@ -274,6 +276,13 @@ public function denormalize($data, $class, $format = null, array $context = []) | |
continue; | ||
} | ||
|
||
if ($context[self::DEEP_OBJECT_TO_POPULATE] ?? $this->defaultContext[self::DEEP_OBJECT_TO_POPULATE] ?? false) { | ||
try { | ||
$context[self::OBJECT_TO_POPULATE] = $this->getAttributeValue($object, $attribute, $format, $context); | ||
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. this overwrites the context. i think this means that if there are more loops, you might have a previous object to populate in the context. i think we should unset OBJECT_TO_POPULATE if it does not find anything and when the DEEP_OBJECT_TO_POPULATE flag is not true. 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. we found where OBJECT_TO_POPULATE is unset. so this is fine as is. i will try to do a test and fix to make OBJECT_TO_POPULATE more robust, as i think there are edge cases that would lead to weird behaviour. 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. => #30977 |
||
} catch (NoSuchPropertyException $e) { | ||
} | ||
} | ||
|
||
$value = $this->validateAndDenormalize($class, $attribute, $value, $format, $context); | ||
try { | ||
$this->setAttributeValue($object, $attribute, $value, $format, $context); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?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\Tests\Fixtures; | ||
|
||
/** | ||
* @author Jérôme Desjardin <jewome62@gmail.com> | ||
*/ | ||
class DeepObjectPopulateChildDummy | ||
{ | ||
public $foo; | ||
|
||
public $bar; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?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\Tests\Fixtures; | ||
|
||
/** | ||
* @author Jérôme Desjardin <jewome62@gmail.com> | ||
*/ | ||
class DeepObjectPopulateParentDummy | ||
{ | ||
/** | ||
* @var DeepObjectPopulateChildDummy|null | ||
*/ | ||
private $child; | ||
|
||
public function setChild(?DeepObjectPopulateChildDummy $child) | ||
{ | ||
$this->child = $child; | ||
} | ||
|
||
public function getChild(): ?DeepObjectPopulateChildDummy | ||
{ | ||
return $this->child; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.