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 f9119da
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- [GH#180](https://github.com/jolicode/automapper/pull/180) Add configuration to generate code with strict types

### Fixed
- [GH#184](https://github.com/jolicode/automapper/pull/184) Fix error when mapping from stdClass to constructor with nullable/optional arguments

## [9.1.2] - 2024-09-03
### Fixed
- [GH#174](https://github.com/jolicode/automapper/pull/174) Fix race condition when writing generated mappers
Expand Down Expand Up @@ -157,7 +160,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- [GH#22](https://github.com/jolicode/automapper/pull/22) Added generic AST extractor
- [GH#21](https://github.com/jolicode/automapper/pull/21) Add VERSION constants within AutoMapper class and use it for transformers hashes

### Changed
- [GH#19](https://github.com/jolicode/automapper/pull/19) Use attributes everywhere instead of annotations
- [GH#18](https://github.com/jolicode/automapper/pull/18) Symfony 7 support
Expand Down
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 f9119da

Please # to comment.