Skip to content

Commit

Permalink
fix: regression after #184 (#187)
Browse files Browse the repository at this point in the history
Turns out NullableTransformer is needed when creating from constructor
if we have argument what expects nullable object
  • Loading branch information
Korbeil authored Sep 13, 2024
2 parents 76e25b5 + 49135b6 commit c3a8fb1
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- [GH#184](https://github.com/jolicode/automapper/pull/184) Fix error when mapping from stdClass to constructor with nullable/optional arguments
- [GH#185](https://github.com/jolicode/automapper/pull/185) Fix constructor with default parameter array does not work with constructor_arguments context
- [GH#187](https://github.com/jolicode/automapper/pull/187) Fix regression after [GH#184](https://github.com/jolicode/automapper/pull/184)

## [9.1.2] - 2024-09-03
### Fixed
Expand Down
6 changes: 3 additions & 3 deletions src/Transformer/NullableTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use PhpParser\Node\Stmt;

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

if ($input instanceof Expr\ArrayDimFetch) {
if ($propertyMapping->source->checkExists) {
/*
* if `$input` is an array access, let's validate if the array key exists and is not null:
* if `$input` is an array access or stdClass, let's validate if the key exists and is not null:
*
* if (isset($value['key'])) {
*/
Expand Down
4 changes: 0 additions & 4 deletions src/Transformer/NullableTransformerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ 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
1 change: 1 addition & 0 deletions tests/Fixtures/ConstructorWithDefaultValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public function __construct(
public ?int $foo = 1,
public int $bar = 0,
public array $someOtters = [],
public ?IntDTO $nullableObject = null,
) {
}
}

0 comments on commit c3a8fb1

Please sign in to comment.