Skip to content

[Serializer] Remove deprecation layer #41657

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 9 additions & 34 deletions src/Symfony/Component/Serializer/Annotation/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,55 +25,30 @@
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
final class Context
{
private $context;
private $normalizationContext;
private $denormalizationContext;
private $groups;
private array $groups;

/**
* @param string|string[] $groups
*
* @throws InvalidArgumentException
*/
public function __construct(array $options = [], array $context = [], array $normalizationContext = [], array $denormalizationContext = [], string|array $groups = [])
{
if (!$context) {
if (!array_intersect((array_keys($options)), ['normalizationContext', 'groups', 'context', 'value', 'denormalizationContext'])) {
// gracefully supports context as first, unnamed attribute argument if it cannot be confused with Doctrine-style options
$context = $options;
} else {
trigger_deprecation('symfony/serializer', '5.3', 'Passing an array of properties as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);

// If at least one of the options match, it's likely to be Doctrine-style options. Search for the context inside:
$context = $options['value'] ?? $options['context'] ?? [];
}
}

$normalizationContext = $options['normalizationContext'] ?? $normalizationContext;
$denormalizationContext = $options['denormalizationContext'] ?? $denormalizationContext;

foreach (compact(['context', 'normalizationContext', 'denormalizationContext']) as $key => $value) {
if (!\is_array($value)) {
throw new InvalidArgumentException(sprintf('Option "%s" of annotation "%s" must be an array.', $key, static::class));
}
}

public function __construct(
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be on one line

Copy link
Member

Choose a reason for hiding this comment

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

when using constructor property promotion, I think we should use several lines actually
we do so already for attributes

private array $context = [],
private array $normalizationContext = [],
private array $denormalizationContext = [],
string|array $groups = [],
) {
if (!$context && !$normalizationContext && !$denormalizationContext) {
throw new InvalidArgumentException(sprintf('At least one of the "context", "normalizationContext", or "denormalizationContext" options of annotation "%s" must be provided as a non-empty array.', static::class));
}

$groups = (array) ($options['groups'] ?? $groups);
$this->groups = (array) $groups;

foreach ($groups as $group) {
foreach ($this->groups as $group) {
if (!\is_string($group)) {
throw new InvalidArgumentException(sprintf('Parameter "groups" of annotation "%s" must be a string or an array of strings. Got "%s".', static::class, get_debug_type($group)));
}
}

$this->context = $context;
$this->normalizationContext = $normalizationContext;
$this->denormalizationContext = $denormalizationContext;
$this->groups = $groups;
}

public function getContext(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,17 @@
#[\Attribute(\Attribute::TARGET_CLASS)]
class DiscriminatorMap
{
private $typeProperty;
private $mapping;

public function __construct(string $typeProperty, array $mapping)
{
public function __construct(
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here

private string $typeProperty,
private array $mapping,
) {
if (empty($typeProperty)) {
throw new InvalidArgumentException(sprintf('Parameter "typeProperty" of annotation "%s" cannot be empty.', static::class));
}

if (empty($mapping)) {
throw new InvalidArgumentException(sprintf('Parameter "mapping" of annotation "%s" cannot be empty.', static::class));
}

$this->typeProperty = $typeProperty;
$this->mapping = $mapping;
}

public function getTypeProperty(): string
Expand Down
10 changes: 4 additions & 6 deletions src/Symfony/Component/Serializer/Annotation/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,24 @@ class Groups
/**
* @var string[]
*/
private $groups;
private array $groups;

/**
* @param string|string[] $groups
*/
public function __construct(string|array $groups)
{
$groups = (array) $groups;
$this->groups = (array) $groups;

if (empty($groups)) {
if (empty($this->groups)) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" cannot be empty.', static::class));
}

foreach ($groups as $group) {
foreach ($this->groups as $group) {
if (!\is_string($group) || '' === $group) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a string or an array of non-empty strings.', static::class));
}
}

$this->groups = $groups;
}

/**
Expand Down
7 changes: 1 addition & 6 deletions src/Symfony/Component/Serializer/Annotation/MaxDepth.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,10 @@
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
class MaxDepth
{
private $maxDepth;

public function __construct(int $maxDepth)
{
public function __construct(private int $maxDepth) {
if ($maxDepth <= 0) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a positive integer.', static::class));
}

$this->maxDepth = $maxDepth;
}

public function getMaxDepth()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
final class SerializedName
{
private $serializedName;

public function __construct(string $serializedName)
public function __construct(private string $serializedName)
{
if (empty($serializedName)) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a non-empty string.', static::class));
}

$this->serializedName = $serializedName;
}

public function getSerializedName(): string
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

6.0
---

* Remove `ArrayDenormalizer::setSerializer()`, call `setDenormalizer()` instead
* Remove the ability to create instances of the annotation classes by passing an array of parameters, use named arguments instead

5.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,21 @@ class SerializerPass implements CompilerPassInterface
{
use PriorityTaggedServiceTrait;

private $serializerService;
private $normalizerTag;
private $encoderTag;

public function __construct(string $serializerService = 'serializer', string $normalizerTag = 'serializer.normalizer', string $encoderTag = 'serializer.encoder')
{
if (0 < \func_num_args()) {
trigger_deprecation('symfony/serializer', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
}

$this->serializerService = $serializerService;
$this->normalizerTag = $normalizerTag;
$this->encoderTag = $encoderTag;
}

public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition($this->serializerService)) {
if (!$container->hasDefinition('serializer')) {
return;
}

if (!$normalizers = $this->findAndSortTaggedServices($this->normalizerTag, $container)) {
throw new RuntimeException(sprintf('You must tag at least one service as "%s" to use the "%s" service.', $this->normalizerTag, $this->serializerService));
if (!$normalizers = $this->findAndSortTaggedServices('serializer.normalizer', $container)) {
throw new RuntimeException('You must tag at least one service as "serializer.normalizer" to use the "serializer" service.');
}

$serializerDefinition = $container->getDefinition($this->serializerService);
$serializerDefinition = $container->getDefinition('serializer');
$serializerDefinition->replaceArgument(0, $normalizers);

if (!$encoders = $this->findAndSortTaggedServices($this->encoderTag, $container)) {
throw new RuntimeException(sprintf('You must tag at least one service as "%s" to use the "%s" service.', $this->encoderTag, $this->serializerService));
if (!$encoders = $this->findAndSortTaggedServices('serializer.encoder', $container)) {
throw new RuntimeException('You must tag at least one service as "serializer.encoder" to use the "serializer" service.');
}

$serializerDefinition->replaceArgument(1, $encoders);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
use Symfony\Component\Serializer\Exception\BadMethodCallException;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerInterface;

/**
* Denormalizes arrays of objects.
Expand All @@ -25,7 +22,7 @@
*
* @final
*/
class ArrayDenormalizer implements ContextAwareDenormalizerInterface, DenormalizerAwareInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
class ArrayDenormalizer implements ContextAwareDenormalizerInterface, DenormalizerAwareInterface, CacheableSupportsMethodInterface
{
use DenormalizerAwareTrait;

Expand Down Expand Up @@ -73,24 +70,6 @@ public function supportsDenormalization(mixed $data, string $type, string $forma
&& $this->denormalizer->supportsDenormalization($data, substr($type, 0, -2), $format, $context);
}

/**
* {@inheritdoc}
*
* @deprecated call setDenormalizer() instead
*/
public function setSerializer(SerializerInterface $serializer)
{
if (!$serializer instanceof DenormalizerInterface) {
throw new InvalidArgumentException('Expected a serializer that also implements DenormalizerInterface.');
}

if (Serializer::class !== debug_backtrace()[1]['class'] ?? null) {
trigger_deprecation('symfony/serializer', '5.3', 'Calling "%s" is deprecated. Please call setDenormalizer() instead.');
}

$this->setDenormalizer($serializer);
}

/**
* {@inheritdoc}
*/
Expand Down
Loading