Skip to content

Commit 035b0dd

Browse files
committed
Add layer for new args in order to avoid BC Break
1 parent 19b1b7d commit 035b0dd

File tree

7 files changed

+104
-11
lines changed

7 files changed

+104
-11
lines changed

src/Symfony/Component/Serializer/Mapping/AttributeMetadata.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,20 @@ public function setSerializedNames(array $serializedNames): void
135135
/**
136136
* {@inheritdoc}
137137
*/
138-
public function setSerializedName(string $serializedName = null, array $groups = [])
138+
public function setSerializedName(string $serializedName = null/* , array $groups = [] */)
139139
{
140-
if (empty($groups)) {
140+
if (\func_num_args() < 2) {
141+
trigger_deprecation('symfony/serializer', '6.2', 'The "%s()" method will have a new "array $groups = []" argument in version 7.0, not defining it is deprecated.', __METHOD__);
142+
$groups = [];
143+
} else {
144+
$groups = func_get_arg(1);
145+
146+
if (!\is_array($groups)) {
147+
throw new \TypeError(sprintf('"%s": Argument $groups is expected to be an array, got "%s".', __METHOD__, get_debug_type($groups)));
148+
}
149+
}
150+
151+
if (!$groups) {
141152
$this->serializedNames['*'] = $serializedName;
142153
}
143154

@@ -157,8 +168,19 @@ public function getSerializedNames(): array
157168
/**
158169
* {@inheritdoc}
159170
*/
160-
public function getSerializedName(array $groups = []): ?string
171+
public function getSerializedName(/* array $groups = [] */): ?string
161172
{
173+
if (\func_num_args() < 1) {
174+
trigger_deprecation('symfony/serializer', '6.2', 'The "%s()" method will have a new "array $groups = []" argument in version 7.0, not defining it is deprecated.', __METHOD__);
175+
$groups = [];
176+
} else {
177+
$groups = func_get_arg(0);
178+
179+
if (!\is_array($groups)) {
180+
throw new \TypeError(sprintf('"%s": Argument $groups is expected to be an array, got "%s".', __METHOD__, get_debug_type($groups)));
181+
}
182+
}
183+
162184
foreach ($groups as $group) {
163185
if (isset($this->serializedNames[$group])) {
164186
return $this->serializedNames[$group];

src/Symfony/Component/Serializer/Mapping/AttributeMetadataInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ public function setSerializedNames(array $serializedNames): void;
6161
*
6262
* @param string[] $groups
6363
*/
64-
public function setSerializedName(string $serializedName = null, array $groups = []);
64+
public function setSerializedName(string $serializedName = null/* , array $groups = [] */);
6565

6666
/**
6767
* Gets the serialization name for given groups.
6868
*
6969
* @param string[] $groups
7070
*/
71-
public function getSerializedName(array $groups = []): ?string;
71+
public function getSerializedName(/* array $groups = [] */): ?string;
7272

7373
/**
7474
* Gets all the serialization names per group ("*" being the default serialization name).

src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
9090
$serializedNames = $data['serialized_name'];
9191

9292
if (\is_string($serializedNames)) {
93-
if (empty($serializedNames)) {
93+
if ('' === $serializedNames) {
9494
throw new MappingException(sprintf('The "serialized_name" value must be a non-empty string in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()));
9595
}
9696
$attributeMetadata->setSerializedName($serializedNames, []);
9797
} elseif (\is_array($serializedNames)) {
9898
foreach ($serializedNames as $serializedName => $groups) {
99-
if (!\is_string($serializedName) || empty($serializedName)) {
99+
if (!\is_string($serializedName) || !$serializedName) {
100100
throw new MappingException(sprintf('The key for "serialized_name" array must be a non-empty string in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()));
101101
}
102102

src/Symfony/Component/Serializer/Tests/Mapping/AttributeMetadataTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Serializer\Tests\Mapping;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
1617
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
1718

@@ -20,6 +21,8 @@
2021
*/
2122
class AttributeMetadataTest extends TestCase
2223
{
24+
use ExpectDeprecationTrait;
25+
2326
public function testInterface()
2427
{
2528
$attributeMetadata = new AttributeMetadata('name');
@@ -50,6 +53,20 @@ public function testMaxDepth()
5053
$this->assertEquals(69, $attributeMetadata->getMaxDepth());
5154
}
5255

56+
/**
57+
* @group legacy
58+
*/
59+
public function testSerializedName()
60+
{
61+
$attributeMetadata = new AttributeMetadata('name');
62+
63+
$this->expectDeprecation('Since symfony/serializer 6.2: The "Symfony\Component\Serializer\Mapping\AttributeMetadata::setSerializedName()" method will have a new "array $groups = []" argument in version 7.0, not defining it is deprecated.');
64+
$attributeMetadata->setSerializedName('serialized_name');
65+
66+
$this->expectDeprecation('Since symfony/serializer 6.2: The "Symfony\Component\Serializer\Mapping\AttributeMetadata::getSerializedName()" method will have a new "array $groups = []" argument in version 7.0, not defining it is deprecated.');
67+
$this->assertEquals('serialized_name', $attributeMetadata->getSerializedName());
68+
}
69+
5370
public function testSerializedNames()
5471
{
5572
$attributeMetadata = new AttributeMetadata('name');

src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Serializer\Tests\Mapping\Loader;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\Serializer\Exception\MappingException;
1617
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
1718
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping;
@@ -27,6 +28,7 @@
2728
abstract class AnnotationLoaderTest extends TestCase
2829
{
2930
use ContextMappingTestTrait;
31+
use ExpectDeprecationTrait;
3032

3133
protected AnnotationLoader $loader;
3234

@@ -82,13 +84,17 @@ public function testLoadMaxDepth()
8284
$this->assertEquals(3, $attributesMetadata['bar']->getMaxDepth());
8385
}
8486

85-
public function testLoadSerializedName()
87+
/**
88+
* @group legacy
89+
*/
90+
public function testLoadSerializedNameWithNoGroupTriggerDeprecation()
8691
{
8792
$classMetadata = new ClassMetadata($this->getNamespace().'\SerializedNameDummy');
8893
$this->loader->loadClassMetadata($classMetadata);
8994

9095
$attributesMetadata = $classMetadata->getAttributesMetadata();
9196

97+
$this->expectDeprecation('Since symfony/serializer 6.2: The "Symfony\Component\Serializer\Mapping\AttributeMetadata::getSerializedName()" method will have a new "array $groups = []" argument in version 7.0, not defining it is deprecated.');
9298
$this->assertEquals('baz', $attributesMetadata['foo']->getSerializedName());
9399
$this->assertEquals('qux', $attributesMetadata['bar']->getSerializedName());
94100
$this->assertEquals([
@@ -99,6 +105,23 @@ public function testLoadSerializedName()
99105
], $attributesMetadata['corge']->getSerializedNames());
100106
}
101107

108+
public function testLoadSerializedName()
109+
{
110+
$classMetadata = new ClassMetadata($this->getNamespace().'\SerializedNameDummy');
111+
$this->loader->loadClassMetadata($classMetadata);
112+
113+
$attributesMetadata = $classMetadata->getAttributesMetadata();
114+
115+
$this->assertEquals('baz', $attributesMetadata['foo']->getSerializedName([]));
116+
$this->assertEquals('qux', $attributesMetadata['bar']->getSerializedName([]));
117+
$this->assertEquals([
118+
'a' => 'nameTwo',
119+
'b' => 'nameTwo',
120+
'c' => 'nameThree',
121+
'*' => 'nameOne',
122+
], $attributesMetadata['corge']->getSerializedNames());
123+
}
124+
102125
public function testLoadClassMetadataAndMerge()
103126
{
104127
$classMetadata = new ClassMetadata($this->getNamespace().'\GroupDummy');

src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Serializer\Tests\Mapping\Loader;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
1617
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping;
1718
use Symfony\Component\Serializer\Mapping\ClassMetadata;
@@ -30,6 +31,7 @@
3031
class XmlFileLoaderTest extends TestCase
3132
{
3233
use ContextMappingTestTrait;
34+
use ExpectDeprecationTrait;
3335

3436
/**
3537
* @var XmlFileLoader
@@ -74,16 +76,30 @@ public function testMaxDepth()
7476
$this->assertEquals(3, $attributesMetadata['bar']->getMaxDepth());
7577
}
7678

77-
public function testSerializedName()
79+
/**
80+
* @group legacy
81+
*/
82+
public function testSerializedNameWithNoGroupTriggerDeprecation()
7883
{
7984
$classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\Annotations\SerializedNameDummy');
8085
$this->loader->loadClassMetadata($classMetadata);
8186

8287
$attributesMetadata = $classMetadata->getAttributesMetadata();
88+
89+
$this->expectDeprecation('Since symfony/serializer 6.2: The "Symfony\Component\Serializer\Mapping\AttributeMetadata::getSerializedName()" method will have a new "array $groups = []" argument in version 7.0, not defining it is deprecated.');
8390
$this->assertEquals('baz', $attributesMetadata['foo']->getSerializedName());
8491
$this->assertEquals('qux', $attributesMetadata['bar']->getSerializedName());
85-
8692
$this->assertEquals('nameDefault', $attributesMetadata['quux']->getSerializedName());
93+
}
94+
95+
public function testSerializedName()
96+
{
97+
$classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\Annotations\SerializedNameDummy');
98+
$this->loader->loadClassMetadata($classMetadata);
99+
100+
$attributesMetadata = $classMetadata->getAttributesMetadata();
101+
102+
$this->assertEquals('nameDefault', $attributesMetadata['quux']->getSerializedName([]));
87103
$this->assertEquals('nameOne', $attributesMetadata['quux']->getSerializedName(['groupB']));
88104
$this->assertEquals('nameTwo', $attributesMetadata['quux']->getSerializedName(['groupC']));
89105
}

src/Symfony/Component/Serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Serializer\Tests\Mapping\Loader;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\Serializer\Exception\MappingException;
1617
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
1718
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping;
@@ -31,6 +32,7 @@
3132
class YamlFileLoaderTest extends TestCase
3233
{
3334
use ContextMappingTestTrait;
35+
use ExpectDeprecationTrait;
3436

3537
/**
3638
* @var YamlFileLoader
@@ -87,14 +89,27 @@ public function testMaxDepth()
8789
$this->assertEquals(3, $attributesMetadata['bar']->getMaxDepth());
8890
}
8991

90-
public function testSerializedName()
92+
/**
93+
* @group legacy
94+
*/
95+
public function testSerializedNameWithNoGroupTriggerDeprecation()
9196
{
9297
$classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\Annotations\SerializedNameDummy');
9398
$this->loader->loadClassMetadata($classMetadata);
9499

95100
$attributesMetadata = $classMetadata->getAttributesMetadata();
101+
102+
$this->expectDeprecation('Since symfony/serializer 6.2: The "Symfony\Component\Serializer\Mapping\AttributeMetadata::getSerializedName()" method will have a new "array $groups = []" argument in version 7.0, not defining it is deprecated.');
96103
$this->assertEquals('baz', $attributesMetadata['foo']->getSerializedName());
97104
$this->assertEquals('qux', $attributesMetadata['bar']->getSerializedName());
105+
}
106+
107+
public function testSerializedName()
108+
{
109+
$classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\Annotations\SerializedNameDummy');
110+
$this->loader->loadClassMetadata($classMetadata);
111+
112+
$attributesMetadata = $classMetadata->getAttributesMetadata();
98113

99114
$this->assertEquals('nameOne', $attributesMetadata['quux']->getSerializedName(['groupA']));
100115
$this->assertEquals('nameOne', $attributesMetadata['quux']->getSerializedName(['groupB']));

0 commit comments

Comments
 (0)