-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
SerializedName based on Groups #37903
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\LegacyBundle\Entity\LegacyPerson: | ||
attributes: | ||
name: | ||
serialized_name: 'full_name' | ||
serialized_names: 'full_name' | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\ModernBundle\src\Entity\ModernPerson: | ||
attributes: | ||
name: | ||
serialized_name: 'full_name' | ||
serialized_names: 'full_name' |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,13 +42,11 @@ class AttributeMetadata implements AttributeMetadataInterface | |
public $maxDepth; | ||
|
||
/** | ||
* @var string|null | ||
* | ||
* @internal This property is public in order to reduce the size of the | ||
* class' serialized representation. Do not access it. Use | ||
* {@link getSerializedName()} instead. | ||
* {@link getSerializedNames()} instead. | ||
*/ | ||
public $serializedName; | ||
public $serializedNames = []; | ||
|
||
/** | ||
* @var bool | ||
|
@@ -111,15 +109,51 @@ public function getMaxDepth() | |
*/ | ||
public function setSerializedName(string $serializedName = null) | ||
{ | ||
$this->serializedName = $serializedName; | ||
$this->addSerializedName($serializedName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you cannot pass null here. |
||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function addSerializedName(string $serializedName, array $groups = []) | ||
{ | ||
$this->serializedNames[$serializedName] = $groups; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSerializedName(): ?string | ||
{ | ||
return $this->serializedName; | ||
return $this->getSerializedNameForGroups(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSerializedNames(): array | ||
{ | ||
return $this->serializedNames; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSerializedNameForGroups(array $groups = []): ?string | ||
{ | ||
$defaultSerializedName = null; | ||
|
||
foreach ($this->serializedNames as $serializedName => $groupsForSerializedName) { | ||
if (!$groupsForSerializedName) { | ||
$defaultSerializedName = $serializedName; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i dont think you can make this assumption :/ any name without groups could be considered default |
||
} | ||
|
||
if (array_intersect($groups, $groupsForSerializedName)) { | ||
return $serializedName; | ||
} | ||
} | ||
|
||
return $defaultSerializedName; | ||
} | ||
|
||
/** | ||
|
@@ -152,9 +186,9 @@ public function merge(AttributeMetadataInterface $attributeMetadata) | |
$this->maxDepth = $attributeMetadata->getMaxDepth(); | ||
} | ||
|
||
// Overwrite only if not defined | ||
if (null === $this->serializedName) { | ||
$this->serializedName = $attributeMetadata->getSerializedName(); | ||
// Overwrite only if empty or nullable array | ||
if (!$this->serializedNames) { | ||
$this->serializedNames = $attributeMetadata->getSerializedNames(); | ||
} | ||
|
||
if ($ignore = $attributeMetadata->isIgnored()) { | ||
|
@@ -169,6 +203,6 @@ public function merge(AttributeMetadataInterface $attributeMetadata) | |
*/ | ||
public function __sleep() | ||
{ | ||
return ['name', 'groups', 'maxDepth', 'serializedName', 'ignore']; | ||
return ['name', 'groups', 'maxDepth', 'serializedNames', 'ignore']; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,11 +56,26 @@ public function getMaxDepth(); | |
*/ | ||
public function setSerializedName(string $serializedName = null); | ||
|
||
/** | ||
* Adds the serialization name for this attribute. | ||
*/ | ||
public function addSerializedName(string $serializedName, array $groups = []); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO should this one should be setSerializedName(null) previous, vs setSerializedNames([]) now |
||
|
||
/** | ||
* Gets the serialization name for this attribute. | ||
*/ | ||
public function getSerializedName(): ?string; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. id remove/deprecate this one |
||
|
||
/** | ||
* Gets the serialization names for this attribute. | ||
*/ | ||
public function getSerializedNames(): array; | ||
|
||
/** | ||
* Gets the serialization name for this attribute. | ||
*/ | ||
public function getSerializedNameForGroups(array $groups = []): ?string; | ||
|
||
/** | ||
* Sets if this attribute must be ignored or not. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,12 +86,28 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata) | |
$attributeMetadata->setMaxDepth($data['max_depth']); | ||
} | ||
|
||
if (isset($data['serialized_name'])) { | ||
if (!\is_string($data['serialized_name']) || empty($data['serialized_name'])) { | ||
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())); | ||
if (isset($data['serialized_names'])) { | ||
if (!\is_string($data['serialized_names']) && !\is_array($data['serialized_names'])) { | ||
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())); | ||
} | ||
|
||
$attributeMetadata->setSerializedName($data['serialized_name']); | ||
if (\is_string($data['serialized_names'])) { | ||
if (!$data['serialized_names']) { | ||
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())); | ||
} | ||
$attributeMetadata->addSerializedName($data['serialized_names']); | ||
} elseif (\is_array($data['serialized_names'])) { | ||
if (empty($data['serialized_names'])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if (!$data['serialized_names']) { |
||
throw new MappingException(sprintf('The "serialized_names" value must be a non-empty array of serialized name/groups in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName())); | ||
} | ||
foreach ($data['serialized_names'] as $serializedName => $groups) { | ||
if (!\is_string($serializedName) || empty($serializedName)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. || '' === trim($serializedName) |
||
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())); | ||
} | ||
|
||
$attributeMetadata->addSerializedName($serializedName, (array) $groups); | ||
} | ||
} | ||
} | ||
|
||
if (isset($data['ignore'])) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
use Symfony\Component\Serializer\Annotation\Groups; | ||
use Symfony\Component\Serializer\Annotation\SerializedName; | ||
|
||
/** | ||
* @author Jordan Samouh <jordan.samouh@gmail.com> | ||
*/ | ||
class SerializedNameWithGroupsDummy | ||
{ | ||
/** | ||
* @SerializedName("baz") | ||
*/ | ||
public $foo; | ||
|
||
public $bar; | ||
|
||
public $quux; | ||
|
||
/** | ||
* @SerializedName("bazs") | ||
*/ | ||
public $foos; | ||
|
||
/** | ||
* @SerializedName("bargroups", groups={"group1"}) | ||
* @Groups({"group1"}) | ||
*/ | ||
public $barWithGroup; | ||
|
||
/** | ||
* @SerializedName("quuxgroups2", groups={"group1", "group2"}) | ||
* @SerializedName("quuxgroups1", groups={"group1"}) | ||
* @Groups({"group1", "group2"}) | ||
*/ | ||
public $quuxWithGroups; | ||
|
||
/** | ||
* @SerializedName("qux") | ||
* @Groups({"group1"}) | ||
*/ | ||
public function getBar() | ||
{ | ||
return $this->bar; | ||
} | ||
} |
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.
seems to break user configs, thus BC
IMHO we can keep supporting 2 formats: