Skip to content

Commit

Permalink
feature symfony#43982 [Messenger][Serializer] Deprecate "context awar…
Browse files Browse the repository at this point in the history
…e" interfaces (mtarld)

This PR was merged into the 6.1 branch.

Discussion
----------

[Messenger][Serializer] Deprecate "context aware" interfaces

| Q             | A
| ------------- | ---
| Branch?       | 6.1
| Bug fix?      | no
| New feature?  | no
| Deprecations? | yes
| Tickets       |
| License       | MIT
| Doc PR        | TODO

In `supports*` methods, we are really often relying on serialization context.
Previously (~2017), in order to have the context in these methods (and be BC compatible), new interfaces were introduced:
- `ContextAwareNormalizerInterface`
- `ContextAwareDenormalizerInterface`
- `ContextAwareEncoderInterface`
- `ContextAwareDecoderInterface`

But right now, thanks to the `DebugClassLoader`, we're able to have an upgrade path where the regular interfaces:
- `NormalizerInterface`
- `DenormalizerInterface`
- `EncoderInterface`
- `DecoderInterface`
can be updated to rely on the serialization context.

Therefore, the "context aware" interfaces could be deprecated in favor of "regular" ones (and then save 4 interfaces in 7.0).

Commits
-------

ab72f80 Merge context aware interfaces into regular ones
  • Loading branch information
fabpot committed Jan 9, 2022
2 parents 098ff62 + ab72f80 commit 6b9fafb
Show file tree
Hide file tree
Showing 36 changed files with 146 additions and 58 deletions.
10 changes: 10 additions & 0 deletions UPGRADE-6.1.md
@@ -0,0 +1,10 @@
UPGRADE FROM 6.0 to 6.1
=======================

Serializer
----------

* Deprecate `ContextAwareNormalizerInterface`, use `NormalizerInterface` instead
* Deprecate `ContextAwareDenormalizerInterface`, use `DenormalizerInterface` instead
* Deprecate `ContextAwareEncoderInterface`, use `EncoderInterface` instead
* Deprecate `ContextAwareDecoderInterface`, use `DecoderInterface` instead
8 changes: 8 additions & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
@@ -1,6 +1,14 @@
CHANGELOG
=========

6.1
---

* Deprecate `ContextAwareNormalizerInterface`, use `NormalizerInterface` instead
* Deprecate `ContextAwareDenormalizerInterface`, use `DenormalizerInterface` instead
* Deprecate `ContextAwareEncoderInterface`, use `EncoderInterface` instead
* Deprecate `ContextAwareDecoderInterface`, use `DecoderInterface` instead

6.0
---

