Skip to content

[ObjectMapper] Add support for nested source #62499

@TakTaki

Description

@TakTaki

Description

Hi,

I'm trying to work with the #[Map(source: XXX)] and it seems to me that nested object with source mapped is not working properly.

I'm not really sure if that is something that should work or not ?
Thank you !

Example

Here is a simple example :

final readonly class A {
    public function __construct(
        public B $b,
        public string $var1,
    ) {}
}

final readonly class B {
    public function __construct(
        public string $var2,
    ) {}
}

#[Map(source: A::class)]
final readonly class AMapped {
    public function __construct(
        #[Map(transform: BMapped::class)]
        public BMapped $b,
        #[Map(source: 'var1')]
        public string $var1Mapped,
    ) {}
}

#[Map(source: B::class)]
final readonly class BMapped {
    public function __construct(
        #[Map(source: 'var2')]
        public string $var2Mapped,
    ) {}
}

/** @var AMapped $out */
$out = (new ObjectMapper())->map(
    source: new A(
        b: new B(
            var2: 'bar',
        ),
        var1: 'foo',
    ),
    target: AMapped::class,
);
var_dump($out);
echo $out->b->var2Mapped;

This output :

TypeError: AMapped::__construct(): Argument #1 ($b) must be of type BMapped, B given, called in /var/www/html/vendor/symfony/object-mapper/ObjectMapper.php on line 167

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