Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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

Merged
fabpot merged 1 commit intosymfony:masterfromdunglas:serializer_attributes
Feb 13, 2017

Conversation

@dunglas
Copy link
Member

@dunglasdunglas commentedMay 22, 2016
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed ticketsn/a
LicenseMIT
Doc PRtodo

This new features give the possibility to the end user to select select fields to normalize.

Exemple:

class Foo{public$a ='1';public$b ='2';public$c ='3';}$normalizer =new \Symfony\Component\Serializer\Normalizer\ObjectNormalizer();$normalizer->normalize(newFoo(),null, ['attributes' => ['a','c']]);// ['a' => '1', 'c' => '3']

Denormalization is also supported. This feature works for any normalizer using theSymfony\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}

Nested parameters are also supported:

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']

Thanks to this PR, support for this feature will be available out of the box inAPI Platform.

GuilhemN, chalasr, e0ipso, Simperfit, aledeg, norkunas, sstok, yceruto, jvasseur, hugohenrique, and 4 more reacted with thumbs up emojiyceruto reacted with hooray emojiyceruto reacted with heart emoji
@GuilhemN
Copy link
Contributor

GuilhemN commentedMay 22, 2016
edited
Loading

Your example in the description is wrong, the context should be a nested array['attributes' => ['a', 'c']].
Otherwise, this looks great!

@Simperfit
Copy link
Contributor

👍

@dunglas
Copy link
MemberAuthor

Good catch @Ener-Getick, thanks!

@theofidry
Copy link
Contributor

theofidry commentedMay 22, 2016
edited
Loading

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]
damienalexandre, norkunas, chalasr, and yceruto reacted with thumbs up emoji

@GuilhemN
Copy link
Contributor

GuilhemN commentedJun 7, 2016
edited
Loading

@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
Copy link
Member

Sounds like a good new feature.@dunglas Can you finish it?

@fabpot
Copy link
Member

Any news?

@dunglas
Copy link
MemberAuthor

Yes I've an idea of how to manage related entities. I'll finish this one ASAP.

@teohhanhui
Copy link
Contributor

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.

theofidry and yceruto reacted with thumbs up emoji

@xabbuh
Copy link
Member

Making use of the PropertyAccess component for this feature sounds like a good idea to me.

@dunglas
Copy link
MemberAuthor

dunglas commentedFeb 3, 2017
edited
Loading

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

@dunglasdunglas modified the milestones:3.3,3.xFeb 4, 2017
@nicolas-grekasnicolas-grekas changed the title[Serializer] Add the possibilitiy to filter attributes[Serializer] Add the possibility to filter attributesFeb 13, 2017
Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

👍

@fabpot
Copy link
Member

@dunglas Can you submit a PR on the docs?

@fabpot
Copy link
Member

Thank you@dunglas.

@fabpotfabpot merged commitb3826fb intosymfony:masterFeb 13, 2017
fabpot added a commit that referenced this pull requestFeb 13, 2017
…(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
@dunglasdunglas deleted the serializer_attributes branchFebruary 13, 2017 14:34
@fabpotfabpot mentioned this pull requestMay 1, 2017
fabpot added a commit that referenced this pull requestDec 7, 2017
…(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
weaverryan added a commit to symfony/symfony-docs that referenced this pull requestDec 13, 2017
…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
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

3.3

Development

Successfully merging this pull request may close these issues.

10 participants

@dunglas@GuilhemN@Simperfit@theofidry@fabpot@teohhanhui@xabbuh@nicolas-grekas@javiereguiluz@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp