-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serialized] allow configuring the serialized name of properties through metadata #28505
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
…ugh metadata
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?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\Annotation; | ||
|
||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* Annotation class for @SerializedName(). | ||
* | ||
* @Annotation | ||
* @Target({"PROPERTY", "METHOD"}) | ||
* | ||
* @author Fabien Bourigault <bourigaultfabien@gmail.com> | ||
*/ | ||
final class SerializedName | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $serializedName; | ||
|
||
public function __construct(array $data) | ||
{ | ||
if (!isset($data['value'])) { | ||
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', \get_class($this))); | ||
} | ||
|
||
if (!\is_string($data['value']) || empty($data['value'])) { | ||
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a non-empty string.', \get_class($this))); | ||
} | ||
|
||
$this->serializedName = $data['value']; | ||
} | ||
|
||
public function getSerializedName(): string | ||
{ | ||
return $this->serializedName; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,15 @@ 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. | ||
*/ | ||
public $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. Is it still really necessary to make this property public? We do that everywhere in validator (and I copied this behavior in Serializer), but I'm not sure that it's still accurate. |
||
|
||
public function __construct(string $name) | ||
{ | ||
$this->name = $name; | ||
|
@@ -88,6 +97,22 @@ public function getMaxDepth() | |
return $this->maxDepth; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setSerializedName(string $serializedName = null) | ||
{ | ||
$this->serializedName = $serializedName; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSerializedName(): ?string | ||
{ | ||
return $this->serializedName; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -101,6 +126,11 @@ public function merge(AttributeMetadataInterface $attributeMetadata) | |
if (null === $this->maxDepth) { | ||
$this->maxDepth = $attributeMetadata->getMaxDepth(); | ||
} | ||
|
||
// Overwrite only if not defined | ||
if (null === $this->serializedName) { | ||
$this->serializedName = $attributeMetadata->getSerializedName(); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -110,6 +140,6 @@ public function merge(AttributeMetadataInterface $attributeMetadata) | |
*/ | ||
public function __sleep() | ||
{ | ||
return array('name', 'groups', 'maxDepth'); | ||
return array('name', 'groups', 'maxDepth', 'serializedName'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,16 @@ public function setMaxDepth($maxDepth); | |
*/ | ||
public function getMaxDepth(); | ||
|
||
/** | ||
* Sets the serialization name for this attribute. | ||
*/ | ||
public function setSerializedName(string $serializedName = null); | ||
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. BC break? why not otherwise? 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. OK, internal interface. |
||
|
||
/** | ||
* Gets the serialization name for this attribute. | ||
*/ | ||
public function getSerializedName(): ?string; | ||
|
||
/** | ||
* Merges an {@see AttributeMetadataInterface} with in the current one. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?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\NameConverter; | ||
|
||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; | ||
|
||
/** | ||
* @author Fabien Bourigault <bourigaultfabien@gmail.com> | ||
*/ | ||
final class MetadataAwareNameConverter implements AdvancedNameConverterInterface | ||
{ | ||
private $metadataFactory; | ||
|
||
/** | ||
* @var NameConverterInterface|AdvancedNameConverterInterface|null | ||
*/ | ||
private $fallbackNameConverter; | ||
|
||
private static $normalizeCache = array(); | ||
|
||
private static $denormalizeCache = array(); | ||
|
||
private static $attributesMetadataCache = array(); | ||
|
||
public function __construct(ClassMetadataFactoryInterface $metadataFactory, NameConverterInterface $fallbackNameConverter = null) | ||
{ | ||
$this->metadataFactory = $metadataFactory; | ||
$this->fallbackNameConverter = $fallbackNameConverter; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function normalize($propertyName, string $class = null, string $format = null, array $context = array()) | ||
{ | ||
if (null === $class) { | ||
return $this->normalizeFallback($propertyName, $class, $format, $context); | ||
} | ||
|
||
if (!isset(self::$normalizeCache[$class][$propertyName])) { | ||
self::$normalizeCache[$class][$propertyName] = $this->getCacheValueForNormalization($propertyName, $class); | ||
} | ||
|
||
fbourigault marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return self::$normalizeCache[$class][$propertyName] ?? $this->normalizeFallback($propertyName, $class, $format, $context); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function denormalize($propertyName, string $class = null, string $format = null, array $context = array()) | ||
{ | ||
if (null === $class) { | ||
return $this->denormalizeFallback($propertyName, $class, $format, $context); | ||
} | ||
|
||
if (!isset(self::$denormalizeCache[$class][$propertyName])) { | ||
self::$denormalizeCache[$class][$propertyName] = $this->getCacheValueForDenormalization($propertyName, $class); | ||
} | ||
|
||
return self::$denormalizeCache[$class][$propertyName] ?? $this->denormalizeFallback($propertyName, $class, $format, $context); | ||
} | ||
|
||
private function getCacheValueForNormalization(string $propertyName, string $class): ?string | ||
{ | ||
if (!$this->metadataFactory->hasMetadataFor($class)) { | ||
return null; | ||
} | ||
|
||
return $this->metadataFactory->getMetadataFor($class)->getAttributesMetadata()[$propertyName]->getSerializedName() ?? null; | ||
} | ||
|
||
private function normalizeFallback(string $propertyName, string $class = null, string $format = null, array $context = array()): string | ||
{ | ||
return $this->fallbackNameConverter ? $this->fallbackNameConverter->normalize($propertyName, $class, $format, $context) : $propertyName; | ||
} | ||
|
||
private function getCacheValueForDenormalization(string $propertyName, string $class): ?string | ||
{ | ||
if (!isset(self::$attributesMetadataCache[$class])) { | ||
self::$attributesMetadataCache[$class] = $this->getCacheValueForAttributesMetadata($class); | ||
} | ||
|
||
return self::$attributesMetadataCache[$class][$propertyName] ?? null; | ||
} | ||
|
||
private function denormalizeFallback(string $propertyName, string $class = null, string $format = null, array $context = array()): string | ||
{ | ||
return $this->fallbackNameConverter ? $this->fallbackNameConverter->denormalize($propertyName, $class, $format, $context) : $propertyName; | ||
} | ||
|
||
private function getCacheValueForAttributesMetadata(string $class): array | ||
{ | ||
if (!$this->metadataFactory->hasMetadataFor($class)) { | ||
return array(); | ||
} | ||
|
||
$classMetadata = $this->metadataFactory->getMetadataFor($class); | ||
|
||
$cache = array(); | ||
foreach ($classMetadata->getAttributesMetadata() as $name => $metadata) { | ||
if (null === $metadata->getSerializedName()) { | ||
continue; | ||
} | ||
|
||
$cache[$metadata->getSerializedName()] = $name; | ||
} | ||
|
||
return $cache; | ||
} | ||
} |
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.
Personally, I'm ok to bump this dependency. But feature detection may be used to support both versions. WDYT @symfony/deciders?