Skip to content

Commit

Permalink
fix: mapping unset property to not null target property (#116)
Browse files Browse the repository at this point in the history
* fix: mapping unset property to not null target property

* the fix

* psalm
  • Loading branch information
priyadi authored Sep 21, 2024
1 parent f720d59 commit ce8108e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* test: modernize tests
* feat: union types support in object mappers
* feat: union types support in property mappers
* fix: mapping unset source property to not null target property

## 1.6.1

Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<file name="tests/src/Fixtures/UninitializedPropertyDto/ObjectWithUninitializedPropertyDto.php" />
<file name="tests/src/Fixtures/ArrayLikeDto/ObjectWithNotNullArrayAccessPropertyDto.php" />
<file name="tests/src/Fixtures/ArrayLikeDto/ObjectWithNotNullTraversablePropertyDto.php" />
<file name="tests/src/Fixtures/DynamicProperty/ObjectWithNonNullPropertyThatCannotBeCastFromNull.php" />
</errorLevel>
</MissingConstructor>
<ArgumentTypeCoercion>
Expand Down
13 changes: 11 additions & 2 deletions src/Transformer/Util/ReaderWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,21 @@ public function readSourceProperty(
$accessorName = $propertyMapping->getSourceReadName();
$mode = $propertyMapping->getSourceReadMode();

if ($accessorName === null) {
throw new UninitializedSourcePropertyException('(null)');
}

if ($mode === ReadMode::Property) {
return $source->{$accessorName};
} elseif ($mode === ReadMode::Method) {
/** @psalm-suppress MixedMethodCall */
return $source->{$accessorName}();
} elseif ($mode === ReadMode::DynamicProperty) {
return $source->{$accessorName} ?? null;
if (!property_exists($source, $accessorName)) {
throw new UninitializedSourcePropertyException($accessorName);
}

return $source->{$accessorName};
}

return null;
Expand Down Expand Up @@ -96,7 +104,8 @@ public function readTargetProperty(
$propertyMapping->getTargetSetterWriteMode() === WriteMode::AdderRemover
&& $propertyMapping->getTargetSetterWriteVisibility() === Visibility::Public
) {
if ($propertyMapping->getTargetRemoverWriteVisibility() === Visibility::Public
if (
$propertyMapping->getTargetRemoverWriteVisibility() === Visibility::Public
) {
$removerMethodName = $propertyMapping->getTargetRemoverWriteName();
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/mapper package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\Mapper\Tests\Fixtures\DynamicProperty;

final class ObjectWithNonNullPropertyThatCannotBeCastFromNull
{
public \DateTimeInterface $date;
}
9 changes: 9 additions & 0 deletions tests/src/IntegrationTest/DynamicPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Rekalogika\Mapper\Tests\Fixtures\DynamicProperty\ObjectExtendingStdClass;
use Rekalogika\Mapper\Tests\Fixtures\DynamicProperty\ObjectExtendingStdClassWithExplicitScalarProperties;
use Rekalogika\Mapper\Tests\Fixtures\DynamicProperty\ObjectExtendingStdClassWithProperties;
use Rekalogika\Mapper\Tests\Fixtures\DynamicProperty\ObjectWithNonNullPropertyThatCannotBeCastFromNull;
use Rekalogika\Mapper\Tests\Fixtures\Scalar\ObjectWithScalarProperties;
use Rekalogika\Mapper\Tests\Fixtures\ScalarDto\ObjectWithScalarPropertiesDto;

Expand Down Expand Up @@ -232,4 +233,12 @@ public function testStdClassToStdClassWithExistingNullValue(): void
*/
$this->assertInstanceOf(ObjectWithScalarProperties::class, $target->property);
}

public function testStdClassToObjectWithNotNullProperty(): void
{
$source = new ObjectExtendingStdClass();
$result = $this->mapper->map($source, ObjectWithNonNullPropertyThatCannotBeCastFromNull::class);

$this->assertFalse(isset($result->date));
}
}

0 comments on commit ce8108e

Please sign in to comment.