Skip to content

[ObjectMapper] Condition to target a specific class #60028

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

Merged
merged 1 commit into from
May 6, 2025

Conversation

soyuka
Copy link
Contributor

@soyuka soyuka commented Mar 24, 2025

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
License MIT

We want to be able to choose which property to map according to the target. Here foo is mapped to B only when the target is B. If C has a foo property it won't be mapped as we only map to bar.

use Symfony\Component\ObjectMapper\Attribute\Map;
use Symfony\Component\ObjectMapper\TargetClass;

#[Map(target: B::class)]
#[Map(target: C::class)]
class A
{
    #[Map(target: 'foo', transform: 'strtoupper', if: new TargetClass(B::class))]
    #[Map(target: 'bar')]
    public string $something = 'test';

    public string $doesNotExistInTargetB = 'foo';
}

This is a good alternative to groups as we can have one class that has multiple representation.

@OskarStark
Copy link
Contributor

I extracted the CS fixes into

to keep this PR clean

@soyuka soyuka force-pushed the allow-different-targets branch from 564b44b to 81039a8 Compare March 25, 2025 08:57
@soyuka soyuka closed this Mar 25, 2025
@soyuka soyuka force-pushed the allow-different-targets branch from 81039a8 to f803dc1 Compare March 25, 2025 13:17
@soyuka soyuka reopened this Mar 25, 2025
@soyuka soyuka force-pushed the allow-different-targets branch from 95783e6 to 2b46190 Compare March 27, 2025 11:41
@soyuka soyuka force-pushed the allow-different-targets branch from 2b46190 to 377a6a6 Compare March 27, 2025 15:44
@soyuka soyuka changed the title [ObjectMapper] Target in the transform and condition callback [ObjectMapper] Condition allows target class name Mar 27, 2025
@soyuka soyuka force-pushed the allow-different-targets branch from 377a6a6 to 388f5ab Compare March 27, 2025 15:46
@soyuka
Copy link
Contributor Author

soyuka commented Mar 27, 2025

Ready for review @mtarld @GromNaN

@soyuka soyuka force-pushed the allow-different-targets branch 4 times, most recently from a60b582 to ca88a41 Compare March 28, 2025 08:20
@GromNaN
Copy link
Member

GromNaN commented Mar 31, 2025

The feature is relevant, but I find the syntax of the attribute is not explicit.

I would find it clearer if the target contained [class, property] instead. That would let the if for an actual condition.

- #[Map(target: 'foo', transform: 'strtoupper', if: B::class)]
+ #[Map(target: [B::class, 'foo'], transform: 'strtoupper']

@soyuka
Copy link
Contributor Author

soyuka commented Mar 31, 2025

I would find it clearer if the target contained [class, property] instead. That would let the if for an actual condition.

This was my first attempt but its weird to use a Closure that's not interpreted as a Closure no? Also the target is either a class or a property name, using this would require more code changes. instead of

@GromNaN
Copy link
Member

GromNaN commented Mar 31, 2025

Then, can we add a targetClass property to the attribute? We'd need separate attribute classes since the properties diverge: MapClass and MapProperty.

@soyuka
Copy link
Contributor Author

soyuka commented Mar 31, 2025

targetClass is confusing with target, not sure this needs to split attributes in two for now and if we did MapProperty would have class and property. What's wrong with just having the class on the if?

#[Map(if: fn($target) => $target instance of ClassName)] 

using the class name is just a shortcut for that. Although to make this work we would also need to add the target to our callbacks.

To re-center the debate I think symfony should allow some use cases with a as straightforward as possible DX, when users want more behavior it's quite feasible using the MetadataFactory.

@GromNaN
Copy link
Member

GromNaN commented Mar 31, 2025

Adding the target after the value and the source, sounds like a good idea. The condition callable signature would become function (mixed $value, object $source, object $target).

The if condition can be an invokable object that implements ConditionCallableInterface.

readonly class TargetClass implements ConditionCallableInterface {
    public function __construct(private string $targetClass) {}
    public function __invoke(mixed $value, object $source, object $target): bool {
        return is_a($this->targetClass, $target, true);
    }
}

Usage:

#[Map(target: B::class)]
class A
{
    #[Map(target: 'foo', transform: 'strtoupper', if: new TargetClass(B::class))]
    public string $something = 'test';
}