Expand Down
Expand Up @@ -15,6 +15,8 @@
* Adds the support of an extra $context parameter for the supportsDecoding method.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*
* @deprecated since symfony/serializer 6.1, use DecoderInterface instead
*/
interface ContextAwareDecoderInterface extends DecoderInterface
{
Expand Down
Expand Up @@ -15,6 +15,8 @@
* Adds the support of an extra $context parameter for the supportsEncoding method.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*
* @deprecated since symfony/serializer 6.1, use EncoderInterface instead
*/
interface ContextAwareEncoderInterface extends EncoderInterface
{
Expand Down
8 changes: 6 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/CsvEncoder.php
Expand Up @@ -123,8 +123,10 @@ public function encode(mixed $data, string $format, array $context = []): string

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsEncoding(string $format): bool
public function supportsEncoding(string $format /*, array $context = [] */): bool
{
return self::FORMAT === $format;
}
Expand Down Expand Up @@ -209,8 +211,10 @@ public function decode(string $data, string $format, array $context = []): mixed

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsDecoding(string $format): bool
public function supportsDecoding(string $format /*, array $context = [] */): bool
{
return self::FORMAT === $format;
}
Expand Down
Expand Up @@ -40,8 +40,9 @@ public function decode(string $data, string $format, array $context = []);
* Checks whether the deserializer can decode from given format.
*
* @param string $format Format name
* @param array $context Options that decoders have access to
*
* @return bool
*/
public function supportsDecoding(string $format);
public function supportsDecoding(string $format /*, array $context = [] */);
}
5 changes: 3 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/EncoderInterface.php
Expand Up @@ -32,7 +32,8 @@ public function encode(mixed $data, string $format, array $context = []): string
/**
* Checks whether the serializer can encode to given format.
*
* @param string $format Format name
* @param string $format Format name
* @param array $context Options that normalizers/encoders have access to
*/
public function supportsEncoding(string $format): bool;
public function supportsEncoding(string $format /*, array $context = [] */): bool;
}
4 changes: 3 additions & 1 deletion src/Symfony/Component/Serializer/Encoder/JsonDecode.php
Expand Up @@ -95,8 +95,10 @@ public function decode(string $data, string $format, array $context = []): mixed

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsDecoding(string $format): bool
public function supportsDecoding(string $format /*, array $context = [] */): bool
{
return JsonEncoder::FORMAT === $format;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Serializer/Encoder/JsonEncode.php
Expand Up @@ -57,8 +57,10 @@ public function encode(mixed $data, string $format, array $context = []): string

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsEncoding(string $format): bool
public function supportsEncoding(string $format /*, array $context = [] */): bool
{
return JsonEncoder::FORMAT === $format;
}
Expand Down
8 changes: 6 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/JsonEncoder.php
Expand Up @@ -47,16 +47,20 @@ public function decode(string $data, string $format, array $context = []): mixed

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsEncoding(string $format): bool
public function supportsEncoding(string $format /*, array $context = [] */): bool
{
return self::FORMAT === $format;
}

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsDecoding(string $format): bool
public function supportsDecoding(string $format /*, array $context = [] */): bool
{
return self::FORMAT === $format;
}
Expand Down
8 changes: 6 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Expand Up @@ -174,16 +174,20 @@ public function decode(string $data, string $format, array $context = []): mixed

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsEncoding(string $format): bool
public function supportsEncoding(string $format /*, array $context = [] */): bool
{
return self::FORMAT === $format;
}

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsDecoding(string $format): bool
public function supportsDecoding(string $format /*, array $context = [] */): bool
{
return self::FORMAT === $format;
}
Expand Down
8 changes: 6 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/YamlEncoder.php
Expand Up @@ -67,8 +67,10 @@ public function encode(mixed $data, string $format, array $context = []): string

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsEncoding(string $format): bool
public function supportsEncoding(string $format /*, array $context = [] */): bool
{
return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format;
}
Expand All @@ -85,8 +87,10 @@ public function decode(string $data, string $format, array $context = []): mixed

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsDecoding(string $format): bool
public function supportsDecoding(string $format /*, array $context = [] */): bool
{
return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format;
}
Expand Down
Expand Up @@ -134,8 +134,10 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsNormalization(mixed $data, string $format = null)
public function supportsNormalization(mixed $data, string $format = null /*, array $context = [] */)
{
return \is_object($data) && !$data instanceof \Traversable;
}
Expand Down Expand Up @@ -349,8 +351,10 @@ abstract protected function getAttributeValue(object $object, string $attribute,

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsDenormalization(mixed $data, string $type, string $format = null)
public function supportsDenormalization(mixed $data, string $type, string $format = null /*, array $context = [] */)
{
return class_exists($type) || (interface_exists($type, false) && $this->classDiscriminatorResolver && null !== $this->classDiscriminatorResolver->getMappingForClass($type));
}
Expand Down
Expand Up @@ -37,7 +37,7 @@ public function normalize($object, $format = null, array $context = []): int|str
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null): bool
public function supportsNormalization($data, $format = null, array $context = []): bool
{
return $data instanceof \BackedEnum;
}
Expand Down Expand Up @@ -67,7 +67,7 @@ public function denormalize($data, $type, $format = null, array $context = []):
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null): bool
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
{
return is_subclass_of($type, \BackedEnum::class);
}
Expand Down
Expand Up @@ -106,8 +106,10 @@ public function normalize(mixed $object, string $format = null, array $context =

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsNormalization(mixed $data, string $format = null): bool
public function supportsNormalization(mixed $data, string $format = null /*, array $context = [] */): bool
{
return $data instanceof ConstraintViolationListInterface;
}
Expand Down
Expand Up @@ -15,6 +15,8 @@
* Adds the support of an extra $context parameter for the supportsDenormalization method.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*
* @deprecated since symfony/serializer 6.1, use DenormalizerInterface instead
*/
interface ContextAwareDenormalizerInterface extends DenormalizerInterface
{
Expand Down
Expand Up @@ -15,6 +15,8 @@
* Adds the support of an extra $context parameter for the supportsNormalization method.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*
* @deprecated since symfony/serializer 6.1, use NormalizerInterface instead
*/
interface ContextAwareNormalizerInterface extends NormalizerInterface
{
Expand Down
16 changes: 9 additions & 7 deletions src/Symfony/Component/Serializer/Normalizer/CustomNormalizer.php
Expand Up @@ -44,22 +44,24 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
/**
* Checks if the given class implements the NormalizableInterface.
*
* @param mixed $data Data to normalize
* @param string $format The format being (de-)serialized from or into
* @param mixed $data Data to normalize
* @param string $format The format being (de-)serialized from or into
* @param array $context
*/
public function supportsNormalization(mixed $data, string $format = null): bool
public function supportsNormalization(mixed $data, string $format = null /*, array $context = [] */): bool
{
return $data instanceof NormalizableInterface;
}

/**
* Checks if the given class implements the DenormalizableInterface.
*
* @param mixed $data Data to denormalize from
* @param string $type The class to which the data should be denormalized
* @param string $format The format being deserialized from
* @param mixed $data Data to denormalize from
* @param string $type The class to which the data should be denormalized
* @param string $format The format being deserialized from
* @param array $context
*/
public function supportsDenormalization(mixed $data, string $type, string $format = null): bool
public function supportsDenormalization(mixed $data, string $type, string $format = null /*, array $context = [] */): bool
{
return is_subclass_of($type, DenormalizableInterface::class);
}
Expand Down
Expand Up @@ -73,8 +73,10 @@ public function normalize(mixed $object, string $format = null, array $context =

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsNormalization(mixed $data, string $format = null): bool
public function supportsNormalization(mixed $data, string $format = null /*, array $context = [] */): bool
{
return $data instanceof \SplFileInfo;
}
Expand Down Expand Up @@ -117,8 +119,10 @@ public function denormalize(mixed $data, string $type, string $format = null, ar

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsDenormalization(mixed $data, string $type, string $format = null): bool
public function supportsDenormalization(mixed $data, string $type, string $format = null /*, array $context = [] */): bool
{
return isset(self::SUPPORTED_TYPES[$type]);
}
Expand Down
Expand Up @@ -49,8 +49,10 @@ public function normalize(mixed $object, string $format = null, array $context =

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsNormalization(mixed $data, string $format = null): bool
public function supportsNormalization(mixed $data, string $format = null /*, array $context = [] */): bool
{
return $data instanceof \DateInterval;
}
Expand Down Expand Up @@ -117,8 +119,10 @@ public function denormalize(mixed $data, string $type, string $format = null, ar

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsDenormalization(mixed $data, string $type, string $format = null): bool
public function supportsDenormalization(mixed $data, string $type, string $format = null /*, array $context = [] */): bool
{
return \DateInterval::class === $type;
}
Expand Down
Expand Up @@ -71,8 +71,10 @@ public function normalize(mixed $object, string $format = null, array $context =

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsNormalization(mixed $data, string $format = null): bool
public function supportsNormalization(mixed $data, string $format = null /*, array $context = [] */): bool
{
return $data instanceof \DateTimeInterface;
}
Expand Down Expand Up @@ -112,8 +114,10 @@ public function denormalize(mixed $data, string $type, string $format = null, ar

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsDenormalization(mixed $data, string $type, string $format = null): bool
public function supportsDenormalization(mixed $data, string $type, string $format = null /*, array $context = [] */): bool
{
return isset(self::SUPPORTED_TYPES[$type]);
}
Expand Down
Expand Up @@ -38,8 +38,10 @@ public function normalize(mixed $object, string $format = null, array $context =

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsNormalization(mixed $data, string $format = null): bool
public function supportsNormalization(mixed $data, string $format = null /*, array $context = [] */): bool
{
return $data instanceof \DateTimeZone;
}
Expand All @@ -64,8 +66,10 @@ public function denormalize(mixed $data, string $type, string $format = null, ar

/**
* {@inheritdoc}
*
* @param array $context
*/
public function supportsDenormalization(mixed $data, string $type, string $format = null): bool
public function supportsDenormalization(mixed $data, string $type, string $format = null /*, array $context = [] */): bool
{
return \DateTimeZone::class === $type;
}
Expand Down

0 comments on commit 6b9fafb

Please sign in to comment.