From 68fb7332b3339553fcc96234ea0325a31e37263b Mon Sep 17 00:00:00 2001 From: David Badura Date: Fri, 6 Nov 2026 18:21:51 +0100 Subject: [PATCH] Pass non-array values from ObjectNormalizer to the hydrator Extracting an object applies its class normalizer, so the result can be a scalar, but denormalize rejected everything that was not an array. Such a property could be written but never read back. Now the hydrator decides: it uses the class normalizer or throws ArrayDataRequired if there is none. --- src/Normalizer/ObjectNormalizer.php | 6 ----- .../Fixture/ExplicitObjectNormalizerDto.php | 16 ++++++++++++ .../Unit/Normalizer/ObjectNormalizerTest.php | 21 ++++++++++++++++ tests/Unit/StackHydratorTest.php | 25 +++++++++++++++++++ 4 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 tests/Unit/Fixture/ExplicitObjectNormalizerDto.php diff --git a/src/Normalizer/ObjectNormalizer.php b/src/Normalizer/ObjectNormalizer.php index f254b359..c6f654e3 100644 --- a/src/Normalizer/ObjectNormalizer.php +++ b/src/Normalizer/ObjectNormalizer.php @@ -12,8 +12,6 @@ use Symfony\Component\TypeInfo\Type\ObjectType; use Symfony\Component\TypeInfo\Type\TemplateType; -use function is_array; - #[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_CLASS)] final class ObjectNormalizer implements Normalizer, TypeAwareNormalizer, HydratorAwareNormalizer { @@ -56,10 +54,6 @@ public function denormalize(mixed $value, array $context): object|null return null; } - if (!is_array($value)) { - throw InvalidArgument::withWrongType('array|null', $value); - } - $className = $this->className(); return $this->hydrator->hydrate($className, $value, $context); diff --git a/tests/Unit/Fixture/ExplicitObjectNormalizerDto.php b/tests/Unit/Fixture/ExplicitObjectNormalizerDto.php new file mode 100644 index 00000000..c8489e01 --- /dev/null +++ b/tests/Unit/Fixture/ExplicitObjectNormalizerDto.php @@ -0,0 +1,16 @@ +createMock(Hydrator::class); + + $expected = ProfileId::fromString('1'); + + $hydrator->expects($this->once())->method('hydrate')->with( + ProfileId::class, + '1', + ) + ->willReturn($expected); + + $normalizer = new ObjectNormalizer(ProfileId::class); + $normalizer->setHydrator($hydrator); + + $this->assertEquals( + $expected, + $normalizer->denormalize('1', []), + ); + } + public function testNormalizePassesContextToHydrator(): void { $context = ['key' => 'value']; diff --git a/tests/Unit/StackHydratorTest.php b/tests/Unit/StackHydratorTest.php index fa93b9a6..85c9820e 100644 --- a/tests/Unit/StackHydratorTest.php +++ b/tests/Unit/StackHydratorTest.php @@ -36,6 +36,7 @@ use Patchlevel\Hydrator\Tests\Unit\Fixture\DummyMiddleware; use Patchlevel\Hydrator\Tests\Unit\Fixture\Email; use Patchlevel\Hydrator\Tests\Unit\Fixture\ExplicitArrayShapeNormalizerDto; +use Patchlevel\Hydrator\Tests\Unit\Fixture\ExplicitObjectNormalizerDto; use Patchlevel\Hydrator\Tests\Unit\Fixture\InferNormalizerDto; use Patchlevel\Hydrator\Tests\Unit\Fixture\InferNormalizerWithArrayShapeDto; use Patchlevel\Hydrator\Tests\Unit\Fixture\InferNormalizerWithIterablesDto; @@ -746,6 +747,30 @@ public function testHydrateClassOfObjectMap(): void ); } + public function testRoundTripWithExplicitObjectNormalizerAndClassNormalizer(): void + { + $object = new ExplicitObjectNormalizerDto(ProfileId::fromString('1')); + + $data = $this->hydrator->extract($object); + + self::assertEquals(['profileId' => '1'], $data); + self::assertEquals($object, $this->hydrator->hydrate(ExplicitObjectNormalizerDto::class, $data)); + } + + public function testHydrateWithExplicitObjectNormalizerAndNonArrayData(): void + { + try { + $this->hydrator->hydrate( + ProfileCreatedWrapper::class, + ['event' => 'foo'], + ); + + self::fail('DenormalizationFailure was not thrown'); + } catch (DenormalizationFailure $e) { + self::assertInstanceOf(ArrayDataRequired::class, $e->getPrevious()); + } + } + #[RequiresPhp('>=8.4')] public function testLazyHydrate(): void {