Skip to content

Commit dd7d761

Browse files
Merge branch '6.4' into 7.0
* 6.4: [Cache] fix the previous fix [Cache] Fix expiration time for CouchbaseCollection [FrameworkBundle] Update docblock AbstractController [HttpFoundation][FrameworkBundle] Fix default locale is ignored when set_locale_from_accept_language is used add missing translations [Validator] updated Lithuanian translation [Validator] fix some non-sense Lithuanian translations [Validator] updated Slovenian translation [Validator] updated Finnish translation [RateLimit] Test and fix peeking behavior on rate limit policies [Validator] Add `Charset` french translation [Tests] Streamline CompiledUrlGenerator tests [Serializer] Skip uninitialized properties with deep_object_to_populate fix Constraints\Email::ERROR_NAMES
2 parents 2620a67 + f87ea9d commit dd7d761

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

Normalizer/AbstractObjectNormalizer.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,7 @@ public function normalize(mixed $object, string $format = null, array $context =
179179
$attributeValue = $attribute === $this->classDiscriminatorResolver?->getMappingForMappedObject($object)?->getTypeProperty()
180180
? $this->classDiscriminatorResolver?->getTypeForMappedObject($object)
181181
: $this->getAttributeValue($object, $attribute, $format, $attributeContext);
182-
} catch (UninitializedPropertyException $e) {
183-
if ($context[self::SKIP_UNINITIALIZED_VALUES] ?? $this->defaultContext[self::SKIP_UNINITIALIZED_VALUES] ?? true) {
184-
continue;
185-
}
186-
throw $e;
187-
} catch (\Error $e) {
182+
} catch (UninitializedPropertyException|\Error $e) {
188183
if (($context[self::SKIP_UNINITIALIZED_VALUES] ?? $this->defaultContext[self::SKIP_UNINITIALIZED_VALUES] ?? true) && $this->isUninitializedValueError($e)) {
189184
continue;
190185
}
@@ -349,6 +344,10 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
349344
? $discriminatorMapping
350345
: $this->getAttributeValue($object, $attribute, $format, $attributeContext);
351346
} catch (NoSuchPropertyException) {
347+
} catch (UninitializedPropertyException|\Error $e) {
348+
if (!(($context[self::SKIP_UNINITIALIZED_VALUES] ?? $this->defaultContext[self::SKIP_UNINITIALIZED_VALUES] ?? true) && $this->isUninitializedValueError($e))) {
349+
throw $e;
350+
}
352351
}
353352
}
354353

@@ -742,9 +741,10 @@ private function getCacheKey(?string $format, array $context): bool|string
742741
* This error may occur when specific object normalizer implementation gets attribute value
743742
* by accessing a public uninitialized property or by calling a method accessing such property.
744743
*/
745-
private function isUninitializedValueError(\Error $e): bool
744+
private function isUninitializedValueError(\Error|UninitializedPropertyException $e): bool
746745
{
747-
return str_starts_with($e->getMessage(), 'Typed property')
746+
return $e instanceof UninitializedPropertyException
747+
|| str_starts_with($e->getMessage(), 'Typed property')
748748
&& str_ends_with($e->getMessage(), 'must not be accessed before initialization');
749749
}
750750

Tests/Normalizer/Features/SkipUninitializedValuesTestTrait.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
namespace Symfony\Component\Serializer\Tests\Normalizer\Features;
1313

1414
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
15-
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
15+
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
1616

1717
/**
1818
* Test AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES.
1919
*/
2020
trait SkipUninitializedValuesTestTrait
2121
{
22-
abstract protected function getNormalizerForSkipUninitializedValues(): NormalizerInterface;
22+
abstract protected function getNormalizerForSkipUninitializedValues(): AbstractObjectNormalizer;
2323

2424
/**
2525
* @dataProvider skipUninitializedValuesFlagProvider
@@ -31,6 +31,15 @@ public function testSkipUninitializedValues(array $context)
3131
$normalizer = $this->getNormalizerForSkipUninitializedValues();
3232
$result = $normalizer->normalize($object, null, $context);
3333
$this->assertSame(['initialized' => 'value'], $result);
34+
35+
$normalizer->denormalize(
36+
['unInitialized' => 'value'],
37+
TypedPropertiesObjectWithGetters::class,
38+
null,
39+
['object_to_populate' => $objectToPopulate = new TypedPropertiesObjectWithGetters(), 'deep_object_to_populate' => true] + $context
40+
);
41+
42+
$this->assertSame('value', $objectToPopulate->getUninitialized());
3443
}
3544

3645
public function skipUninitializedValuesFlagProvider(): iterable

Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ protected function getNormalizerForCacheableObjectAttributesTest(): GetSetMethod
487487
return new GetSetMethodNormalizer();
488488
}
489489

490-
protected function getNormalizerForSkipUninitializedValues(): NormalizerInterface
490+
protected function getNormalizerForSkipUninitializedValues(): GetSetMethodNormalizer
491491
{
492492
return new GetSetMethodNormalizer(new ClassMetadataFactory(new AttributeLoader()));
493493
}

Tests/Normalizer/PropertyNormalizerTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
2626
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
2727
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
28-
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2928
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
3029
use Symfony\Component\Serializer\Serializer;
3130
use Symfony\Component\Serializer\SerializerInterface;
@@ -492,7 +491,7 @@ protected function getNormalizerForCacheableObjectAttributesTest(): AbstractObje
492491
return new PropertyNormalizer();
493492
}
494493

495-
protected function getNormalizerForSkipUninitializedValues(): NormalizerInterface
494+
protected function getNormalizerForSkipUninitializedValues(): PropertyNormalizer
496495
{
497496
return new PropertyNormalizer(new ClassMetadataFactory(new AttributeLoader()));
498497
}

0 commit comments

Comments
 (0)