That way, it's explicit, it doesn't require the PHP 8.5 closures in attributes and keep a good DX. Later you can combine more conditions by nesting objects.

Experimentation: soyuka#1

@soyuka soyuka force-pushed the allow-different-targets branch 3 times, most recently from c826e6d to 53b56d6 Compare April 2, 2025 10:13
@joelwurtz
Copy link
Contributor

I'm being late on this, but having this check on the if condition will avoid potential optimization later if there is other conditions.

It's due to the fact, that those kind of condition (evaluating a target class) can be easily checked during metadata extraction. But if there is future condition that induce runtime check it will be impossible to extrapolate this check during metadata extraction (since it's too generic) so we will have to do it each time

@joelwurtz
Copy link
Contributor

Maybe something like this would be better :

#[Map(target: B::class)]
#[Map(target: C::class)]
class A
{
    #[Map(to: 'foo', target: B::class, transform: 'strtoupper')]
    #[Map(to: 'bar')]
    public string $something = 'test';

    public string $doesNotExistInTargetB = 'foo';
}

It keep target consistent with the attribute being on either a property or the class,

However this is a hard change in current api (but since it's not released i think it's ok ?)

@OskarStark
Copy link
Contributor

It would be no problem yes

@soyuka
Copy link
Contributor Author

soyuka commented Apr 2, 2025

@joelwurtz I think it's too confusing to have to/from and source/target. I don't share the concerns as we can easily take this check as a "static" check that we can induce during the metadata extraction. We could also add a particular interface that would force these checks at runtime instead.

@joelwurtz
Copy link
Contributor

joelwurtz commented Apr 2, 2025

Personally i find this confusing to have the same property on the same attribute that does not mean the same thing given the position of the attribute :/ (as an user i would expect a property having the same meaning)

I don't share the concerns as we can easily take this check as a "static" check that we can induce during the metadata extraction. We could also add a particular interface that would force these checks at runtime instead.

Not sure how it would work since they may be check that combine multiple checks ?

I just find this complex given the use case

EDIT :

Also this could be :

#[Map(property: 'foo', target: B::class, transform: 'strtoupper')]

(Could be also rename ? or any other name)

So no to / from, work both ways

@soyuka
Copy link
Contributor Author

soyuka commented Apr 3, 2025

I'm not sure we need to anticipate these use cases quite yet as we're still a long way to go before adding performance optimizations. I suggest that we leave this like this: the if adds a condition, and it's quite clear that "map only when this class is the target" is a condition. I want this API to be tried in user-land and we'll see if the use cases don't match we'll consider changing the whole API. Same goes when we will want to introduce performance with a cache warmup we'll see how this goes.

Not sure how it would work since they may be check that combine multiple checks ?

Sure but then they would be executed at runtime, just like transformations. Let's say we have class TargetClass implements StaticConditionInterface then we know we can execute the condition when doing an optimization pass. If not, we just use the condition at runtime.

Personally i find this confusing to have the same property on the same attribute that does not mean the same thing given the position of the attribute

You mean having the target being a class on the class attribute and a property on the property attribute? Then we shall have two different attributes and it brings other issues (like mapping an embed object).

Copy link
Contributor

@mtarld mtarld left a comment

Choose a reason for hiding this comment

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

With minor comments

@joelwurtz
Copy link
Contributor

You mean having the target being a class on the class attribute and a property on the property attribute? Then we shall have two different attributes and it brings other issues (like mapping an embed object).

Yes that was i mean, i think consistent wording is important across a component, specially when this parameter can be considered as a "configuration" parameter having different meaning for the same word in the same component will induce a lot of friction IMO

@chalasr
Copy link
Member

chalasr commented Apr 15, 2025

Although the potential confusion seems real, making that setting context-dependent (i.e. expecting a property if used on a property vs a FQCN if used on a class) looks sensible to me. Throwing meaningful errors on such mistake should be good enough, do we have that?

@soyuka
Copy link
Contributor Author

soyuka commented Apr 16, 2025

IMHO its outside of the current scope of this PR we can always open a new issue to discuss this (or add this to the RFC issue)

@fabpot fabpot force-pushed the allow-different-targets branch from fd55ff0 to a5698aa Compare May 6, 2025 06:50
@fabpot
Copy link
Member

fabpot commented May 6, 2025

Thank you @soyuka.

@fabpot fabpot merged commit 7379bfb into symfony:7.3 May 6, 2025
9 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants