Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[ObjectMapper] Can't use if condition with source mapping #60418

Copy link
Copy link
Open
@AlexisL61

Description

@AlexisL61
Issue body actions

Symfony version(s) affected

7.3.0-BETA2

Description

Hello!

I have been trying to use the ObjectMapper to map my object from one to another using source mapping.
However, it appears that the if condition is not taken into account.

It works fine when using target mapping.

How to reproduce

I want to map my TestDTO to my TestResource but my DTO shouldn't have knowledge about my resource, that's why source mapping is used.

// TestResource.php

#[Map(
    source: TestDTO::class,
)]
class TestResource
{
    public string $id;

    #[Map(if: false)]
    public ?string $description = null;
}
class TestDTO
{
    public string $id;
}
$testDTO = new TestDTO();
$testDTO->id = '123';

$testResource = $this->objectMapper->map($testDTO, TestResource::class);

By using this code I receive the error:

 [Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException]
 Can't get a way to read the property "description" in class "App\DTO\API\TestDTO".

Possible Solution

This is linked to the propertyAccessor used in the getRawValue function here. The property doesn't exist in the source, and that's why it doesn't work as intended.

Additional Context

Stacktrace:

Exception trace:
  at /home/project/vendor/symfony/property-access/PropertyAccessor.php:461
 Symfony\Component\PropertyAccess\PropertyAccessor->readProperty() at /home/project/vendor/symfony/property-access/PropertyAccessor.php:102
 Symfony\Component\PropertyAccess\PropertyAccessor->getValue() at /home/project/vendor/symfony/object-mapper/ObjectMapper.php:170
 Symfony\Component\ObjectMapper\ObjectMapper->getRawValue() at /home/project/vendor/symfony/object-mapper/ObjectMapper.php:124
 Symfony\Component\ObjectMapper\ObjectMapper->map() at /home/project/src/Command/TestObjectMapperCommand.php:35
 App\Command\TestObjectMapperCommand->execute() at /home/project/vendor/symfony/console/Command/Command.php:318
 Symfony\Component\Console\Command\Command->run() at /home/project/vendor/symfony/console/Application.php:1092
 Symfony\Component\Console\Application->doRunCommand() at /home/project/vendor/symfony/framework-bundle/Console/Application.php:123
 Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /home/project/vendor/symfony/console/Application.php:341
 Symfony\Component\Console\Application->doRun() at /home/project/vendor/symfony/framework-bundle/Console/Application.php:77
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /home/project/vendor/symfony/console/Application.php:192
 Symfony\Component\Console\Application->run() at /home/project/vendor/symfony/runtime/Runner/Symfony/ConsoleApplicationRunner.php:49
 Symfony\Component\Runtime\Runner\Symfony\ConsoleApplicationRunner->run() at /home/project/vendor/autoload_runtime.php:29
 require_once() at /home/project/bin/console:15

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.