Skip to content

Commit

Permalink
fix: error when mapping from stdClass to constructor with nullable/op…
Browse files Browse the repository at this point in the history
…tional arguments
  • Loading branch information
MrMeshok committed Sep 12, 2024
1 parent 8fa3035 commit 018ab22
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Transformer/NullableTransformerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ final class NullableTransformerFactory implements TransformerFactoryInterface, P

public function getTransformer(TypesMatching $types, SourcePropertyMetadata $source, TargetPropertyMetadata $target, MapperMetadata $mapperMetadata): ?TransformerInterface
{
if (null !== $target->parameterInConstructor) {
return null;
}

$sourceType = $types->getSourceUniqueType();

if (null === $sourceType) {
Expand Down
9 changes: 9 additions & 0 deletions tests/AutoMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,15 @@ public function testConstructorForcedException(): void
$this->autoMapper->map($data, ConstructorWithDefaultValues::class);
}

public function testConstructorWithDefaultFromStdClass(): void
{
$data = (object) ['baz' => 'baz'];
/** @var ConstructorWithDefaultValues $object */
$object = $this->autoMapper->map($data, ConstructorWithDefaultValues::class);

self::assertInstanceOf(ConstructorWithDefaultValues::class, $object);
}

public function testConstructorWithDefault(): void
{
$user = new Fixtures\UserDTONoAge();
Expand Down

0 comments on commit 018ab22

Please sign in to comment.