Skip to content
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

[Serializer] Respect ignored attributes in cache key of normalizer #30907

Merged
merged 1 commit into from Apr 8, 2019

Conversation

dbu
Copy link
Contributor

@dbu dbu commented Apr 6, 2019

EUFOSSA

Q A
Branch 3.4
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

Do not share the attributes cache in object normalizer when using a different setting for the ignoredAttributes setting.

In Symfony 4.2, the setter is deprecated in favor of the ignored_attibutes option in the $context. When merging this up, we will however still need to respect the field as well for BC, the cache key does not look at the default context (apart from the deprecated modifiers, the default context is immutable)

There might be performance regression for some use cases, but also could be a performance improvement when using 'attributes' in the context with lists of objects of the same class.

@@ -128,15 +128,13 @@ protected function getAttributes($object, $format = null, array $context)
return $allowedAttributes;
}

if (isset($context['attributes'])) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we previously skipped the cache if attributes are defined in the context. now we use the attributes as part of the cache key.

@dbu dbu force-pushed the fix-object-normalizer-attributes-cache branch 2 times, most recently from 6737389 to 7b64d88 Compare April 6, 2019 14:45
@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Apr 6, 2019
'context' => $context,
'ignored' => $this->ignoredAttributes,
'camelized' => $this->camelizedAttributes,
]));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

i hope i caught all normalizer state that could influence which attributes are available and not too many.

things like circular reference or callbacks should not change which attributes are available i guess?

(serialize one array rather than concat several serialize result, assuming that gives better performance)

Copy link
Member

@dunglas dunglas left a comment

Choose a reason for hiding this comment

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

Such changes require careful manual testing and profiling. But LGTM.

@dbu
Copy link
Contributor Author

dbu commented Apr 7, 2019

FTR: build error is due to a travis install error during hhvm build. i am confident this is not related to the PR ;-) the other builds are green.

@dbu dbu force-pushed the fix-object-normalizer-attributes-cache branch from ea4f387 to be2e3fa Compare April 8, 2019 08:05
*
* @return array
*
* @internal
*/
protected function createChildContext(array $parentContext, $attribute)
protected function createChildContext(array $parentContext, $attribute/*, ?string $format*/)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

should i also trigger a deprecation warning here? we don't actually use the format at all...

Copy link
Member

Choose a reason for hiding this comment

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

not on 3.4 but on master yes - also, this should be /*, string $format = null */, isn't it?

Copy link
Member

Choose a reason for hiding this comment

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

