Skip to content

[Serializer] Alias for attributes #15171

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

Closed
estahn opened this issue Jul 2, 2015 · 8 comments
Closed

[Serializer] Alias for attributes #15171

estahn opened this issue Jul 2, 2015 · 8 comments

Comments

@estahn
Copy link

estahn commented Jul 2, 2015

It would be very helpful to have the possibility to specify an alias for an attribute. This is the same as serialized_name in JMSSerializer".

@SerializedName
This annotation can be defined on a property to define the serialized name for a property. If this is not
defined, the property will be translated from camel-case to a lower-cased underscored name, e.g.
camelCase -> camel_case.

Here is an example YAML:

AppBundle\Entity\Product:
    attributes:
        id:
            groups: [default]
        name:
            groups: [default]
        brand.name:
            alias: brand
            groups: [default]

I wonder about the general take on this.

This was also mentioned in #14924 .

My current workaround is using a custom NameConverter.

use Symfony\Component\Serializer\Exception\UnsupportedException;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;

class ArrayNameConverter implements NameConverterInterface
{
    protected $map = [];

    /**
     * @param array $map
     */
    public function __construct(array $map)
    {
        $this->map = $map;
    }

    public function normalize($propertyName)
    {
        return isset($this->map[$propertyName]) ? $this->map[$propertyName] : $propertyName;
    }

    public function denormalize($propertyName)
    {
        throw new UnsupportedException('This method needs to be implemented.');
    }
}

$normalizer = new ObjectNormalizer($classMetadataFactory, new ArrayNameConverter(['brand.name' => 'brand']));
@dunglas
Copy link
Member

dunglas commented Jul 2, 2015

I've started a POC about that. It's very similar to what you describe. It uses a new name converter that alias (was named serializedName in my POC but alias looks better) metadata.

@estahn
Copy link
Author

estahn commented Jul 3, 2015

@dunglas Kevin, do you want me to contribute the code? It should be fairly simple to implement i assume.

@IndraGunawan
Copy link
Contributor

any news for this feature?

@magarzon
Copy link

magarzon commented Jan 18, 2017

@IndraGunawan the feature was in development by @dunglas in #15200, but was closed in favor of #18016, that for me is not the same

@stof
Copy link
Member

stof commented Jan 18, 2017

@magarzon #15200 was not mergeable, due to breaking BC.

@SebastianLeuters
Copy link

Hey guys,
whats the last state of this feature request?
It would be nice to implement an "alias" annotation to customise the property names for different serialization groups.

Best regards.

dunglas added a commit that referenced this issue Oct 5, 2018
…properties through metadata (fbourigault)

This PR was squashed before being merged into the 4.2-dev branch (closes #28505).

Discussion
----------

[Serialized] allow configuring the serialized name of properties through metadata

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #15171
| License       | MIT
| Doc PR        | symfony/symfony-docs#10422

This leverage the new `AdvancedNameConverterInterface` interface (#27021) to implement a name converter that relies on metadata. The name to use is configured per property using a `@SerializedName` annotation or the `serialized-name` XML attribute or the `serialized_name` key for YAML.

This was exposed by @dunglas in #19374 (comment).

# Framework integration
For FramworkBundle integration, a ChainNameConverter could be added to allow users to use this name converter with a custom one.

# To do

- [x] add a CHANGELOG.md entry.
- [x] add a fallback.
- [x] add framework integration.
- [x] add local caching to `MetadataAwareNameConverter`.
- [x] add a doc PR.

Commits
-------

d1d1ceb [Serialized] allow configuring the serialized name of properties through metadata
acataluddi added a commit to acataluddi/symfony that referenced this issue Dec 14, 2018
@fbourigault
Copy link
Contributor

@javiereguiluz this one could be closed since #28505 is merged and released.

@javiereguiluz
Copy link
Member

Closing as fixed then. Thanks!

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

No branches or pull requests

8 participants