Skip to content

[ObjectMapper] ObjectMapper::map calling constructor without any arguments #61348

@PeKowa

Description

@PeKowa

Symfony version(s) affected

7.3.2

Description

There seems to be an issue introduced by #61036.

When calling map with a FQDN as target the mapping fails because the constructor is called without any arguments. Prior to the change the constructor was not called at all because $ctorArguments is empty.

The issue does not occur when property promotion is being used because:

if (!$parameter->isPromoted()) {
    continue;
}

does not lead to the filling of $ctorArguments being skipped.

How to reproduce

<?php

namespace App;

class Foo {
    public string $bar;

    public function __construct(string $bar) {
        $this->bar = $bar;
    }
}
<?php

namespace App\Controller;

use App\Foo;
use stdClass;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\ObjectMapper\ObjectMapperInterface;

#[AsCommand(name: 'test')]
class TestCommand extends Command
{
    public function __construct(
        private readonly ObjectMapperInterface $mapper,
    ) {
        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $object = new stdClass();
        $object->bar = 'bar';

        $foo = $this->mapper->map($object, Foo::class);

        return Command::SUCCESS;
    }
}

Too few arguments to function App\Foo::__construct(), 0 passed in /var/www/html/vendor/symfony/object-mapper/ObjectMapper.php on line 159 and exactly 1 expected

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      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