diff --git a/src/Normalizer/ObjectNormalizer.php b/src/Normalizer/ObjectNormalizer.php index f254b35..c6f654e 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 0000000..c8489e0 --- /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 fa93b9a..85c9820 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 {