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] deprecated normalizers and encoders who dont implement the base interfaces#27819
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
dunglas 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.
Why not
nicolas-grekas commentedJul 3, 2018 • 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.
Has this been introduced for 4.2? If not, this should throw a deprecation instead I suppose? |
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.
(see comment)
rodnaph commentedJul 3, 2018
@nicolas-grekas Ok I'll switch the exception to a deprecation warning - can you advise which since/to versions this warning should read? eg. |
nicolas-grekas commentedJul 3, 2018
LGTM, let's propose this yes. |
rodnaph commentedJul 4, 2018
@nicolas-grekas Switched to deprecation with docs. |
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.
just some minor comment
UPGRADE-4.2.md Outdated
| * Relying on the default value (false) of the "as_collection" option is deprecated since 4.2. | ||
| You should set it to false explicitly instead as true will be the default value in 5.0. | ||
| * Deprecated creating a`Serializer` with normalizers which do not implement | ||
| either`NormalizerInterface` or`DenormalizerInterface`. |
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.
missing indentation
UPGRADE-5.0.md Outdated
| ---------- | ||
| * Creating a `Serializer` with normalizers that do not implement either | ||
| `NormalizerInterface`or `DenormalizerInterface` will now throw an exception. |
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.
missing indentation
| ----- | ||
| * deprecated creating a`Serializer` with normalizers which do not implement | ||
| either`NormalizerInterface` or`DenormalizerInterface`. |
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.
indentation
| if (!($normalizerinstanceof NormalizerInterface ||$normalizerinstanceof DenormalizerInterface)) { | ||
| @trigger_error(\sprintf('Passing normalizers (%s) which do not implement either %s or %s has been deprecated since 4.2 and will throw an exception in 5.0',get_class($normalizer), NormalizerInterface::class, DenormalizerInterface::class),E_USER_DEPRECATED); | ||
| // throw new \InvalidArgumentException(\sprintf('The class %s does not implement %s or %s', get_class($normalizer), NormalizerInterface::class, DenormalizerInterface::class)); |
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.
please add double quotes around all the "%s"
the " and will throw an exception in 5." part should be removed
and a final dot added
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.
@nicolas-grekas removing the 5.0 part is not done in other places, btw?https://github.com/symfony/symfony/pull/26564/files#diff-9d63a61ac1b3720a090df6b1015822f2R729
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.
it should have been :)
rodnaph commentedJul 4, 2018
@nicolas-grekas Done, thanks. |
| } | ||
| if (!($normalizerinstanceof NormalizerInterface ||$normalizerinstanceof DenormalizerInterface)) { | ||
| @trigger_error(\sprintf('Passing normalizers ("%s") which do not implement either "%s" or "%s" has been deprecated.',get_class($normalizer), NormalizerInterface::class, DenormalizerInterface::class),E_USER_DEPRECATED); |
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.
has been deprecated since Symfony 4.2.
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.
Doh :(
ogizanagi commentedJul 4, 2018
Injecting something else than a |
dunglas commentedJul 4, 2018 • 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.
Altenative proposal: /** * @param NormalizerInterface[]|DenormalizerInterface[] $normalizers * @param EncoderInterface[]|DecoderInterface[] $encoders */publicfunction __construct(array$normalizers =array(),array$encoders =array()) No deprecation, no overhead. |
dunglas commentedJul 4, 2018
Should be done for encoders too btw. |
nicolas-grekas commentedJul 5, 2018
Good idea to add the docblock@dunglas, let's do that. The instanceof checks are mostly free not sure that's overhead. Don't you want to have an exception in 5.0? |
rodnaph commentedJul 5, 2018
Regards docblock... My reason for adding this check was having lost time debugging a mistake with a normalizer service I was injecting, a runtime exception would have be the quickest notification (docs or static analysis probably wouldn't have highlighted it for me in that instance, but of course worth having). |
nicolas-grekas commentedJul 5, 2018 • 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.
Still let's add them also :) |
rodnaph commentedJul 5, 2018
👍 |
| private$normalizerCache =array(); | ||
| /** | ||
| * @param NormalizerInterface[]|DenormalizerInterface[] $normalizers |
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.
The syntax we previously used for such arrays is(NormalizerInterface|DenormalizerInterface)[], like in
symfony/src/Symfony/Component/Security/Core/Authentication/Token/UsernamePasswordToken.php
Line 30 in04b2c2d
| * @param (Role|string)[] $roles An array of roles |
But as it's still not supported by IDEs AFAIK 👍 to keep as is.
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.
The current syntax is a bit miss leading as it suggests either array of NormalizerInterface or an array of DenormalizerInterface but not mixed.
dunglas commentedSep 5, 2018
Can you rebase please? |
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.
(rebased)
fabpot commentedSep 25, 2018
Thank you@rodnaph. |
…ont implement the base interfaces (rodnaph)This PR was merged into the 4.2-dev branch.Discussion----------[Serializer] deprecated normalizers and encoders who dont implement the base interfaces| Q | A| ------------- | ---| Branch? | master| Bug fix? | no| New feature? | yes| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets | None| License | MIT| Doc PR | NoneCurrently the `Serializer` can be constructed with any object regardless of whether or not it implements `NormalizerInterface` or `DenormalizerInterface`. This object will then be ignored when getting a normalizer/denormalizer, so in effect silently ignored for serializer operations.This change throws an exception on construct if a given normalizer object does not implement one of these interfaces - are there use cases where this would not be true?Commits-------cbc2be8 [Serializer] deprecated normalizers and encoders who dont implement the base interfaces
…ders passed to Serializer (ogizanagi)This PR was merged into the 5.0-dev branch.Discussion----------[Serializer] Throw exception on invalid normalizers/encoders passed to Serializer| Q | A| ------------- | ---| Branch? | master| Bug fix? | no| New feature? | no <!-- please update src/**/CHANGELOG.md files -->| BC breaks? | no <!-- seehttps://symfony.com/bc -->| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->| Tests pass? | yes <!-- please add some, will be required by reviewers -->| Fixed tickets |#27819 <!-- #-prefixed issue number(s), if any -->| License | MIT| Doc PR | N/A <!-- required for new features -->As planned in#27819Commits-------5ab6ad4 [Serializer] Throw exception on invalid normalizers/encoders passed to Serializer
Currently the
Serializercan be constructed with any object regardless of whether or not it implementsNormalizerInterfaceorDenormalizerInterface. This object will then be ignored when getting a normalizer/denormalizer, so in effect silently ignored for serializer operations.This change throws an exception on construct if a given normalizer object does not implement one of these interfaces - are there use cases where this would not be true?