Skip to content

Commit c3a8fb1

Browse files
authored
fix: regression after #184 (#187)
Turns out NullableTransformer is needed when creating from constructor if we have argument what expects nullable object
2 parents 76e25b5 + 49135b6 commit c3a8fb1

File tree

4 files changed

+5
-7
lines changed

4 files changed

+5
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
### Fixed
1313
- [GH#184](https://github.com/jolicode/automapper/pull/184) Fix error when mapping from stdClass to constructor with nullable/optional arguments
1414
- [GH#185](https://github.com/jolicode/automapper/pull/185) Fix constructor with default parameter array does not work with constructor_arguments context
15+
- [GH#187](https://github.com/jolicode/automapper/pull/187) Fix regression after [GH#184](https://github.com/jolicode/automapper/pull/184)
1516

1617
## [9.1.2] - 2024-09-03
1718
### Fixed

src/Transformer/NullableTransformer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use PhpParser\Node\Stmt;
1212

1313
/**
14-
* Tansformer decorator to handle null values.
14+
* Transformer decorator to handle null values.
1515
*
1616
* @author Joel Wurtz <[email protected]>
1717
*
@@ -51,9 +51,9 @@ public function transform(Expr $input, Expr $target, PropertyMetadata $propertyM
5151
$itemStatements[] = new Stmt\Expression(new $assignClass($newOutput, $output));
5252
}
5353

54-
if ($input instanceof Expr\ArrayDimFetch) {
54+
if ($propertyMapping->source->checkExists) {
5555
/*
56-
* if `$input` is an array access, let's validate if the array key exists and is not null:
56+
* if `$input` is an array access or stdClass, let's validate if the key exists and is not null:
5757
*
5858
* if (isset($value['key'])) {
5959
*/

src/Transformer/NullableTransformerFactory.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ 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-
2824
$sourceType = $types->getSourceUniqueType();
2925

3026
if (null === $sourceType) {

tests/Fixtures/ConstructorWithDefaultValues.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public function __construct(
1111
public ?int $foo = 1,
1212
public int $bar = 0,
1313
public array $someOtters = [],
14+
public ?IntDTO $nullableObject = null,
1415
) {
1516
}
1617
}

0 commit comments

Comments
 (0)