From d859e70eb983323f138cc78be5ccc5f6baf9c53a Mon Sep 17 00:00:00 2001 From: Artem Henvald Date: Thu, 14 Mar 2024 14:59:24 +0200 Subject: [PATCH] Fix deserialization for nested objects (#55) --- Request/DtoExtractor.php | 6 +++++- Tests/Request/DtoExtractorTest.php | 9 ++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Request/DtoExtractor.php b/Request/DtoExtractor.php index 0b99a6f..0e8a1fd 100644 --- a/Request/DtoExtractor.php +++ b/Request/DtoExtractor.php @@ -17,6 +17,7 @@ use StfalconStudio\ApiBundle\Service\AttributeProcessor\DtoAttributeProcessor; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; +use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer; use Symfony\Component\Serializer\SerializerInterface; /** @@ -59,7 +60,10 @@ public function getDtoFromRequestForDtoClass(Request $request, string $dtoClassN { $context = []; if (null !== $objectToPopulate) { - $context = [AbstractNormalizer::OBJECT_TO_POPULATE => $objectToPopulate]; + $context = [ + AbstractNormalizer::OBJECT_TO_POPULATE => $objectToPopulate, + AbstractObjectNormalizer::DEEP_OBJECT_TO_POPULATE => true, + ]; } $object = $this->serializer->deserialize($request->getContent(), $dtoClassName, 'json', $context); diff --git a/Tests/Request/DtoExtractorTest.php b/Tests/Request/DtoExtractorTest.php index 675eb23..cddd93d 100644 --- a/Tests/Request/DtoExtractorTest.php +++ b/Tests/Request/DtoExtractorTest.php @@ -90,11 +90,11 @@ public function testGetDtoFromRequestWithoutPopulation(?object $objectToPopulate public static function dataProvider(): iterable { yield [ - 'object_to_populate' => null, + 'objectToPopulate' => null, 'context' => [], ]; yield [ - 'object_to_populate' => new \stdClass(), + 'objectToPopulate' => new \stdClass(), 'context' => [ AbstractNormalizer::OBJECT_TO_POPULATE => new \stdClass(), AbstractObjectNormalizer::DEEP_OBJECT_TO_POPULATE => true, @@ -127,7 +127,10 @@ public function testExceptionOnDtoWithoutInterface(): void $dtoMock = $this->createStub(\stdClass::class); $objectToPopulate = new \stdClass(); - $context = [AbstractNormalizer::OBJECT_TO_POPULATE => $objectToPopulate]; + $context = [ + AbstractNormalizer::OBJECT_TO_POPULATE => $objectToPopulate, + AbstractObjectNormalizer::DEEP_OBJECT_TO_POPULATE => true, + ]; $this->serializer ->expects(self::once())