@dbu Can you fix the typos? (no ? and a space before */?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

as its a protected function, i actually think ?string would make more sense, you will always pass the parameter even when its null. nullable types is php 7.2 only, which i guess symfony 5 will require at least. i changed it to the optional argument syntax for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

also, if its indeed an optional argument, the deprecation warning would be unnecessary.

Copy link
Member

Choose a reason for hiding this comment

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

ok, fair enough

Copy link
Contributor Author

Choose a reason for hiding this comment

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

shall i change it to nullable argument? the nullable syntax was introduced in 7.1 actually. or shall i only change it on master when i also add a deprecation warning?

@@ -382,8 +406,13 @@ private function isMaxDepthReached(array $attributesMetadata, $class, $attribute
*/
private function getCacheKey($format, array $context)
{
unset($context['cache_key']); // avoid artifically different keys
Copy link
Contributor Author

Choose a reason for hiding this comment

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

if we would keep this field, every call to this method generates a new key as the previous key is part of what we hash. the correct thing is to not include the previous key in the hash as its not meaningful information.

@dbu dbu force-pushed the fix-object-normalizer-attributes-cache branch 3 times, most recently from 20aae5f to 100fe8d Compare April 8, 2019 09:30
@fabpot fabpot force-pushed the fix-object-normalizer-attributes-cache branch from 100fe8d to 926d228 Compare April 8, 2019 10:10
@fabpot
Copy link
Member

fabpot commented Apr 8, 2019

Thank you @dbu.

@fabpot fabpot merged commit 926d228 into symfony:3.4 Apr 8, 2019
fabpot added a commit that referenced this pull request Apr 8, 2019
…rmalizer (dbu)

This PR was squashed before being merged into the 3.4 branch (closes #30907).

Discussion
----------

[Serializer] Respect ignored attributes in cache key of normalizer

EUFOSSA

| Q             | A
| ------------- | ---
| Branch       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Do not share the attributes cache in object normalizer when using a different setting for the ignoredAttributes setting.

In Symfony 4.2, the setter is deprecated in favor of the ignored_attibutes option in the $context. When merging this up, we will however still need to respect the field as well for BC, the cache key does not look at the default context (apart from the deprecated modifiers, the default context is immutable)

There might be performance regression for some use cases, but also could be a performance improvement when using 'attributes' in the context with lists of objects of the same class.

Commits
-------

926d228 [Serializer] Respect ignored attributes in cache key of normalizer
@fabpot
Copy link
Member

fabpot commented Apr 8, 2019

@dbu I think we need your help with merging this one on 4.2 and master.

@dbu
Copy link
Contributor Author

dbu commented Apr 8, 2019

happy to help. do i create a branch from 4.2, merge 3.4 to it, then solve conflicts and do a PR with that?

@dbu dbu deleted the fix-object-normalizer-attributes-cache branch April 8, 2019 10:48
@fabpot
Copy link
Member

fabpot commented Apr 8, 2019

@dbu That would be super hepful, yes.

@xabbuh
Copy link
Member

xabbuh commented Apr 8, 2019

Sorry, I didn't see your conversation before merging 3.4 up.

@dbu Can you review my conflict resolution in ec41d76 and also take a look into #31005 which should solve the remaining deprecation (and thus the failing test)?

@dbu
Copy link
Contributor Author

dbu commented Apr 8, 2019

ah, now i was confused until i red this :-D

resolution looks good to me.

@xabbuh
Copy link
Member

xabbuh commented Apr 8, 2019

@dbu Thank you! 👍

@dbu
Copy link
Contributor Author

dbu commented Apr 8, 2019

should i do the deprecation change against 4.2 or master? ec41d76#r33074748 => will do against master when changes have been merged there.

@fabpot
Copy link
Member

fabpot commented Apr 8, 2019

@dbu 4.2 has been merged into master now.

This was referenced Apr 16, 2019
fabpot added a commit that referenced this pull request Apr 27, 2019
This PR was squashed before being merged into the 4.3-dev branch (closes #30888).

Discussion
----------

[serializer] extract normalizer tests to traits

eufossa

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | Relates to #30818
| License       | MIT
| Doc PR        | -

As discussed with @joelwurtz, extract normalizer functionality tests into traits to ensure consistent behaviour of all normalizers.

* [x] Rebase when #30977, #30950 and #30907 are merged to master **blocker**
* [x] Clean up order of trait inclusion and methods in the tests
* [x] Clean up fixture classes of the traits. I started having one class named the same as the trait, where possible

Stuff that we should do eventually, but can also do in separate pull requests, after this one has been merged:
* [ ] Extract all features that we can (the existing normalizer tests should more or less only have the legacy tests in them, all functionality should be in trait)
* [ ] Run test coverage and increase coverage so that we cover all important features and all relevant error cases.

Commits
-------

2b6ebea [serializer] extract normalizer tests to traits
fabpot added a commit that referenced this pull request May 15, 2019
…out the format parameter (dbu)

This PR was submitted for the master branch but it was merged into the 4.3 branch instead (closes #31030).

Discussion
----------

[Serializer] Deprecate calling createChildContext without the format parameter

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

as discussed in #30907 deprecate omitting the format parameter when extending the AbstractNormalizer.

Commits
-------

cb77902 deprecate calling createChildContext without the format parameter
nicolas-grekas added a commit that referenced this pull request Jan 26, 2021
…lasr)

This PR was merged into the 4.4 branch.

Discussion
----------

[Serializer] Fix tests wrongly marked as incomplete

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

#30907 has been merged meanwhile.

Commits
-------

e632302 [Serializer] Fix tests  marked as incomplete
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.

None yet

9 participants