Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[Serializer] Add the possibility to filter attributes#18834
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
GuilhemN commentedMay 22, 2016 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Your example in the description is wrong, the context should be a nested array |
Simperfit commentedMay 22, 2016
👍 |
dunglas commentedMay 22, 2016
Good catch @Ener-Getick, thanks! |
theofidry commentedMay 22, 2016 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Is there a support for nested attributes as well? Example: class Foo {public$bar;publicfunction__construct(Bar$bar) {$this->bar =$bar; }}class Bar {public$a =1;public$b =2;}$normalizer =new \Symfony\Component\Serializer\Normalizer\ObjectNormalizer();$normalizer->normalize(newFoo(newBar()),null, ['attributes' => ['bar.a']]);// => ['bar.a' => 1] |
GuilhemN commentedJun 7, 2016 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@theofidry no this isn't possible. @dunglas maybe an associative array could be used to allow nested attributes safely ? ['attributes' => ['a' => ['b' => [],'c' => ['d' ] ],'e' => [] ]] BTW there are no bc breaks/deprecations in this PR, so this can be removed from the pr header. |
fabpot commentedJun 16, 2016
Sounds like a good new feature.@dunglas Can you finish it? |
fabpot commentedSep 14, 2016
Any news? |
dunglas commentedSep 14, 2016
Yes I've an idea of how to manage related entities. I'll finish this one ASAP. |
teohhanhui commentedDec 20, 2016
I think it's crucial to have nested attributes support, and in a nice syntax. Perhaps using PropertyAccess path... The semantics could be something like this: If one attribute is specified for a certain object / item, then all attributes on the same object / item need to be specified (otherwise they will be omitted). Similar to how Prophecy mocks work. |
xabbuh commentedDec 28, 2016
Making use of the PropertyAccess component for this feature sounds like a good idea to me. |
f7b9ca8 to527bc77Comparedunglas commentedFeb 3, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I eventually added support for nested attributes (it works for normalization and denormalization). Usage: class Foo{public$a ='1';public$b ='2';public$c ='3';public$bar =newBar();}class Bar{public$x ='x';public$y ='y';public$z ='z';}$serializer =new \Symfony\Component\Serializer( [new \Symfony\Component\Serializer\Normalizer\ObjectNormalizer()]);$serializer->normalize(newFoo(),null, ['attributes' => ['a','c','bar' => ['y']]]);// ['a' => '1', 'c' => '3', 'bar' => ['y' => 'y'] This PR is ready now. ping @symfony/deciders |
cebc036 tob3826fbCompare
nicolas-grekas left a comment
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.
👍
fabpot commentedFeb 13, 2017
@dunglas Can you submit a PR on the docs? |
fabpot commentedFeb 13, 2017
Thank you@dunglas. |
…(dunglas)This PR was merged into the 3.3-dev branch.Discussion----------[Serializer] Add the possibility to filter attributes| Q | A || --- | --- || Branch? | master || Bug fix? | no || New feature? | yes || BC breaks? | no || Deprecations? | no || Tests pass? | yes || Fixed tickets | n/a || License | MIT || Doc PR | todo |This new features give the possibility to the end user to select select fields to normalize.Exemple:``` phpclass Foo{ public $a = '1'; public $b = '2'; public $c = '3';}$normalizer = new \Symfony\Component\Serializer\Normalizer\ObjectNormalizer();$normalizer->normalize(new Foo(), null, ['attributes' => ['a', 'c']]);// ['a' => '1', 'c' => '3']```Denormalization is also supported. This feature works for any normalizer using the `Symfony\Component\Serializer\Normalizer\AbstractNormalizer` class.The main use case is to permit to API clients to optimize responses length by dropping unnecessary information like in the following example:```http://exemple.com/cars?fields=a,c{'a' => 1, 'c' => 2}```Thanks to this PR, support for this feature will be available out of the box in [API Platform](https://api-platform.com).Commits-------b3826fb [Serializer] Add the possibility to filter attributes…(dunglas)This PR was merged into the 3.3 branch.Discussion----------[Serializer] Unset attributes when creating child context | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/aIn some cases, the `attributes` key isn't overrode when creating the context passed to nested normalizers. It's definitely a bug, but an attacker cannot access to non public data (ignored attributes are checked before the `attributes` key). However some data that must be public may be missing as highlighted by the test.I've introduced the initial bug here:#18834Commits-------4ff9d99 [Serializer] Unset attributes when creating child context
…as, javiereguiluz)This PR was merged into the 3.3 branch.Discussion----------[Serializer] Add docs for attributes context keyDocumentation forsymfony/symfony#18834Commits-------d48d6d4 Minor rewordea211ab Review5d75696 [Serializer] Add docs for attributes context key
Uh oh!
There was an error while loading.Please reload this page.
This new features give the possibility to the end user to select select fields to normalize.
Exemple:
Denormalization is also supported. This feature works for any normalizer using the
Symfony\Component\Serializer\Normalizer\AbstractNormalizerclass.The main use case is to permit to API clients to optimize responses length by dropping unnecessary information like in the following example:
Nested parameters are also supported:
Thanks to this PR, support for this feature will be available out of the box inAPI Platform.