-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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
Has this been introduced for 4.2? If not, this should throw a deprecation instead I suppose? |
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)
@nicolas-grekas Ok I'll switch the exception to a deprecation warning - can you advise which since/to versions this warning should read? eg. |
LGTM, let's propose this yes. |
@nicolas-grekas Switched to deprecation with docs. |
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
@@ -64,3 +64,5 @@ Serializer | |||
|
|||
* 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 (!($normalizer instanceof NormalizerInterface || $normalizer instanceof 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 :)
@nicolas-grekas Done, thanks. |
@@ -78,6 +78,11 @@ public function __construct(array $normalizers = array(), array $encoders = arra | |||
if ($normalizer instanceof NormalizerAwareInterface) { | |||
$normalizer->setNormalizer($this); | |||
} | |||
|
|||
if (!($normalizer instanceof NormalizerInterface || $normalizer instanceof 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 :(
Injecting something else than a |
Altenative proposal: /**
* @param NormalizerInterface[]|DenormalizerInterface[] $normalizers
* @param EncoderInterface[]|DecoderInterface[] $encoders
*/
public function __construct(array $normalizers = array(), array $encoders = array()) No deprecation, no overhead. |
Should be done for encoders too btw. |
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? |
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). |
Still let's add them also :) |
👍 |
@@ -64,6 +64,10 @@ class Serializer implements SerializerInterface, ContextAwareNormalizerInterface | |||
private $denormalizerCache = array(); | |||
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 in 04b2c2d
* @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.
Can you rebase please? |
…he base interfaces
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)
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 | None Currently 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 <!-- see https://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 #27819 Commits ------- 5ab6ad4 [Serializer] Throw exception on invalid normalizers/encoders passed to Serializer
Currently the
Serializer
can be constructed with any object regardless of whether or not it implementsNormalizerInterface
orDenormalizerInterface
. 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?