Skip to content

Commit 954ce72

Browse files
authored
Merge pull request #434 : Fix unmarshalling of typed assoc arrays
2 parents 774ca97 + 3af4bf6 commit 954ce72

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

src/Internal/Marshaller/Type/ArrayType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function parse($value, $current)
8383
$result = [];
8484

8585
foreach ($value as $i => $item) {
86-
$result[] = $this->type->parse($item, $current[$i] ?? null);
86+
$result[$i] = $this->type->parse($item, $current[$i] ?? null);
8787
}
8888

8989
return $result;

tests/Unit/DTO/Type/ArrayType/ArrayDto.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ class ArrayDto
3333
public iterable $iterable;
3434

3535
public ?iterable $iterableNullable;
36+
37+
public array $assoc;
38+
39+
#[MarshalArray(of: \stdClass::class)]
40+
public array $assocOfType;
3641
}

tests/Unit/DTO/Type/ArrayType/ArrayTestCase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function testMarshalling(): void
2929
yield 'foo';
3030
})();
3131
$dto->iterableNullable = null;
32+
$dto->assoc = ['foo' => 'bar'];
33+
$dto->assocOfType = ['foo' => (object)['baz' => 'bar']];
3234

3335
$result = $this->marshal($dto);
3436
$this->assertSame([
@@ -40,6 +42,8 @@ public function testMarshalling(): void
4042
'nullableBar' => null,
4143
'iterable' => ['foo'],
4244
'iterableNullable' => null,
45+
'assoc' => ['foo' => 'bar'],
46+
'assocOfType' => ['foo' => ['baz' => 'bar']],
4347
], $result);
4448
}
4549

@@ -54,6 +58,8 @@ public function testUnmarshalling(): void
5458
'nullableBar' => null,
5559
'iterable' => ['it'],
5660
'iterableNullable' => ['itn'],
61+
'assoc' => ['foo' => 'bar'],
62+
'assocOfType' => ['key' => ['foo' => 'bar']],
5763
], new ArrayDto());
5864

5965
$this->assertSame(['foo'], $dto->foo);
@@ -64,6 +70,8 @@ public function testUnmarshalling(): void
6470
$this->assertSame(['it'], $dto->iterable);
6571
$this->assertSame(['itn'], $dto->iterableNullable);
6672
$this->assertSame(null, $dto->nullableBar);
73+
$this->assertSame(['foo' => 'bar'], $dto->assoc);
74+
$this->assertEquals(['key' => (object)['foo' => 'bar']], $dto->assocOfType);
6775
}
6876

6977
public function testSetNullToNotNullable(): void

0 commit comments

Comments
 (0)