Skip to content

Commit 018ab22

Browse files
committed
fix: error when mapping from stdClass to constructor with nullable/optional arguments
1 parent 8fa3035 commit 018ab22

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/Transformer/NullableTransformerFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ final class NullableTransformerFactory implements TransformerFactoryInterface, P
2121

2222
public function getTransformer(TypesMatching $types, SourcePropertyMetadata $source, TargetPropertyMetadata $target, MapperMetadata $mapperMetadata): ?TransformerInterface
2323
{
24+
if (null !== $target->parameterInConstructor) {
25+
return null;
26+
}
27+
2428
$sourceType = $types->getSourceUniqueType();
2529

2630
if (null === $sourceType) {

tests/AutoMapperTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,15 @@ public function testConstructorForcedException(): void
525525
$this->autoMapper->map($data, ConstructorWithDefaultValues::class);
526526
}
527527

528+
public function testConstructorWithDefaultFromStdClass(): void
529+
{
530+
$data = (object) ['baz' => 'baz'];
531+
/** @var ConstructorWithDefaultValues $object */
532+
$object = $this->autoMapper->map($data, ConstructorWithDefaultValues::class);
533+
534+
self::assertInstanceOf(ConstructorWithDefaultValues::class, $object);
535+
}
536+
528537
public function testConstructorWithDefault(): void
529538
{
530539
$user = new Fixtures\UserDTONoAge();

0 commit comments

Comments
 (0)