Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2016

Conversation

juliendidier
Copy link
Contributor

@juliendidier juliendidier commented Sep 17, 2016

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

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

@@ -32,6 +33,7 @@

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

@dunglas dunglas Sep 17, 2016

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 the denormalize 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 nested denormalize calls, you should use the $context parameter.

Copy link
Contributor Author

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.

@@ -183,6 +185,10 @@ public function denormalize($data, $class, $format = null, array $context = arra
}

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 called allow_extra_fields. I propose to rename the key to allow_extra_attributes for consistency.

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 had a lot of hesitation. Updated.

@@ -194,6 +200,10 @@ public function denormalize($data, $class, $format = null, array $context = arra
}
}

if (!empty($this->extraAttributes)) {
throw new ExtraAttributesException(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
Contributor Author

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.

@juliendidier juliendidier force-pushed the serializer-extra-attributes branch 2 times, most recently from 4cd4e13 to 8444b21 Compare September 17, 2016 19:20
@dunglas
Copy link
Member

dunglas commented Sep 18, 2016

👍, travis error not related.

Status: reviewed

@juliendidier
Copy link
Contributor Author

Doc PR added.
See #6975

use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\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
Contributor Author

Choose a reason for hiding this comment

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

Alright. done.

@juliendidier juliendidier force-pushed the serializer-extra-attributes branch 2 times, most recently from 8430c61 to dca221b Compare September 18, 2016 20:29
@@ -194,6 +201,10 @@ public function denormalize($data, $class, $format = null, array $context = arra
}
}

if (!empty($extraAttributes)) {
throw new ExtraAttributesException(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) (see Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException for example).

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 don't really like doing that. And it's not the current behavior for serializer exceptions (like the CircularReferenceException).

However, I pushed this change.

Copy link
Contributor

@unkind unkind Sep 19, 2016

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.

@juliendidier juliendidier force-pushed the serializer-extra-attributes branch 3 times, most recently from d485036 to 313fca9 Compare September 20, 2016 22:59
@juliendidier juliendidier force-pushed the serializer-extra-attributes branch from 313fca9 to 565a984 Compare September 21, 2016 23:37
@juliendidier
Copy link
Contributor Author

Rebased from master to fix builds.

@dunglas
Copy link
Member

dunglas commented Nov 11, 2016

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

@dunglas
Copy link
Member

dunglas commented Dec 5, 2016

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

@dunglas dunglas merged commit 565a984 into symfony:master Dec 5, 2016
dunglas added a commit that referenced this pull request Dec 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
Contributor Author

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

@dunglas
Copy link
Member

dunglas commented Dec 6, 2016

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 request Mar 3, 2017
…juliendidier)

This PR was merged into the master branch.

Discussion
----------

[Serializer] documentation for allow_extra_attributes

Doc PR for [symfony/symfony#19958](symfony/symfony#19958)

Commits
-------

28dcc94 [Serializer] documentation for allow_extra_attributes
@fabpot fabpot mentioned this pull request May 1, 2017
@juliendidier juliendidier deleted the serializer-extra-attributes branch December 4, 2018 19:22
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.

5 participants