Skip to content

Commit c81b54a

Browse files
committed
fix infer normalizer for nullable types
1 parent e60410f commit c81b54a

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

src/Metadata/AttributeMetadataFactory.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use ReflectionProperty;
3030
use Symfony\Component\TypeInfo\Type;
3131
use Symfony\Component\TypeInfo\Type\CollectionType;
32+
use Symfony\Component\TypeInfo\Type\NullableType;
3233
use Symfony\Component\TypeInfo\Type\ObjectType;
3334
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;
3435

@@ -396,13 +397,21 @@ private function findNormalizer(ReflectionProperty $reflectionProperty, Type $ty
396397
return $attributeReflectionList[0]->newInstance();
397398
}
398399

400+
if ($type instanceof NullableType) {
401+
$type = $type->getWrappedType();
402+
}
403+
399404
if ($type instanceof ObjectType) {
400405
return $this->findNormalizerOnClass(new ReflectionClass($type->getClassName()));
401406
}
402407

403408
if ($type instanceof CollectionType && $type->getCollectionValueType() instanceof ObjectType) {
404409
$valueType = $type->getCollectionValueType();
405410

411+
if ($valueType instanceof NullableType) {
412+
$valueType = $type->getWrappedType();
413+
}
414+
406415
if (!$valueType instanceof ObjectType) {
407416
return null;
408417
}

tests/Unit/Fixture/InferNormalizerWithNullableDto.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function __construct(
1515
public DateTimeImmutable|null $dateTimeImmutable,
1616
public DateTime|null $dateTime = null,
1717
public DateTimeZone|null $dateTimeZone = null,
18+
public ProfileId|null $profileId = null,
1819
) {
1920
}
2021
}

tests/Unit/Metadata/AttributeMetadataFactoryTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Patchlevel\Hydrator\Tests\Unit\Fixture\MissingSubjectIdDto;
2828
use Patchlevel\Hydrator\Tests\Unit\Fixture\ParentDto;
2929
use Patchlevel\Hydrator\Tests\Unit\Fixture\ParentWithPersonalDataDto;
30+
use Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileId;
3031
use Patchlevel\Hydrator\Tests\Unit\Fixture\Status;
3132
use PHPUnit\Framework\TestCase;
3233

@@ -199,6 +200,27 @@ public function __construct(
199200
self::assertSame(Status::class, $normalizer->getEnum());
200201
}
201202

203+
public function testEventWithInferNormalizer(): void
204+
{
205+
$object = new class {
206+
public function __construct(
207+
public ProfileId|null $profileId = null,
208+
) {
209+
}
210+
};
211+
212+
$metadataFactory = new AttributeMetadataFactory();
213+
$metadata = $metadataFactory->metadata($object::class);
214+
215+
$properties = $metadata->properties();
216+
217+
self::assertCount(1, $properties);
218+
219+
$propertyMetadata = $metadata->propertyForField('profileId');
220+
221+
self::assertEquals(new IdNormalizer(ProfileId::class), $propertyMetadata->normalizer());
222+
}
223+
202224
public function testExtends(): void
203225
{
204226
$metadataFactory = new AttributeMetadataFactory();

tests/Unit/MetadataHydratorTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,28 @@ public function testExtractCircularReference(): void
106106
$this->hydrator->extract($dto1);
107107
}
108108

109+
public function testExtractWithInferNormalizer2(): void
110+
{
111+
$result = $this->hydrator->extract(
112+
new InferNormalizerWithNullableDto(
113+
null,
114+
null,
115+
profileId: ProfileId::fromString('1'),
116+
),
117+
);
118+
119+
self::assertEquals(
120+
[
121+
'status' => null,
122+
'dateTimeImmutable' => null,
123+
'dateTime' => null,
124+
'dateTimeZone' => null,
125+
'profileId' => '1',
126+
],
127+
$result,
128+
);
129+
}
130+
109131
public function testExtractWithInferNormalizerFailed(): void
110132
{
111133
$this->expectException(NormalizationMissing::class);

0 commit comments

Comments
 (0)