-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Serializer] Fix normalizing objects with accessors having the same name as a property #61097
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
Conversation
Another Edge-CaseAfter I just found #61023 I also tried having properties and getter methods called class ObjectWithBooleanPropertyAndIsserWithSameName
{
private $foo = 'foo';
private $isFoo = true;
private $canFoo = false;
public function isFoo()
{
return $this->isFoo;
}
public function canFoo()
{
return $this->canFoo;
}
} This results in [
'isFoo' => true,
'foo' => true, // it's using the isFoo isser method!
] Notice, that for the Reading through the linked PR I wonder if my PR also classifies as "new feature" instead of "bugfix"? |
Extended Edge-Caseclass ObjectWithBooleanPropertyAndIsserWithSameName
{
public $foo = 'foo';
private $getFoo = 'getFoo';
private $hasFoo = 'hasFoo';
private $canFoo = 'canFoo';
private $isFoo = 'isFoo';
public function getFoo()
{
return $this->getFoo;
}
public function hasFoo()
{
return $this->hasFoo;
}
public function canFoo()
{
return $this->canFoo;
}
public function isFoo()
{
return $this->isFoo;
}
} Results in [
'foo' => 'getFoo',
'isFoo' => 'isFoo'
] So we may also want to add the That would result in: [
'getFoo' => 'getFoo',
'hasFoo' => 'hasFoo',
'canFoo' => 'canFoo',
'isFoo' => 'isFoo',
'foo' => 'getFoo'
] |
What do you think, how should we proceed here? Should we do the
IMO it would definitely make sense for |
isser
, hasser
, canner
) where the property has the same name
isser
, hasser
, canner
) where the property has the same name
|
Hey @nicolas-grekas, thanks for chiming in. I just want to give a little summary, why I think the current state can lead to unnoticed bugs which may be hard to debug. I had this little sidenote in the linked issue #61096:
So imagine the following steps:
The problem is, that de-/serialization doesn't behave deterministically in this case. The issue has a more detailed description why. Although my implementation might not be the way to go, I really would like to have a solution for this case. I get your point, that this looks into private details, but the current state (deciding on getters, canners, hassers and issers) also already does this. So if it's done in one way (serialization), it should also work in the opposite direction (deserialization). I don't really think, that this is extremely specific and it definitely would be my expectation, that it should work in both directions. What do you think? |
d6f13d7
to
ed8290e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the current state (deciding on getters, canners, hassers and issers) also already does this
I forgot this aspect indeed. This voids my argument.
👍 (I updated the implementation to make it a bit more generic, doing the same for all accessors)
…ame as a property
ed8290e
to
7fe90ba
Compare
Thank you very much, this is exactly what I would expect. I first noticed it for issers, that's why I implemented it only there. Afterwards I saw it would make sense for all accessors, but wanted to get feedback by Symfony Core members. Thanks for understanding and adjusting the PR! Edit: Also I love your adjustment of the match-statement. Very readable and clever to assign |
Thank you @RafaelKr. |
@nicolas-grekas Shouldn't there be also tests for all accessor methods? |
There could yes. Since everything is in the same codepath now, I didn't bother, but feel free to submit another PR of course! |
@nicolas-grekas please see #61660 While doing the changes I notices, that
It was added to the 7.4 branch: Should this maybe be backported? Also another place, where
Should I create seperate issues for those? |
Support for canners has been added as a new feature, it cannot be backported (but it can be added to 7.4 for places where it's missing - if that makes sense) |
@nicolas-grekas, oh. This branch was merged into 6.4 And it also already supported
Or do you only talk about the Validator AttributeLoader? |
I mean Validator AttributeLoader yes |
Okay, but the fb1da0b should still be backported, if Serializer already also supported |
Well, that was discussed a bit in #61023 |
…r method changes from #61097 (RafaelKr) This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [Serializer] Adjust ObjectNormalizerTest for the accessor method changes from #61097 | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | no <!-- if yes, also update src/**/CHANGELOG.md --> | Deprecations? | no <!-- if yes, also update UPGRADE-*.md and src/**/CHANGELOG.md --> | Issues | Fix #61096 | License | MIT This is a follow-up PR to #61097 CC `@nicolas`-grekas Commits ------- 2e1a76e [Serializer] Adjust ObjectNormalizerTest for the accessor method changes from #61097
I'm not sure if a new PR tackling only this one place is a good idea. For future consistency it may be a better idea to define all default accessor (and mutator?) methods in a place shared accross all Symfony components, if that's possible. @stof I'm pinging you in reference to your comment at #61023 (comment). What do you think about this situation? TLDR: We're talking about the discrepancy of the As one part of the Serializer (the Normalizer) already supports it, but another part (the AttributeLoader) doesn't, it may classify as a bugfix. |
* 6.4: fix test setup [Validator] Review Turkish translations [Validator] Review Croatian translations [Validator] Review translations for Polish (pl) use the empty string instead of null as an array offset Review translations for Chinese (zh_TW) [Serializer] Adjust ObjectNormalizerTest for the accessor method changes from #61097
* 7.3: fix test setup [Validator] Review Turkish translations [Validator] Review Croatian translations [Validator] Review translations for Polish (pl) use the empty string instead of null as an array offset Review translations for Chinese (zh_TW) [Serializer] Adjust ObjectNormalizerTest for the accessor method changes from #61097
* 7.4: [SecurityBundle] Fix tests on Windows use the empty string instead of null as an array offset pass the empty string instead of null as key to array_key_exists() fix test setup [Validator] Review Turkish translations [Validator] Review Croatian translations [Console] Add #[Input] attribute to support DTOs in commands [Security][SecurityBundle] Dump role hierarchy as mermaid chart [DependencyInjection] Allow `Class::function(...)` and `global_function(...)` closures in PHP DSL for factories [VarExporter] Add support for exporting named closures [Validator] Review translations for Polish (pl) use the empty string instead of null as an array offset Review translations for Chinese (zh_TW) [Serializer] Adjust ObjectNormalizerTest for the accessor method changes from #61097 fix merge [Security] Fix `HttpUtils::createRequest()` when the base request is forwarded map legacy options to the "sentinel" key when parsing DSNs fix setup to actually run Redis Sentinel/Cluster integration tests [Routing] Don't rebuild cache when controller action body changes
Notes
Although technically this is a bugfix and its pretty much an edge-case, this may affect existing projects and may alter their serialization results. So it could be a breaking change.
Edit: Also see comments below for further edge cases.
Before/After comparison
Before this PR
It was normalized as:
Removing the
$owner
property and getters would result in:After this PR
It will be normalized as: