Skip to content

Commit

Permalink
Merge pull request #22 from sitegeist/bugfix/fixOtionalParametersHand…
Browse files Browse the repository at this point in the history
…ling

BUGFIX: Fix optional parameter handling in deserializer
  • Loading branch information
mwuest authored Jun 19, 2024
2 parents cafa8fb + 9fcf7eb commit bdc3c62
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Classes/Domain/Schema/SchemaDenormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ private static function convertValueObject(array|int|float|string|bool $value, s
$parameterReflections = $reflection->getConstructor()->getParameters();
$convertedArguments = [];
if (is_array($value)) {
foreach ($parameterReflections as $name => $reflectionParameter) {
foreach ($parameterReflections as $reflectionParameter) {
$name = $reflectionParameter->getName();
$type = $reflectionParameter->getType();
if ($reflectionParameter->isDefaultValueAvailable() && !array_key_exists($reflectionParameter->getName(), $value)) {
continue;
Expand Down
24 changes: 24 additions & 0 deletions Tests/Unit/Domain/Schema/SchemaNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@

final class SchemaNormalizerTest extends TestCase
{
public static function denormalizeObjectsWithOptionalParametersDataProvider(): \Generator
{
yield 'min PostalAddress' => [
Fixtures\PostalAddress::class,
['streetAddress' => 'Sesamstraße 42', 'addressRegion' => 'Muppetingen'],
new Fixtures\PostalAddress(streetAddress: 'Sesamstraße 42', addressRegion: 'Muppetingen')
];
yield 'PostalAddress with missing parameter in between' => [
Fixtures\PostalAddress::class,
['streetAddress' => 'Dämonenweg 23', 'addressRegion' => 'Hölle', 'postOfficeBoxNumber' => '666'],
new Fixtures\PostalAddress(streetAddress: 'Dämonenweg 23', addressRegion: 'Hölle', postOfficeBoxNumber: '666')
];
}

/**
* @dataProvider denormalizeObjectsWithOptionalParametersDataProvider
* @test
*/
public function denormalizeObjectsWithOptionalParameters(mixed $type, mixed $data, mixed $expected): void
{
Assert::assertEquals($expected, SchemaDenormalizer::denormalizeValue($data, $type));
}


/**
* @dataProvider valueNormalizationPairs
* @test
Expand Down

0 comments on commit bdc3c62

Please sign in to comment.