Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoMapper fails to map a nested array to a specified model type #203

Open
alexk136 opened this issue Oct 31, 2024 · 0 comments
Open

AutoMapper fails to map a nested array to a specified model type #203

alexk136 opened this issue Oct 31, 2024 · 0 comments

Comments

@alexk136
Copy link

When trying to use AutoMapper to map a nested array structure to a nested model type in Symfony, an error occurs due to the configuration in Symfony\Component\PropertyInfo\Type, which sets the collection attribute to true for nested arrays. As a result, AutoMapper interprets the endpoints property as an array rather than an object of type TestSubTarget, causing a TypeError.

Here’s a simplified version of the mapping logic to reproduce the issue:

class ClassContainer
{
    public ?array $endpoints = null;
}

class TestSubTarget
{
    public function __construct(
        private ?string $endpoint = null,
        private ?array $params = null,
    ) {}
}

class TestTarget
{
    public function __construct(
        private ?TestSubTarget $endpoints = null,
    ) {}
}

Steps to reproduce:

   $classContainer = new ClassContainer();
   $classContainer->endpoints = [
       'endpoint' => 'https://example.com',
       'params' => ['param1', 'param2'],
   ];
   
   $autoMapper->map($classContainer, TestTarget::class);

This triggers the error:

TypeError: TestTarget::__construct(): Argument #1 ($endpoints) must be of type ?TestSubTarget, array given.

It would be great to have an option to override the collection type or manually specify the target type for nested arrays in AutoMapper.

Automapper version: 9.1.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant