Skip to content
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

[WIP] [Serializer] Alias support #15200

Closed
wants to merge 1 commit into from
Closed

Conversation

dunglas
Copy link
Member

@dunglas dunglas commented Jul 4, 2015

Q A
Bug fix? no
New feature? yes
BC breaks? yes
Deprecations? yes
Tests pass? not yet
Fixed tickets #15171
License MIT
Doc PR not yet

Follows up #15171. It's the POC I was speaking about @estahn

Progression:

  • Metadata (works fine)
  • Tests for the metadata
  • Name converter leveraging the new metadata
  • Tests for the new name converter
  • Update of current normalizers (the code exists but I've not tried yet)

The main issue about this PR is that it introduces a huge BC break in the Name Converter. To make it working it's needed to inject the current object or at least its class in the name converter and it's currently not possible.
What do you think about that @symfony/deciders?

@estahn you're welcome to contribute or rework this PR!

* @return string
*/
public function normalize($propertyName);
public function normalize($class, $propertyName);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An easy way to limit the BC break is to inverse parameters (same for denormalize).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the BC promise, you'd need to make $class the second argument and make it optional. That's not ideal, but I don't think there's another way

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weaverryan even if you make it optional, it is still a BC break: people implementing the interface will have to change their code, otherwise they will get PHP errors about signature mismatches.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm reading this from (my recent new best friend) out BC promise: http://symfony.com/doc/current/contributing/code/bc.html#changing-interfaces for non-API interfaces, we are allowed to add optional arguments to methods. Am I reading that wrong? Or perhaps that's not correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it says Should be avoided, because it is still a BC break. The Regular column is a column saying "We don't actually provide BC here, but only partial BC".

And actually, I don't think any code added since 2.3 has actually been tagged as @api. Many things are extension points for our users where we care about BC as if they were flagged as @api even though it was not done.
Given that we became better at writing BC layers than a few years back, we may decide to drop the distinction between Regular and API btw. The default behavior of a class should be full BC. The meaningful distinction would be Regular vs Internal (where internal classes not meant to be used by users are less strict on BC)

*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class Alias
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the SerializedName from JMS more than alias, which seems to tell me less about what it does. Any reason for using alias specifically?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weaverryan agree with you, but Alias is still understandable

@weaverryan
Copy link
Member

@dunglas would I have to use the AliasNameConverter to use aliases? Could I alias some properties, but allow other un-aliases properties to use a a different name converter?

@dunglas dunglas closed this Dec 5, 2015
@dunglas dunglas deleted the serializer_alias branch December 5, 2015 09:02
@dunglas dunglas restored the serializer_alias branch December 5, 2015 09:02
@dunglas dunglas deleted the serializer_alias branch December 5, 2015 09:03
@dunglas dunglas restored the serializer_alias branch December 5, 2015 09:05
@dunglas dunglas reopened this Dec 5, 2015
@fabpot
Copy link
Member

fabpot commented Jan 25, 2016

@dunglas This one looks unfinished. Will you have time to finish it in the coming weeks?

@dunglas
Copy link
Member Author

dunglas commented Jan 25, 2016

It's the last one on my todo list... Not sure it's even possible to finish it without BC break.

@teohhanhui
Copy link
Contributor

What about skipping the name converter entirely if there's an alias specified? I think that makes more sense conceptually (i.e. there's no need for automatic name generation because the desired name is already specified)...

@teohhanhui
Copy link
Contributor

Hopefully that also obviates the need to change the name converter's method signature...

@@ -70,25 +75,29 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
foreach ($reflectionClass->getMethods() as $method) {
if ($method->getDeclaringClass()->name === $className) {
foreach ($this->reader->getMethodAnnotations($method) as $groups) {
if (!preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes the getter name is simply the name of the property without any prefix. I think it would be good to support it too.

@fabpot
Copy link
Member

fabpot commented Jun 29, 2016

@dunglas any news about this one?

@dunglas
Copy link
Member Author

dunglas commented Jun 29, 2016

#18016 looks more promising (but is limited to ObjectNormalizer). I think we can close this PR.

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.

None yet

8 participants