-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Allow to add groups to SerializedName annotation/attribute #46432
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
base: 7.4
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -42,13 +42,13 @@ class AttributeMetadata implements AttributeMetadataInterface | |
public $maxDepth; | ||
|
||
/** | ||
* @var string|null | ||
* @var array<string, string|null> An array of serialized names by group | ||
* | ||
* @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 $serializedName = []; | ||
|
||
/** | ||
* @internal This property is public in order to reduce the size of the | ||
|
@@ -116,20 +116,68 @@ public function getMaxDepth(): ?int | |
return $this->maxDepth; | ||
} | ||
|
||
public function setSerializedName(string $serializedName = null) | ||
alamirault marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public function setSerializedNames(array $serializedNames): void | ||
{ | ||
$this->serializedName = $serializedNames; | ||
} | ||
|
||
/** | ||
* Set a serialization name for given groups. | ||
* | ||
* @param string[] $groups | ||
*/ | ||
public function setSerializedName(string $serializedName = null/* , array $groups = [] */) | ||
{ | ||
if (1 > \func_num_args()) { | ||
trigger_deprecation('symfony/serializer', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__); | ||
} | ||
|
||
if (\func_num_args() < 2) { | ||
$groups = []; | ||
} else { | ||
$groups = func_get_arg(1); | ||
|
||
if (!\is_array($groups)) { | ||
throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be array, "%s" given.', __METHOD__, get_debug_type($groups))); | ||
} | ||
} | ||
|
||
$this->serializedName = $serializedName; | ||
foreach ($groups ?: ['*'] as $group) { | ||
$this->serializedName[$group] = $serializedName; | ||
} | ||
} | ||
|
||
public function getSerializedName(): ?string | ||
public function getSerializedNames(): array | ||
{ | ||
return $this->serializedName; | ||
} | ||
|
||
/** | ||
* Gets the serialization name for given groups. | ||
* | ||
* @param string[] $groups | ||
*/ | ||
public function getSerializedName(/* array $groups = [] */): ?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. Why pass an array here? Shouldn't we accept string|null instead? 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. Used by Serializer group context can be multiple. So returned Should be done in |
||
{ | ||
if (\func_num_args() < 1) { | ||
$groups = []; | ||
} else { | ||
$groups = func_get_arg(0); | ||
|
||
if (!\is_array($groups)) { | ||
throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be array, "%s" given.', __METHOD__, get_debug_type($groups))); | ||
} | ||
} | ||
|
||
foreach ($groups as $group) { | ||
if (isset($this->serializedName[$group])) { | ||
return $this->serializedName[$group]; | ||
} | ||
} | ||
|
||
return $this->serializedName['*'] ?? null; | ||
} | ||
|
||
public function setSerializedPath(PropertyPath $serializedPath = null): void | ||
{ | ||
$this->serializedPath = $serializedPath; | ||
|
@@ -210,7 +258,7 @@ public function merge(AttributeMetadataInterface $attributeMetadata) | |
|
||
// Overwrite only if not defined | ||
$this->maxDepth ??= $attributeMetadata->getMaxDepth(); | ||
$this->serializedName ??= $attributeMetadata->getSerializedName(); | ||
$this->serializedName ??= $attributeMetadata->getSerializedNames(); | ||
$this->serializedPath ??= $attributeMetadata->getSerializedPath(); | ||
|
||
// Overwrite only if both contexts are empty | ||
|
@@ -233,4 +281,16 @@ public function __sleep(): array | |
{ | ||
return ['name', 'groups', 'maxDepth', 'serializedName', 'serializedPath', 'ignore', 'normalizationContexts', 'denormalizationContexts']; | ||
} | ||
|
||
public function __wakeup() | ||
{ | ||
// Preserve compatibility with existing serialized payloads | ||
if (null === $this->serializedName) { | ||
$this->serializedName = []; | ||
} elseif (\is_string($this->serializedName)) { | ||
$this->serializedName = [ | ||
'*' => $this->serializedName, | ||
]; | ||
} | ||
} | ||
} |
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.
For maximum BC, I think we should keep this as a string when possible, and use arrays only when actually required.
Uh oh!
There was an error while loading. Please reload this page.
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.
@nicolas-grekas , Tu be sure, somthing like this ?
And deal with string and array in every methods, is it right ?