automapper
automapper copied to clipboard
Mapper doesn't differentiate between property and getter
I ran into an issue where a target object wasn't populated as expected, due to the getter being the same name as the protected property.
//source: Order::class (abbreviated)
/** @var ?LineItem[] */
protected ?array $lineItems = null;
/**
* @return ?LineItem[]
*/
public function getLineItems(
): ?array {
return $this->lineItems;
}
//target: CartView::class (abbreviated)
/** @var ?LineItem[] $items */
#[MapFrom(source: Order::class, property: 'line_items')]
public ?array $items = null;
After dumping the GeneratorMetaData, I noticed that lineItems was being ignored, with the following reason: "Property cannot be read from source, and the attached transformer require a value.".
Not sure if it's an actual issue with the symfony property component itself, but it looks like after detecting the getter for "line_items", it then tries to use the protected method directly.
And is there maybe a way to specify the method directly? For now, I've just added a wrapper method that calls getLineItems, but that's obviously not ideal.