Skip to content

[ObjectMapper] embed collection transformer #60442

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

Merged
merged 1 commit into from
Aug 23, 2025
Merged

Conversation

soyuka
Copy link
Contributor

@soyuka soyuka commented May 17, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues Related to #60432 also fixes #61116
License MIT

This is another approach to embeded collection mapping that avoids changing the ObjectMapper. Even if this doesn't land in the component it'd be a good thing to document. Let me know your thoughts.

@soyuka soyuka force-pushed the map-collection branch 2 times, most recently from 36eef03 to 2da896a Compare May 19, 2025 08:49
@soyuka
Copy link
Contributor Author

soyuka commented Jul 16, 2025

We're already using this on a project and it works quite well.

@crtl
Copy link
Contributor

crtl commented Aug 4, 2025

How do you use it? How is this supposed when using sources only?
Ive tried using MapCollection transformer but it will throw exception:

class UserDto {
/**
     * @var OrganizationMembershipDto[]
     */
    #[Map(transform: new MapCollection(), source: "memberships")]
    public array $memberships;
    
}
$dto = $this->mapper->map($user, UserDto::class);

will throw:

Symfony\Component\ObjectMapper\Exception\MappingException : Mapping target not found for source "App\Entity\User\OrganizationMembership".
 /var/www/vendor/symfony/object-mapper/ObjectMapper.php:58
 /var/www/src/Dto/Response/ObjectMapper/Transformer/MapCollection.php:30
 /var/www/vendor/symfony/object-mapper/ObjectMapper.php:225
 /var/www/vendor/symfony/object-mapper/ObjectMapper.php:259
 /var/www/vendor/symfony/object-mapper/ObjectMapper.php:176
 /var/www/vendor/symfony/object-mapper/ObjectMapper.php:139

Like ive mentioned in #61116 theres is currently no API in object mapper to provide metadata to transformers and without this transformers are unable to guess type of target value.
Having to set the target on the source breaks SRP and erodes the actual purpose of object mapper.

TDLR; Only works when mapping target is defined on source.

@soyuka
Copy link
Contributor Author

soyuka commented Aug 6, 2025

@crtl related to #61310

@soyuka soyuka force-pushed the map-collection branch 2 times, most recently from 39aab74 to 68b8bdd Compare August 19, 2025 13:52
@fabpot
Copy link
Member

fabpot commented Aug 23, 2025

Thank you @soyuka.

@fabpot fabpot merged commit cd42aac into symfony:7.4 Aug 23, 2025
10 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ObjectMapper] Support mapping to nested arrays of objects
9 participants