-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Description
Symfony version(s) affected
7.4
Description
Hi there,
I'm not sure, if it's really a bug or I'm using it wrong.
Into an Controller I'll get as JSON an object of arrays of objects. If not understand what I mean, an example is in the additional context, controller and DTO is in how to reproduce.
Sending an request to this, I'll get an exception: Symfony\Component\ObjectMapper\ObjectMapper::map(): Argument #1 ($source) must be of type object, array given, called in /var/www/html/project/vendor/symfony/object-mapper/Transform/MapCollection.php on line 39
Of course, I understand the mapper tries to map an array to an ArrayCollection of Entites. But how should this work, what was the programmer thinking?
How to reproduce
class ManySpecsDTO
{
public function __construct(
#[Map(target: "spec_1", transform: new MapCollection())]
/** @var Spec1[] */
public readonly array $spec_1,
#[Map(target: "spec_2", transform: new MapCollection())]
/** @var Spec2[] */
public readonly array $spec_2,
)
{
}
}
public function controllerMethod(
#[MapRequestPayload] ManySpecsDTO $manySpecsDTO)
{
}
Possible Solution
I wish to get two behaviors:
- In the RequestPayload-DTO an array of DTOs (without mapper?)
- in the Mapper-Target-Object an ArrayCollection of the request objects
With the documentation I don't get how this should work.
Additional Context
JSON Object of Request:
{
spec_1: [
{
spec_1_id: 1,
spec_1_name: "Test"
},
{
spec_1_id: 2,
spec_1_name: "Test 2"
}
],
spec_2: [
{
spec_2_id: 1,
spec_2_name: "Test"
},
{
spec_2_id: 2,
spec_2_name: "Test 2"
}
],
}
Could someone help here please?
Thank you