-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Your example in the description is wrong, the context should be a nested array |
👍 |
Good catch @Ener-Getick, thanks! |
Is there a support for nested attributes as well? Example: class Foo {
public $bar;
public function __construct(Bar $bar) {
$this->bar = $bar;
}
}
class Bar {
public $a = 1;
public $b = 2;
}
$normalizer = new \Symfony\Component\Serializer\Normalizer\ObjectNormalizer();
$normalizer->normalize(new Foo(new Bar()), null, ['attributes' => ['bar.a']]);
// => ['bar.a' => 1] |
@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. |
Sounds like a good new feature. @dunglas Can you finish it? |
Any news? |
Yes I've an idea of how to manage related entities. I'll finish this one ASAP. |
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. |
Making use of the PropertyAccess component for this feature sounds like a good idea to me. |
f7b9ca8
to
527bc77
Compare
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 = new Bar();
}
class Bar
{
public $x = 'x';
public $y = 'y';
public $z = 'z';
}
$serializer = new \Symfony\Component\Serializer(
[new \Symfony\Component\Serializer\Normalizer\ObjectNormalizer()]
);
$serializer->normalize(new Foo(), null, ['attributes' => ['a', 'c', 'bar' => ['y']]]);
// ['a' => '1', 'c' => '3', 'bar' => ['y' => 'y'] This PR is ready now. ping @symfony/deciders |
cebc036
to
b3826fb
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.
👍
@dunglas Can you submit a PR on the docs? |
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: ``` php class 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/a In 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: #18834 Commits ------- 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 key Documentation for symfony/symfony#18834 Commits ------- d48d6d4 Minor reword ea211ab Review 5d75696 [Serializer] Add docs for attributes context key
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\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:
Nested parameters are also supported:
Thanks to this PR, support for this feature will be available out of the box in API Platform.