Skip to content

[ObjectMapper] Correctly manage constructor initialization #61036

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

Conversation

alanpoulain
Copy link
Contributor

Q A
Branch? 7.3
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #60797 Fix #60807
License MIT

Currently the ObjectMapper cannot support empty constructor initialization, which is particularly useful for Doctrine collections:

class Article
{
    #[ORM\ManyToMany(targetEntity: Tag::class)]
    private Collection $tags;

    public function __construct()
    {
        $this->tags = new ArrayCollection();
    }

    public function addTag($tag)
    {
         // ...
    }

    public function removeTag($tag)
    {
         // ...
    }
}

This kind of error is thrown by the PropertyAccessor:

Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException: The property "Symfony\Component\ObjectMapper\Tests\Fixtures\InitializedConstructor\B::$tags" is not readable because it is typed "array". You should initialize it or declare a default value instead.

This PR fixes this but also another related issue (#60807): since an "empty" (no constructor arguments) constructor is always called, the class-level transformation test does not pass anymore:

Symfony\Component\ObjectMapper\Tests\ObjectMapperTest::testMapToWithInstanceHook
ArgumentCountError: Too few arguments to function Symfony\Component\ObjectMapper\Tests\Fixtures\InstanceCallback\B::__construct(), 0 passed in /home/workspace/symfony/symfony/src/Symfony/Component/ObjectMapper/ObjectMapper.php on line 151 and exactly 1 expected

To solve this, a check is done on map?->transform.

@carsonbot
Copy link

Hey!

Thanks for your PR. You are targeting branch "7.4" but it seems your PR description refers to branch "7.3".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

@alanpoulain alanpoulain changed the base branch from 7.4 to 7.3 July 4, 2025 15:27
@@ -146,7 +146,7 @@ public function map(object $source, object|string|null $target = null): object
}
}

if (!$mappingToObject && $ctorArguments && $constructor) {
if (!$mappingToObject && !$map?->transform && $constructor) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because in case of a transform we should've already built the object is that right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's right.

@nicolas-grekas nicolas-grekas modified the milestones: 7.4, 7.3 Jul 7, 2025
@nicolas-grekas
Copy link
Member

Thank you @alanpoulain.

@nicolas-grekas nicolas-grekas merged commit 31c3b33 into symfony:7.3 Jul 7, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy