Skip to content

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

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

Closed
AlexisL61 opened this issue May 14, 2025 · 3 comments
Closed

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

AlexisL61 opened this issue May 14, 2025 · 3 comments

Comments

@AlexisL61
Copy link

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
@mttsch
Copy link
Contributor

mttsch commented Jun 3, 2025

@AlexisL61 This issue appears to be fixed with 5b2634f?

@AlexisL61
Copy link
Author

@mttsch I will test it tomorrow. I am keeping you updated!

@AlexisL61
Copy link
Author

I can confirm that the bug has been fixed on this commit.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 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