Skip to content

Commit 69f7789

Browse files
authored
fix mapping array of enums to setter which expects array of enums (#280)
Fix #269 I found out that this bug was introduced in #230, to be more precise this lines [ArrayTransformerFactory](https://github.com/jolicode/automapper/blob/9.4.1/src/Transformer/ArrayTransformerFactory.php#L42-L45). Before #230 `$targetCollections` would be equal `[]` so we would get `return new DictionaryTransformer(new CopyTransformer());`. And CopyTransformer would just pass enums from property to `setPaymentMethods()`. But simply removing this lines wouldn't work because they do serve they purpose in fixing weird behavior in #230. So I dug further and found out original issue. Here [WriteMutator](https://github.com/jolicode/automapper/blob/9.4.1/src/Extractor/WriteMutator.php#L160-L165) should get that `setPaymentMethods()` expects `PaymentMethod[]` but gets blocked here [GetTypeTrait](https://github.com/jolicode/automapper/blob/9.4.1/src/Extractor/GetTypeTrait.php#L81) because argument `$property` equals `'setPaymentMethods'` and we get this check that result in false and we skip trying to extract types ```php '$paymentMethods' !== '$setPaymentMethods' ```
2 parents 7ed9746 + e8fbae4 commit 69f7789

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/Extractor/WriteMutator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public function getTypes(string $target): ?array
161161
$reflectionMethod->getDocComment(),
162162
$target,
163163
$reflectionMethod->getDeclaringClass()->getName(),
164-
$this->property,
164+
$reflectionMethod->getParameters()[0]->name,
165165
'@param'
166166
)) {
167167
return $types;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
AutoMapper\Tests\AutoMapperTest\Issue269\Entity {
2+
-paymentMethods: [
3+
AutoMapper\Tests\AutoMapperTest\Issue269\PaymentMethod {
4+
+name: "First"
5+
+value: "First"
6+
}
7+
]
8+
}

tests/AutoMapperTest/Issue269/map.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AutoMapper\Tests\AutoMapperTest\Issue269;
6+
7+
use AutoMapper\Tests\AutoMapperBuilder;
8+
9+
enum PaymentMethod: string
10+
{
11+
case First = 'First';
12+
}
13+
14+
class Dto
15+
{
16+
/** @var PaymentMethod[] */
17+
public ?array $paymentMethods = null;
18+
}
19+
20+
class Entity
21+
{
22+
/** @var PaymentMethod[] */
23+
private array $paymentMethods = [];
24+
25+
/** @param PaymentMethod[] $paymentMethods */
26+
public function setPaymentMethods(array $paymentMethods): self
27+
{
28+
$this->paymentMethods = $paymentMethods;
29+
30+
return $this;
31+
}
32+
33+
/** @return PaymentMethod[] */
34+
public function getPaymentMethods(): array
35+
{
36+
return $this->paymentMethods;
37+
}
38+
}
39+
40+
$dto = new Dto();
41+
$dto->paymentMethods = [PaymentMethod::First];
42+
43+
return AutoMapperBuilder::buildAutoMapper()->map($dto, Entity::class);

0 commit comments

Comments
 (0)