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] Throw exception when extra attributes are used during an object denor…#19958

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

Conversation

@juliendidier
Copy link
Contributor

@juliendidierjuliendidier commentedSep 17, 2016
edited
Loading

QA
Branch?"master"
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#19948
LicenseMIT
Doc PR#6975

I will update the doc if you're ok with this PR.


private$propertyTypeExtractor;
private$attributesCache =array();
private$extraAttributes =array();
Copy link
Member

@dunglasdunglasSep 17, 2016
edited
Loading

Choose a reason for hiding this comment

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

A service should not have global states like this one. Here it is worst because it creates a bug: if thedenormalize method fails one time because of an extra attribute, all successive calls will throw an error (even if there is no extra attribute) because$this->extraAttributes isn't reseted to an empty array.

Here I think that it's enough to use a local variable to store the list of extra attributes. If you want to make information available in nesteddenormalize calls, you should use the$context parameter.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Humm yes... sorry for that.

}

if (($allowedAttributes !==false && !in_array($attribute,$allowedAttributes)) || !$this->isAllowedAttribute($class,$attribute,$format,$context)) {
if (isset($context['extra_attributes']) && !$context['extra_attributes']) {
Copy link
Member

Choose a reason for hiding this comment

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

The form component has a similar option calledallow_extra_fields. I propose to rename the key toallow_extra_attributes for consistency.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I had a lot of hesitation. Updated.

}

if (!empty($this->extraAttributes)) {
thrownewExtraAttributesException(sprintf('Extra attributes are not allowed ("%s" given).',implode('", "',$this->extraAttributes)));
Copy link
Member

Choose a reason for hiding this comment

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

Extra attributes are not allowed ("%s" are unknown).?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

It's more simple with no plurial, but that's good for me. Updated.

@juliendidierjuliendidierforce-pushed theserializer-extra-attributes branch 2 times, most recently from4cd4e13 to8444b21CompareSeptember 17, 2016 19:20
@dunglas
Copy link
Member

👍, travis error not related.

Status: reviewed

@juliendidier
Copy link
ContributorAuthor

Doc PR added.
See#6975

useSymfony\Component\Serializer\Exception\LogicException;
useSymfony\Component\Serializer\Exception\UnexpectedValueException;
useSymfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
useSymfony\Component\PropertyInfo\Type;
Copy link
Member

Choose a reason for hiding this comment

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

use statements reorganization should reverted.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Alright. done.

@juliendidierjuliendidierforce-pushed theserializer-extra-attributes branch 2 times, most recently from8430c61 todca221bCompareSeptember 18, 2016 20:29
}

if (!empty($extraAttributes)) {
thrownewExtraAttributesException(sprintf('Extra attributes are not allowed ("%s" are unknown).',implode('", "',$extraAttributes)));
Copy link
Contributor

Choose a reason for hiding this comment

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

Given you have specific exception type, it makes sense to put such logic into the exception, i.e.throw new ExtraAttributesException($extraAttributes) (seeSymfony\Component\DependencyInjection\Exception\ServiceNotFoundException for example).

dunglas and chalasr reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I don't really like doing that. And it's not the current behavior for serializer exceptions (like theCircularReferenceException).

However, I pushed this change.

Copy link
Contributor

@unkindunkindSep 19, 2016
edited
Loading

Choose a reason for hiding this comment

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

It makes exception API stricter. It is harder to misuse it, it decreases possible inconsistency between messages in different places if an exception has many usages in the code. It makes easier to unit test the exception if the message-forming logic becomes tricky.

@juliendidierjuliendidierforce-pushed theserializer-extra-attributes branch 3 times, most recently fromd485036 to313fca9CompareSeptember 20, 2016 22:59
@juliendidierjuliendidierforce-pushed theserializer-extra-attributes branch from313fca9 to565a984CompareSeptember 21, 2016 23:37
@juliendidier
Copy link
ContributorAuthor

Rebased from master to fix builds.

@dunglas
Copy link
Member

I'll merge this PR in 3.3. Thanks@juliendidier

@dunglas
Copy link
Member

Thanks@juliendidier for working on this feature, this is much appreciated.

@dunglasdunglas merged commit565a984 intosymfony:masterDec 5, 2016
dunglas added a commit that referenced this pull requestDec 5, 2016
… used during an object denor… (juliendidier)This PR was merged into the 3.3-dev branch.Discussion----------[Serializer] Throw exception when extra attributes are used during an object denor…| Q | A || --- | --- || Branch? | "master" || Bug fix? | no || New feature? | yes || BC breaks? | no || Deprecations? | no || Tests pass? | yes || Fixed tickets |#19948 || License | MIT || Doc PR | [#6975](symfony/symfony-docs#6975) |I will update the doc if you're ok with this PR.Commits-------565a984 throw exception when extra attributes are used during an object denormalization
@juliendidier
Copy link
ContributorAuthor

@dunglas thx, don't forget the documentation ;)symfony/symfony-docs/pull/6975

@dunglas
Copy link
Member

I cannot merge the doc but I'm sure that the @symfony/team-symfony-docs will handle it soon!

xabbuh added a commit to symfony/symfony-docs that referenced this pull requestMar 3, 2017
…juliendidier)This PR was merged into the master branch.Discussion----------[Serializer] documentation for allow_extra_attributesDoc PR for [symfony/symfony#19958](symfony/symfony#19958)Commits-------28dcc94 [Serializer] documentation for allow_extra_attributes
@fabpotfabpot mentioned this pull requestMay 1, 2017
@juliendidierjuliendidier deleted the serializer-extra-attributes branchDecember 4, 2018 19:22
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@fabpotfabpotfabpot left review comments

@dunglasdunglasdunglas approved these changes

+1 more reviewer

@unkindunkindunkind left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

5 participants

@juliendidier@dunglas@fabpot@unkind@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp