Skip to content

Commit

Permalink
Merge branch '6.3' into 6.4
Browse files Browse the repository at this point in the history
* 6.3:
  [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
  • Loading branch information
nicolas-grekas committed Dec 29, 2023
2 parents d4028ee + 83a2e5e commit f87ea9d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
16 changes: 8 additions & 8 deletions Normalizer/AbstractObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,7 @@ public function normalize(mixed $object, string $format = null, array $context =
$attributeValue = $attribute === $this->classDiscriminatorResolver?->getMappingForMappedObject($object)?->getTypeProperty()
? $this->classDiscriminatorResolver?->getTypeForMappedObject($object)
: $this->getAttributeValue($object, $attribute, $format, $attributeContext);
} catch (UninitializedPropertyException $e) {
if ($context[self::SKIP_UNINITIALIZED_VALUES] ?? $this->defaultContext[self::SKIP_UNINITIALIZED_VALUES] ?? true) {
continue;
}
throw $e;
} catch (\Error $e) {
} catch (UninitializedPropertyException|\Error $e) {
if (($context[self::SKIP_UNINITIALIZED_VALUES] ?? $this->defaultContext[self::SKIP_UNINITIALIZED_VALUES] ?? true) && $this->isUninitializedValueError($e)) {
continue;
}
Expand Down Expand Up @@ -373,6 +368,10 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
? $discriminatorMapping
: $this->getAttributeValue($object, $attribute, $format, $attributeContext);
} catch (NoSuchPropertyException) {
} catch (UninitializedPropertyException|\Error $e) {
if (!(($context[self::SKIP_UNINITIALIZED_VALUES] ?? $this->defaultContext[self::SKIP_UNINITIALIZED_VALUES] ?? true) && $this->isUninitializedValueError($e))) {
throw $e;
}
}
}

Expand Down Expand Up @@ -769,9 +768,10 @@ private function getCacheKey(?string $format, array $context): bool|string
* This error may occur when specific object normalizer implementation gets attribute value
* by accessing a public uninitialized property or by calling a method accessing such property.
*/
private function isUninitializedValueError(\Error $e): bool
private function isUninitializedValueError(\Error|UninitializedPropertyException $e): bool
{
return str_starts_with($e->getMessage(), 'Typed property')
return $e instanceof UninitializedPropertyException
|| str_starts_with($e->getMessage(), 'Typed property')
&& str_ends_with($e->getMessage(), 'must not be accessed before initialization');
}

Expand Down
13 changes: 11 additions & 2 deletions Tests/Normalizer/Features/SkipUninitializedValuesTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
namespace Symfony\Component\Serializer\Tests\Normalizer\Features;

use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;

/**
* Test AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES.
*/
trait SkipUninitializedValuesTestTrait
{
abstract protected function getNormalizerForSkipUninitializedValues(): NormalizerInterface;
abstract protected function getNormalizerForSkipUninitializedValues(): AbstractObjectNormalizer;

/**
* @dataProvider skipUninitializedValuesFlagProvider
Expand All @@ -31,6 +31,15 @@ public function testSkipUninitializedValues(array $context)
$normalizer = $this->getNormalizerForSkipUninitializedValues();
$result = $normalizer->normalize($object, null, $context);
$this->assertSame(['initialized' => 'value'], $result);

$normalizer->denormalize(
['unInitialized' => 'value'],
TypedPropertiesObjectWithGetters::class,
null,
['object_to_populate' => $objectToPopulate = new TypedPropertiesObjectWithGetters(), 'deep_object_to_populate' => true] + $context
);

$this->assertSame('value', $objectToPopulate->getUninitialized());
}

public function skipUninitializedValuesFlagProvider(): iterable
Expand Down
2 changes: 1 addition & 1 deletion Tests/Normalizer/GetSetMethodNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ protected function getNormalizerForCacheableObjectAttributesTest(): GetSetMethod
return new GetSetMethodNormalizer();
}

protected function getNormalizerForSkipUninitializedValues(): NormalizerInterface
protected function getNormalizerForSkipUninitializedValues(): GetSetMethodNormalizer
{
return new GetSetMethodNormalizer(new ClassMetadataFactory(new AttributeLoader()));
}
Expand Down
3 changes: 1 addition & 2 deletions Tests/Normalizer/PropertyNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
Expand Down Expand Up @@ -492,7 +491,7 @@ protected function getNormalizerForCacheableObjectAttributesTest(): AbstractObje
return new PropertyNormalizer();
}

protected function getNormalizerForSkipUninitializedValues(): NormalizerInterface
protected function getNormalizerForSkipUninitializedValues(): PropertyNormalizer
{
return new PropertyNormalizer(new ClassMetadataFactory(new AttributeLoader()));
}
Expand Down

0 comments on commit f87ea9d

Please sign in to comment.