From 53d9b74bd37b786a29e4806554c2ceebeae55079 Mon Sep 17 00:00:00 2001 From: David Badura Date: Fri, 6 Nov 2026 18:47:13 +0100 Subject: [PATCH 1/4] Infer normalizers for nullable collection items The collection branch unwrapped the nullable item type from the collection itself instead of from the item type, so collections like list got no normalizer at all and their items were passed through as raw objects. --- src/Metadata/AttributeMetadataFactory.php | 2 +- .../InferNormalizerWithNullableItemsDto.php | 24 +++++++++++++++++ .../Metadata/AttributeMetadataFactoryTest.php | 27 +++++++++++++++++++ tests/Unit/StackHydratorTest.php | 24 +++++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/Fixture/InferNormalizerWithNullableItemsDto.php diff --git a/src/Metadata/AttributeMetadataFactory.php b/src/Metadata/AttributeMetadataFactory.php index b3d530d1..2483389b 100644 --- a/src/Metadata/AttributeMetadataFactory.php +++ b/src/Metadata/AttributeMetadataFactory.php @@ -312,7 +312,7 @@ private function inferNormalizerByType(Type $type): Normalizer|null $valueType = $type->getCollectionValueType(); if ($valueType instanceof NullableType) { - $valueType = $type->getWrappedType(); + $valueType = $valueType->getWrappedType(); } $normalizer = null; diff --git a/tests/Unit/Fixture/InferNormalizerWithNullableItemsDto.php b/tests/Unit/Fixture/InferNormalizerWithNullableItemsDto.php new file mode 100644 index 00000000..894e6c97 --- /dev/null +++ b/tests/Unit/Fixture/InferNormalizerWithNullableItemsDto.php @@ -0,0 +1,24 @@ + $skills + * @param array $statuses + * @param list $dates + * @param array>|null $nested + */ + public function __construct( + public array $skills = [], + public array $statuses = [], + public array $dates = [], + public array|null $nested = null, + ) { + } +} diff --git a/tests/Unit/Metadata/AttributeMetadataFactoryTest.php b/tests/Unit/Metadata/AttributeMetadataFactoryTest.php index 3486bb7d..b2310840 100644 --- a/tests/Unit/Metadata/AttributeMetadataFactoryTest.php +++ b/tests/Unit/Metadata/AttributeMetadataFactoryTest.php @@ -10,6 +10,8 @@ use Patchlevel\Hydrator\Metadata\ClassNotFound; use Patchlevel\Hydrator\Metadata\DuplicatedFieldNameInMetadata; use Patchlevel\Hydrator\Metadata\PropertyMetadataNotFound; +use Patchlevel\Hydrator\Normalizer\ArrayNormalizer; +use Patchlevel\Hydrator\Normalizer\DateTimeImmutableNormalizer; use Patchlevel\Hydrator\Normalizer\EnumNormalizer; use Patchlevel\Hydrator\Normalizer\ObjectNormalizer; use Patchlevel\Hydrator\Tests\Unit\Fixture\BrokenParentDto; @@ -20,9 +22,11 @@ use Patchlevel\Hydrator\Tests\Unit\Fixture\IdNormalizer; use Patchlevel\Hydrator\Tests\Unit\Fixture\IgnoreDto; use Patchlevel\Hydrator\Tests\Unit\Fixture\IgnoreParentDto; +use Patchlevel\Hydrator\Tests\Unit\Fixture\InferNormalizerWithNullableItemsDto; use Patchlevel\Hydrator\Tests\Unit\Fixture\ParentDto; use Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileCreatedWithGeneric; use Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileId; +use Patchlevel\Hydrator\Tests\Unit\Fixture\Skill; use Patchlevel\Hydrator\Tests\Unit\Fixture\Status; use Patchlevel\Hydrator\Tests\Unit\Fixture\Wrapper; use PHPUnit\Framework\Attributes\CoversClass; @@ -222,6 +226,29 @@ public function __construct( self::assertEquals(new IdNormalizer(ProfileId::class), $propertyMetadata->normalizer); } + public function testInferNormalizerForNullableCollectionItems(): void + { + $metadataFactory = new AttributeMetadataFactory(); + $metadata = $metadataFactory->metadata(InferNormalizerWithNullableItemsDto::class); + + self::assertEquals( + new ArrayNormalizer(new ObjectNormalizer(Skill::class)), + $metadata->propertyForField('skills')->normalizer, + ); + self::assertEquals( + new ArrayNormalizer(new EnumNormalizer(Status::class)), + $metadata->propertyForField('statuses')->normalizer, + ); + self::assertEquals( + new ArrayNormalizer(new DateTimeImmutableNormalizer()), + $metadata->propertyForField('dates')->normalizer, + ); + self::assertEquals( + new ArrayNormalizer(new ArrayNormalizer(new EnumNormalizer(Status::class))), + $metadata->propertyForField('nested')->normalizer, + ); + } + public function testInferNormalizerWithGeneric(): void { $metadataFactory = new AttributeMetadataFactory(); diff --git a/tests/Unit/StackHydratorTest.php b/tests/Unit/StackHydratorTest.php index 8b8e3195..700fb0b8 100644 --- a/tests/Unit/StackHydratorTest.php +++ b/tests/Unit/StackHydratorTest.php @@ -35,6 +35,7 @@ use Patchlevel\Hydrator\Tests\Unit\Fixture\InferNormalizerDto; use Patchlevel\Hydrator\Tests\Unit\Fixture\InferNormalizerWithIterablesDto; use Patchlevel\Hydrator\Tests\Unit\Fixture\InferNormalizerWithNullableDto; +use Patchlevel\Hydrator\Tests\Unit\Fixture\InferNormalizerWithNullableItemsDto; use Patchlevel\Hydrator\Tests\Unit\Fixture\LazyProfileCreated; use Patchlevel\Hydrator\Tests\Unit\Fixture\NormalizerInBaseClassDefinedDto; use Patchlevel\Hydrator\Tests\Unit\Fixture\ParentDto; @@ -557,6 +558,29 @@ public function testHydrateWithInferNormalizerWitIterables(): void self::assertEquals($expected, $event); } + public function testRoundTripWithInferNormalizerForNullableCollectionItems(): void + { + $object = new InferNormalizerWithNullableItemsDto( + [new Skill('php'), null], + ['foo' => Status::Draft, 'bar' => null], + [new DateTimeImmutable('2015-02-13 22:34:32+01:00'), null], + ['foo' => [Status::Closed, null]], + ); + + $data = [ + 'skills' => [['name' => 'php'], null], + 'statuses' => ['foo' => 'draft', 'bar' => null], + 'dates' => ['2015-02-13T22:34:32+01:00', null], + 'nested' => ['foo' => ['closed', null]], + ]; + + self::assertSame($data, $this->hydrator->extract($object)); + self::assertEquals( + $object, + $this->hydrator->hydrate(InferNormalizerWithNullableItemsDto::class, $data), + ); + } + public function testHydrateWithClassNormalizer(): void { $object = $this->hydrator->hydrate( From 84a61d7a942e7d9a3e50c8e2f9c9f26681e4e126 Mon Sep 17 00:00:00 2001 From: David Badura Date: Fri, 6 Nov 2026 18:48:16 +0100 Subject: [PATCH 2/4] Skip array shape fields without type info in handleType An explicit ArrayShapeNormalizer map can contain keys that the property's docblock shape doesn't know. handleType read those keys anyway and raised undefined key warnings. Such fields are now left alone, like the whole map is when the property type isn't an array shape. --- src/Normalizer/ArrayShapeNormalizer.php | 3 ++- .../ExplicitArrayShapeNormalizerDto.php | 22 +++++++++++++++ .../Normalizer/ArrayShapeNormalizerTest.php | 27 +++++++++++++++++++ tests/Unit/StackHydratorTest.php | 22 +++++++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/Fixture/ExplicitArrayShapeNormalizerDto.php diff --git a/src/Normalizer/ArrayShapeNormalizer.php b/src/Normalizer/ArrayShapeNormalizer.php index 23d99619..0d5d645a 100644 --- a/src/Normalizer/ArrayShapeNormalizer.php +++ b/src/Normalizer/ArrayShapeNormalizer.php @@ -10,6 +10,7 @@ use Symfony\Component\TypeInfo\Type\ArrayShapeType; use Symfony\Component\TypeInfo\Type\NullableType; +use function array_key_exists; use function is_array; #[Attribute(Attribute::TARGET_PROPERTY)] @@ -105,7 +106,7 @@ public function handleType(Type|null $type): void $shape = $type->getShape(); foreach ($this->normalizerMap as $field => $normalizer) { - if (!$normalizer instanceof TypeAwareNormalizer) { + if (!$normalizer instanceof TypeAwareNormalizer || !array_key_exists($field, $shape)) { continue; } diff --git a/tests/Unit/Fixture/ExplicitArrayShapeNormalizerDto.php b/tests/Unit/Fixture/ExplicitArrayShapeNormalizerDto.php new file mode 100644 index 00000000..9b1bb91a --- /dev/null +++ b/tests/Unit/Fixture/ExplicitArrayShapeNormalizerDto.php @@ -0,0 +1,22 @@ + new EnumNormalizer(), + 'skill' => new ObjectNormalizer(Skill::class), + ])] + public array|null $shape = null, + ) { + } +} diff --git a/tests/Unit/Normalizer/ArrayShapeNormalizerTest.php b/tests/Unit/Normalizer/ArrayShapeNormalizerTest.php index 78be01dc..fe8aa0f7 100644 --- a/tests/Unit/Normalizer/ArrayShapeNormalizerTest.php +++ b/tests/Unit/Normalizer/ArrayShapeNormalizerTest.php @@ -6,11 +6,15 @@ use Patchlevel\Hydrator\Hydrator; use Patchlevel\Hydrator\Normalizer\ArrayShapeNormalizer; +use Patchlevel\Hydrator\Normalizer\EnumNormalizer; use Patchlevel\Hydrator\Normalizer\HydratorAwareNormalizer; use Patchlevel\Hydrator\Normalizer\InvalidArgument; use Patchlevel\Hydrator\Normalizer\Normalizer; +use Patchlevel\Hydrator\Normalizer\TypeAwareNormalizer; +use Patchlevel\Hydrator\Tests\Unit\Fixture\Status; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; +use Symfony\Component\TypeInfo\Type; #[CoversClass(ArrayShapeNormalizer::class)] final class ArrayShapeNormalizerTest extends TestCase @@ -165,6 +169,29 @@ public function testPassHydrator(): void $normalizer->setHydrator($hydrator); } + public function testHandleTypePassesFieldTypes(): void + { + $innerNormalizer = new EnumNormalizer(); + + $normalizer = new ArrayShapeNormalizer(['status' => $innerNormalizer]); + $normalizer->handleType(Type::nullable(Type::arrayShape(['status' => Type::enum(Status::class)]))); + + self::assertSame(Status::class, $innerNormalizer->className()); + } + + public function testHandleTypeSkipsFieldsMissingInShape(): void + { + $statusNormalizer = new EnumNormalizer(); + + $missingNormalizer = $this->createMockForIntersectionOfInterfaces([Normalizer::class, TypeAwareNormalizer::class]); + $missingNormalizer->expects($this->never())->method('handleType'); + + $normalizer = new ArrayShapeNormalizer(['status' => $statusNormalizer, 'missing' => $missingNormalizer]); + $normalizer->handleType(Type::arrayShape(['status' => Type::enum(Status::class)])); + + self::assertSame(Status::class, $statusNormalizer->className()); + } + public function testInnerNormalizers(): void { $innerNormalizer = $this->createMock(Normalizer::class); diff --git a/tests/Unit/StackHydratorTest.php b/tests/Unit/StackHydratorTest.php index 700fb0b8..6e76018b 100644 --- a/tests/Unit/StackHydratorTest.php +++ b/tests/Unit/StackHydratorTest.php @@ -32,6 +32,7 @@ use Patchlevel\Hydrator\Tests\Unit\Fixture\DefaultDto; 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\InferNormalizerDto; use Patchlevel\Hydrator\Tests\Unit\Fixture\InferNormalizerWithIterablesDto; use Patchlevel\Hydrator\Tests\Unit\Fixture\InferNormalizerWithNullableDto; @@ -581,6 +582,27 @@ public function testRoundTripWithInferNormalizerForNullableCollectionItems(): vo ); } + public function testRoundTripWithExplicitArrayShapeNormalizerAndFieldMissingInDocblock(): void + { + $object = new ExplicitArrayShapeNormalizerDto([ + 'status' => Status::Draft, + 'skill' => new Skill('php'), + ]); + + $data = [ + 'shape' => [ + 'status' => 'draft', + 'skill' => ['name' => 'php'], + ], + ]; + + self::assertSame($data, $this->hydrator->extract($object)); + self::assertEquals( + $object, + $this->hydrator->hydrate(ExplicitArrayShapeNormalizerDto::class, $data), + ); + } + public function testHydrateWithClassNormalizer(): void { $object = $this->hydrator->hydrate( From 113d43ce8777ba1eba1f0ab00646d465c42f5596 Mon Sep 17 00:00:00 2001 From: David Badura Date: Fri, 6 Nov 2026 18:49:04 +0100 Subject: [PATCH 3/4] Let inferNormalizerByType unwrap collection and shape items itself The collection and array shape branches unwrapped nullable item types and looked for a class normalizer before recursing, although inferNormalizerByType does both on its own. Dropping the duplicate removes the place where the nullable item bug came from. --- src/Metadata/AttributeMetadataFactory.php | 32 ++--------------------- 1 file changed, 2 insertions(+), 30 deletions(-) diff --git a/src/Metadata/AttributeMetadataFactory.php b/src/Metadata/AttributeMetadataFactory.php index 2483389b..efcd981c 100644 --- a/src/Metadata/AttributeMetadataFactory.php +++ b/src/Metadata/AttributeMetadataFactory.php @@ -278,21 +278,7 @@ private function inferNormalizerByType(Type $type): Normalizer|null $normalizers = []; foreach ($shape as $field => $fieldInfo) { - $valueType = $fieldInfo['type']; - - if ($valueType instanceof NullableType) { - $valueType = $valueType->getWrappedType(); - } - - $normalizer = null; - - if ($valueType instanceof ObjectType) { - $normalizer = $this->findNormalizerOnClass($valueType->getClassName()); - } - - if ($normalizer === null) { - $normalizer = $this->inferNormalizerByType($valueType); - } + $normalizer = $this->inferNormalizerByType($fieldInfo['type']); if ($normalizer === null) { continue; @@ -309,21 +295,7 @@ private function inferNormalizerByType(Type $type): Normalizer|null } if ($type instanceof CollectionType) { - $valueType = $type->getCollectionValueType(); - - if ($valueType instanceof NullableType) { - $valueType = $valueType->getWrappedType(); - } - - $normalizer = null; - - if ($valueType instanceof ObjectType) { - $normalizer = $this->findNormalizerOnClass($valueType->getClassName()); - } - - if ($normalizer === null) { - $normalizer = $this->inferNormalizerByType($valueType); - } + $normalizer = $this->inferNormalizerByType($type->getCollectionValueType()); if ($normalizer) { return new ArrayNormalizer($normalizer); From bd73d00951859416755901e8d45f8f927ca58331 Mon Sep 17 00:00:00 2001 From: David Badura Date: Fri, 6 Nov 2026 18:57:50 +0100 Subject: [PATCH 4/4] Cover array shape inference in the metadata factory test The shape branch of inferNormalizerByType was only reached through the hydrator tests, which do not cover the metadata factory. --- .../InferNormalizerWithNullableShapeItemsDto.php | 14 ++++++++++++++ .../Metadata/AttributeMetadataFactoryTest.php | 16 ++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/Unit/Fixture/InferNormalizerWithNullableShapeItemsDto.php diff --git a/tests/Unit/Fixture/InferNormalizerWithNullableShapeItemsDto.php b/tests/Unit/Fixture/InferNormalizerWithNullableShapeItemsDto.php new file mode 100644 index 00000000..c49fb476 --- /dev/null +++ b/tests/Unit/Fixture/InferNormalizerWithNullableShapeItemsDto.php @@ -0,0 +1,14 @@ +metadata(InferNormalizerWithNullableShapeItemsDto::class); + + self::assertEquals( + new ArrayShapeNormalizer([ + 'skill' => new ObjectNormalizer(Skill::class), + 'status' => new EnumNormalizer(Status::class), + ]), + $metadata->propertyForField('shape')->normalizer, + ); + } + public function testInferNormalizerWithGeneric(): void { $metadataFactory = new AttributeMetadataFactory();