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

Support for Nested Property Mapping for Array to Object mapping #176

Open
TheoD02 opened this issue Jul 27, 2024 · 2 comments
Open

Support for Nested Property Mapping for Array to Object mapping #176

TheoD02 opened this issue Jul 27, 2024 · 2 comments
Labels
documentation Improvements or additions to documentation

Comments

@TheoD02
Copy link
Contributor

TheoD02 commented Jul 27, 2024

Description

I would like to map using nested properties for array to object mapping in the current implementation.

Example

Given an input array:

[
    'saved_at' => '2024-01-01',
    'track' => [
        'uri' => 'uri-code',
     ]
]

And the following entity definition:

#[ORM\Entity(repositoryClass: TrackRepository::class)]
class Track
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[MapFrom(source: 'array', property: 'track.uri')] // tested with [track][uri] like PropertyAccessor
    #[ORM\Column(length: 100)]
    private ?string $uri = null;

    // ... other properties and methods
}

Issue

Currently, when attempting to map the nested track.uri property to the Track entity's uri property, the generated mapping does not appear to handle nested properties as expected. The generated code is:

if (array_key_exists('track.uri', $value) && \AutoMapper\MapperContext::isAllowedAttribute($context, 'track.uri', !isset($value['track.uri'])) && (!array_key_exists('groups', $context) || !$context['groups'])) {
    $result->setUri($value['track.uri']);
}

The code attempts to access track.uri as a single key in the array, instead of navigating through the nested structure.

Expected Behavior

The mapping should properly navigate the nested array structure and map the track.uri value to the uri property of the Track entity. For instance:

if (isset($value['track']['uri']) && \AutoMapper\MapperContext::isAllowedAttribute($context, 'track.uri', !isset($value['track']['uri'])) && (!array_key_exists('groups', $context) || !$context['groups'])) {
    $result->setUri($value['track']['uri']);
}

Questions

  1. Is it possible to support nested properties for array to object mapping?
  2. If not currently supported, are there plans to implement this feature?
@dsoriano
Copy link

👍 It would be great.
As a workaroud, you can use transformer :

    #[MapFrom(source: 'array', property: 'track', transformer: "source['track']['uri'] ?? null")]
    #[ORM\Column(length: 100)]
    private ?string $uri = null;

@joelwurtz
Copy link
Member

You should indeed use a transformer for that, we don't want to do property interpolation here, some api may provide a dot in their property name (weird but already seen it).

I keep this open as we should certainly provide a documentation about how to do this (not the first time it is asked)

@joelwurtz joelwurtz added the documentation Improvements or additions to documentation label Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants