Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Conversation

jsamouh
Copy link
Contributor

@jsamouh jsamouh commented Aug 21, 2020

Q A
Branch? master
Bug fix? no
New feature? yes
Deprecations? yes
Tickets Fix #30483
License MIT
Doc PR symfony/symfony-docs#...

New Feature: Serialized Name based on Groups

/**
 * @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

// YAML 
  attributes:
    foo:
      serialized_names:
         bar: ~
         baz: ['a', 'b']
         bat: ['c']

//XML
        <attribute name="foo" serialized-name="bar" />
        <attribute name="foo" serialized-name="baz">
             <group>a</group>
             <group>b</group>
       </attribute>
        <attribute name="foo" serialized-name="bat">
             <group>c</group>
       </attribute>

@jsamouh jsamouh requested a review from dunglas as a code owner August 21, 2020 01:01
@jsamouh jsamouh changed the title First try Serialized based on groups on Annotation First try SerializedName based on groups on Annotation Aug 21, 2020
@jsamouh jsamouh marked this pull request as draft August 21, 2020 01:19
@jsamouh jsamouh changed the title First try SerializedName based on groups on Annotation [Draft] SerializedName based on Groups Aug 21, 2020
@fabpot fabpot added this to the next milestone Aug 23, 2020
@dunglas
Copy link
Member

dunglas commented Aug 24, 2020

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.

@jvasseur
Copy link
Contributor

The doctrine annotation reader is capable of handling the same annotation multiple time on a property so this shouldn't be a problem.

@jsamouh jsamouh force-pushed the issue-30483-serialized-name-based-on-groups branch from 1d61e0f to 9620fe8 Compare August 26, 2020 02:35
@jsamouh
Copy link
Contributor Author

jsamouh commented Aug 26, 2020

cc @dunglas or other people for review

@jsamouh jsamouh changed the title [Draft] SerializedName based on Groups SerializedName based on Groups Aug 26, 2020
@jsamouh jsamouh marked this pull request as ready for review August 26, 2020 16:09
@fabpot fabpot requested a review from dunglas August 26, 2020 16:42
@jsamouh jsamouh force-pushed the issue-30483-serialized-name-based-on-groups branch from 04c47ef to 3c24e9f Compare August 26, 2020 19:23
@fabpot
Copy link
Member

fabpot commented Sep 2, 2020

@jsamouh Can you squash your commits?

@jsamouh jsamouh force-pushed the issue-30483-serialized-name-based-on-groups branch 2 times, most recently from 56b6f1c to d77f3d0 Compare September 2, 2020 14:24
@jsamouh jsamouh force-pushed the issue-30483-serialized-name-based-on-groups branch from d77f3d0 to 14beaec Compare September 2, 2020 14:33
@@ -111,15 +109,51 @@ public function getMaxDepth()
*/
public function setSerializedName(string $serializedName = null)
{
$this->serializedName = $serializedName;
$this->addSerializedName($serializedName);
Copy link
Contributor

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;
Copy link
Contributor

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;
Copy link
Contributor

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

@ro0NL
Copy link
Contributor

ro0NL commented Sep 2, 2020

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.

@jsamouh
Copy link
Contributor Author

jsamouh commented Sep 2, 2020

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

@fabpot
Copy link
Member

fabpot commented Sep 2, 2020

I tend to agree with @ro0NL

serialized_name: 'full_name'
serialized_names: 'full_name'
Copy link
Contributor

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'])) {
Copy link
Contributor

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)) {
Copy link
Contributor

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 = []);
Copy link
Contributor

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

@jsamouh
Copy link
Contributor Author

jsamouh commented Sep 4, 2020

Thanks for your comments @ro0NL ,

To prevent back and forth, and because some of your comments are the opposite from previous comments suggested, I m waiting @dunglas to review...

@fabpot fabpot closed this Oct 7, 2020
@fabpot
Copy link
Member

fabpot commented Oct 7, 2020

We've just moved away from master as the main branch to use 5.x instead. Unfortunately, I cannot reopen the PR and change the target branch to 5.x. Can you open a new PR referencing this one to not loose the discussion? Thank you for your understanding and for your help.

@hafkenscheid
Copy link

Is there already a new PR for this?

@derrabus
Copy link
Member

I don't think so.

@jsamouh
Copy link
Contributor Author

jsamouh commented Sep 17, 2021

will dot it next week

@janvernieuwe
Copy link
Contributor

This would still be immensely useful for refactoring/versioning things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Serializer] SerializedName based on groups