Skip to content

Commit f9119da

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

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- [GH#180](https://github.com/jolicode/automapper/pull/180) Add configuration to generate code with strict types
1111

12+
### Fixed
13+
- [GH#184](https://github.com/jolicode/automapper/pull/184) Fix error when mapping from stdClass to constructor with nullable/optional arguments
14+
1215
## [9.1.2] - 2024-09-03
1316
### Fixed
1417
- [GH#174](https://github.com/jolicode/automapper/pull/174) Fix race condition when writing generated mappers
@@ -157,7 +160,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
157160
### Added
158161
- [GH#22](https://github.com/jolicode/automapper/pull/22) Added generic AST extractor
159162
- [GH#21](https://github.com/jolicode/automapper/pull/21) Add VERSION constants within AutoMapper class and use it for transformers hashes
160-
163+
161164
### Changed
162165
- [GH#19](https://github.com/jolicode/automapper/pull/19) Use attributes everywhere instead of annotations
163166
- [GH#18](https://github.com/jolicode/automapper/pull/18) Symfony 7 support

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)