Skip to content

Commit 19b1b7d

Browse files
committed
[Serializer] Allow tags and attribute at same time for xml and same key for yaml
1 parent eef8870 commit 19b1b7d

File tree

10 files changed

+26
-53
lines changed

10 files changed

+26
-53
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CHANGELOG
77
* Add support for constructor promoted properties to `Context` attribute
88
* Add context option `PropertyNormalizer::NORMALIZE_VISIBILITY` with bitmask flags `PropertyNormalizer::NORMALIZE_PUBLIC`, `PropertyNormalizer::NORMALIZE_PROTECTED`, `PropertyNormalizer::NORMALIZE_PRIVATE`
99
* Add method `withNormalizeVisibility` to `PropertyNormalizerContextBuilder`
10-
* Add support for `SerializedName` groups including annotations, attributes, XML and YAML mapping.
10+
* Add serialized name group support
1111

1212
6.1
1313
---

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function setSerializedNames(array $serializedNames): void
135135
/**
136136
* {@inheritdoc}
137137
*/
138-
public function setSerializedName(string $serializedName = null, array $groups = []): void
138+
public function setSerializedName(string $serializedName = null, array $groups = [])
139139
{
140140
if (empty($groups)) {
141141
$this->serializedNames['*'] = $serializedName;

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

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

6666
/**
6767
* Gets the serialization name for given groups.

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\Serializer\Mapping\Loader;
1313

1414
use Symfony\Component\Config\Util\XmlUtils;
15-
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
1615
use Symfony\Component\Serializer\Exception\MappingException;
1716
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
1817
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping;
@@ -68,14 +67,7 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
6867
$attributeMetadata->setMaxDepth((int) $attribute['max-depth']);
6968
}
7069

71-
$hasSerializedNameAttribute = isset($attribute['serialized-name']);
72-
$hasSerializedNameElements = !empty($attribute->serialized_name);
73-
74-
if ($hasSerializedNameAttribute && $hasSerializedNameElements) {
75-
throw new InvalidArgumentException(sprintf('You cannot use both the attribute "serialized-name" and <serialized_name> tags at the same time in file "%s".', $this->file));
76-
}
77-
78-
if ($hasSerializedNameAttribute) {
70+
if (isset($attribute['serialized-name'])) {
7971
$attributeMetadata->setSerializedName((string) $attribute['serialized-name'], []);
8072
}
8173

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

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Serializer\Mapping\Loader;
1313

14-
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
1514
use Symfony\Component\Serializer\Exception\MappingException;
1615
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
1716
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping;
@@ -87,40 +86,24 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
8786
$attributeMetadata->setMaxDepth($data['max_depth']);
8887
}
8988

90-
$hasSerializedNameAttribute = isset($data['serialized_name']);
91-
$hasSerializedNameElements = isset($data['serialized_names']);
92-
93-
if ($hasSerializedNameAttribute && $hasSerializedNameElements) {
94-
throw new InvalidArgumentException(sprintf('You cannot use keys "serialized_name" and "serialized_names" at the same time in file "%s".', $this->file));
95-
}
96-
97-
if ($hasSerializedNameAttribute) {
98-
if (!\is_string($data['serialized_name']) || empty($data['serialized_name'])) {
99-
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()));
100-
}
101-
102-
$attributeMetadata->setSerializedName($data['serialized_name'], []);
103-
}
104-
105-
if ($hasSerializedNameElements) {
106-
$serializedNames = $data['serialized_names'];
107-
if (!\is_string($serializedNames) && !\is_array($serializedNames)) {
108-
throw new MappingException(sprintf('The "serialized_names" value must be a non-empty string or an array of serialized name/groups in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()));
109-
}
89+
if (isset($data['serialized_name'])) {
90+
$serializedNames = $data['serialized_name'];
11091

11192
if (\is_string($serializedNames)) {
112-
if (!$serializedNames) {
113-
throw new MappingException(sprintf('The "serialized_names" value must be a non-empty string in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()));
93+
if (empty($serializedNames)) {
94+
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()));
11495
}
11596
$attributeMetadata->setSerializedName($serializedNames, []);
116-
} else {
97+
} elseif (\is_array($serializedNames)) {
11798
foreach ($serializedNames as $serializedName => $groups) {
11899
if (!\is_string($serializedName) || empty($serializedName)) {
119-
throw new MappingException(sprintf('The key for "serialized_names" array must be a non-empty string in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()));
100+
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()));
120101
}
121102

122103
$attributeMetadata->setSerializedName($serializedName, (array) $groups);
123104
}
105+
} else {
106+
throw new MappingException(sprintf('The "serialized_name" value must be a non-empty string or an array of serialized name/groups in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()));
124107
}
125108
}
126109

src/Symfony/Component/Serializer/Tests/Fixtures/serialization.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<class name="Symfony\Component\Serializer\Tests\Fixtures\Annotations\SerializedNameDummy">
2424
<attribute name="foo" serialized-name="baz" />
2525
<attribute name="bar" serialized-name="qux" />
26-
<attribute name="corge">
26+
<attribute name="quux" serialized-name="nameDefault">
2727
<serialized_name name="nameOne">
2828
<group>groupA</group>
2929
<group>groupB</group>

src/Symfony/Component/Serializer/Tests/Fixtures/serialization.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
bar:
1818
serialized_name: 'qux'
1919
quux:
20-
serialized_names: 'qux'
21-
corge:
22-
serialized_names:
20+
serialized_name:
2321
nameOne: ['groupA', 'groupB']
2422
nameTwo: 'groupC'
2523
'Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummy':

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ public function testLoadSerializedName()
8989

9090
$attributesMetadata = $classMetadata->getAttributesMetadata();
9191

92-
$this->assertEquals('baz', $attributesMetadata['foo']->getSerializedName([]));
93-
$this->assertEquals('qux', $attributesMetadata['bar']->getSerializedName([]));
92+
$this->assertEquals('baz', $attributesMetadata['foo']->getSerializedName());
93+
$this->assertEquals('qux', $attributesMetadata['bar']->getSerializedName());
9494
$this->assertEquals([
9595
'a' => 'nameTwo',
9696
'b' => 'nameTwo',

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ public function testSerializedName()
8080
$this->loader->loadClassMetadata($classMetadata);
8181

8282
$attributesMetadata = $classMetadata->getAttributesMetadata();
83-
$this->assertEquals('baz', $attributesMetadata['foo']->getSerializedName([]));
84-
$this->assertEquals('qux', $attributesMetadata['bar']->getSerializedName([]));
83+
$this->assertEquals('baz', $attributesMetadata['foo']->getSerializedName());
84+
$this->assertEquals('qux', $attributesMetadata['bar']->getSerializedName());
8585

86-
$this->assertEquals('nameOne', $attributesMetadata['corge']->getSerializedName(['groupA']));
87-
$this->assertEquals('nameOne', $attributesMetadata['corge']->getSerializedName(['groupB']));
88-
$this->assertEquals('nameTwo', $attributesMetadata['corge']->getSerializedName(['groupC']));
86+
$this->assertEquals('nameDefault', $attributesMetadata['quux']->getSerializedName());
87+
$this->assertEquals('nameOne', $attributesMetadata['quux']->getSerializedName(['groupB']));
88+
$this->assertEquals('nameTwo', $attributesMetadata['quux']->getSerializedName(['groupC']));
8989
}
9090

9191
public function testLoadDiscriminatorMap()

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ public function testSerializedName()
9393
$this->loader->loadClassMetadata($classMetadata);
9494

9595
$attributesMetadata = $classMetadata->getAttributesMetadata();
96-
$this->assertEquals('baz', $attributesMetadata['foo']->getSerializedName([]));
97-
$this->assertEquals('qux', $attributesMetadata['bar']->getSerializedName([]));
96+
$this->assertEquals('baz', $attributesMetadata['foo']->getSerializedName());
97+
$this->assertEquals('qux', $attributesMetadata['bar']->getSerializedName());
9898

99-
$this->assertEquals('nameOne', $attributesMetadata['corge']->getSerializedName(['groupA']));
100-
$this->assertEquals('nameOne', $attributesMetadata['corge']->getSerializedName(['groupB']));
101-
$this->assertEquals('nameTwo', $attributesMetadata['corge']->getSerializedName(['groupC'])); // TODO TODO add test for throw checks
99+
$this->assertEquals('nameOne', $attributesMetadata['quux']->getSerializedName(['groupA']));
100+
$this->assertEquals('nameOne', $attributesMetadata['quux']->getSerializedName(['groupB']));
101+
$this->assertEquals('nameTwo', $attributesMetadata['quux']->getSerializedName(['groupC']));
102102
}
103103

104104
public function testLoadDiscriminatorMap()

0 commit comments

Comments
 (0)