-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
SerializedName based on Groups #37903
Conversation
What do you think about using the following signature: /**
* @SerializedName("bar") <-- Applied only when no groups are provided
* @SerializedName("baz", groups={"a", "b"}) <-- Applied if group is a or b
* @SerializedName("bat", groups="c") <-- Applied if group is c
*/
$foo; If find this easier to read and write. |
The doctrine annotation reader is capable of handling the same annotation multiple time on a property so this shouldn't be a problem. |
1d61e0f
to
9620fe8
Compare
cc @dunglas or other people for review |
src/Symfony/Component/Serializer/Mapping/AttributeMetadataInterface.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Mapping/AttributeMetadataInterface.php
Outdated
Show resolved
Hide resolved
04c47ef
to
3c24e9f
Compare
src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/NameConverter/MetadataAwareNameConverter.php
Outdated
Show resolved
Hide resolved
@jsamouh Can you squash your commits? |
56b6f1c
to
d77f3d0
Compare
d77f3d0
to
14beaec
Compare
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
you cannot pass null here.
* Adds the serialization name for this attribute. | ||
*/ | ||
public function addSerializedName(string $serializedName, array $groups = []); | ||
|
||
/** | ||
* Gets the serialization name for this attribute. | ||
*/ | ||
public function getSerializedName(): ?string; |
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.
id remove/deprecate this one
|
||
foreach ($this->serializedNames as $serializedName => $groupsForSerializedName) { | ||
if (!$groupsForSerializedName) { | ||
$defaultSerializedName = $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.
i dont think you can make this assumption :/ any name without groups could be considered default
i think we should also validate that there can only be 1 default name (no groups) as well as validating no overlap, e.g. 2 names with the same groups. |
I understand your point... I m shared between let the behavior like this and be strict on it. Need a second thought on that cc @dunglas @fabpot Thanks for your feedback |
I tend to agree with @ro0NL |
serialized_name: 'full_name' | ||
serialized_names: 'full_name' |
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:
ser_name: 'some'
ser_names:
some: ~
other: [grp1, grp2]
} | ||
$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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
|| '' === trim($serializedName)
/** | ||
* 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 comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO should this one should be setSerializedNames
(no need for an adder) - which simplifies resetting
setSerializedName(null) previous, vs setSerializedNames([]) now
We've just moved away from |
Is there already a new PR for this? |
I don't think so. |
will dot it next week |
This would still be immensely useful for refactoring/versioning things. |
New Feature: Serialized Name based on Groups