Skip to content

Commit

Permalink
Merge pull request #434 : Fix unmarshalling of typed assoc arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk authored May 15, 2024
2 parents 774ca97 + 3af4bf6 commit 954ce72
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Internal/Marshaller/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/DTO/Type/ArrayType/ArrayDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ class ArrayDto
public iterable $iterable;

public ?iterable $iterableNullable;

public array $assoc;

#[MarshalArray(of: \stdClass::class)]
public array $assocOfType;
}
8 changes: 8 additions & 0 deletions tests/Unit/DTO/Type/ArrayType/ArrayTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -40,6 +42,8 @@ public function testMarshalling(): void
'nullableBar' => null,
'iterable' => ['foo'],
'iterableNullable' => null,
'assoc' => ['foo' => 'bar'],
'assocOfType' => ['foo' => ['baz' => 'bar']],
], $result);
}

Expand All @@ -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);
Expand All @@ -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
Expand Down

0 comments on commit 954ce72

Please sign in to comment.