Skip to content

Commit

Permalink
Fix deserialization for nested objects (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
fre5h authored Mar 14, 2024
1 parent ff5e19c commit d859e70
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 5 additions & 1 deletion Request/DtoExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 6 additions & 3 deletions Tests/Request/DtoExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit d859e70

Please sign in to comment.