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

Using jms_serializer.cache_naming_strategy.class overrides @SerializedName annotation #397

Closed
johnpez opened this issue Jul 1, 2014 · 7 comments

Comments

@johnpez
Copy link

johnpez commented Jul 1, 2014

I'm using jms_serializer.cache_naming_strategy.class: JMS\Serializer\Naming\IdenticalPropertyNamingStrategy to use camel case instead of underscores, but when I have this activated my SerializedName annotation directives are ignored by the serializer.

Is this a bug or is there a workaround?

Thanks!

@inverse
Copy link

inverse commented Jul 15, 2014

Any update on this?

@johnpez
Copy link
Author

johnpez commented Jul 18, 2014

The problem persists and is very easy to recreate, simply add jms_serializer.cache_naming_strategy.class: JMS\Serializer\Naming\IdenticalPropertyNamingStrategy to your parameters and try to use the SerializedName property

@inverse
Copy link

inverse commented Jul 19, 2014

This is what we ended up doing in the end. I guess it means that everything is clearly marked up in annotations now but it feels counter intuitive IMHO.

@wesnick
Copy link

wesnick commented Aug 8, 2014

It is easy to implement this. I just copied the SerializedNameAnnotationStrategy class:

  • Create a custom naming strategy class:
namepsace Acme\ApiBundle\Serializer;

use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
use JMS\Serializer\Naming\PropertyNamingStrategyInterface;

class ApiNamingStrategy implements PropertyNamingStrategyInterface
{
    private $delegate;

    public function __construct()
    {
        $this->delegate = new IdenticalPropertyNamingStrategy();
    }

    /**
     * {@inheritDoc}
     */
    public function translateName(PropertyMetadata $property)
    {
        if (null !== $name = $property->serializedName) {
            return $name;
        }

        return $this->delegate->translateName($property);
    }
}
  • Add this class to your config.yml
services:
    jms_serializer.cache_naming_strategy:
        class: Acme\ApiBundle\Serializer\ApiNamingStrategy

@johnpez
Copy link
Author

johnpez commented Aug 12, 2014

Thank you @wesnick, can confirm, works great~

@maff
Copy link
Contributor

maff commented Apr 2, 2015

See #445 for a solution without adding a new strategy.

@pixels-farm
Copy link

Brilliant! Great job, @wesnick !

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

No branches or pull requests

5 participants