From 3af4bf61757bb94817b3c1c9bacad695e4e28b55 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Wed, 15 May 2024 17:09:08 +0400 Subject: [PATCH] fix(Marshaller): fixed unmarshalling of typed assoc arrays --- src/Internal/Marshaller/Type/ArrayType.php | 2 +- tests/Unit/DTO/Type/ArrayType/ArrayDto.php | 5 +++++ tests/Unit/DTO/Type/ArrayType/ArrayTestCase.php | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Internal/Marshaller/Type/ArrayType.php b/src/Internal/Marshaller/Type/ArrayType.php index efe27264..3edadda6 100644 --- a/src/Internal/Marshaller/Type/ArrayType.php +++ b/src/Internal/Marshaller/Type/ArrayType.php @@ -83,7 +83,7 @@ public function parse($value, $current) $result = []; foreach ($value as $i => $item) { - $result[] = $this->type->parse($item, $current[$i] ?? null); + $result[$i] = $this->type->parse($item, $current[$i] ?? null); } return $result; diff --git a/tests/Unit/DTO/Type/ArrayType/ArrayDto.php b/tests/Unit/DTO/Type/ArrayType/ArrayDto.php index 3f375769..d8078284 100644 --- a/tests/Unit/DTO/Type/ArrayType/ArrayDto.php +++ b/tests/Unit/DTO/Type/ArrayType/ArrayDto.php @@ -33,4 +33,9 @@ class ArrayDto public iterable $iterable; public ?iterable $iterableNullable; + + public array $assoc; + + #[MarshalArray(of: \stdClass::class)] + public array $assocOfType; } diff --git a/tests/Unit/DTO/Type/ArrayType/ArrayTestCase.php b/tests/Unit/DTO/Type/ArrayType/ArrayTestCase.php index af02d3ee..85595ee6 100644 --- a/tests/Unit/DTO/Type/ArrayType/ArrayTestCase.php +++ b/tests/Unit/DTO/Type/ArrayType/ArrayTestCase.php @@ -29,6 +29,8 @@ public function testMarshalling(): void yield 'foo'; })(); $dto->iterableNullable = null; + $dto->assoc = ['foo' => 'bar']; + $dto->assocOfType = ['foo' => (object)['baz' => 'bar']]; $result = $this->marshal($dto); $this->assertSame([ @@ -40,6 +42,8 @@ public function testMarshalling(): void 'nullableBar' => null, 'iterable' => ['foo'], 'iterableNullable' => null, + 'assoc' => ['foo' => 'bar'], + 'assocOfType' => ['foo' => ['baz' => 'bar']], ], $result); } @@ -54,6 +58,8 @@ public function testUnmarshalling(): void 'nullableBar' => null, 'iterable' => ['it'], 'iterableNullable' => ['itn'], + 'assoc' => ['foo' => 'bar'], + 'assocOfType' => ['key' => ['foo' => 'bar']], ], new ArrayDto()); $this->assertSame(['foo'], $dto->foo); @@ -64,6 +70,8 @@ public function testUnmarshalling(): void $this->assertSame(['it'], $dto->iterable); $this->assertSame(['itn'], $dto->iterableNullable); $this->assertSame(null, $dto->nullableBar); + $this->assertSame(['foo' => 'bar'], $dto->assoc); + $this->assertEquals(['key' => (object)['foo' => 'bar']], $dto->assocOfType); } public function testSetNullToNotNullable(): void