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

SF4: JMS serializer seems to be ignoring global naming strategy #1037

Closed
signmeuptwice opened this issue Feb 3, 2019 · 10 comments
Closed

SF4: JMS serializer seems to be ignoring global naming strategy #1037

signmeuptwice opened this issue Feb 3, 2019 · 10 comments

Comments

@signmeuptwice
Copy link

signmeuptwice commented Feb 3, 2019

symfony 4.2
"jms/serializer-bundle": "^3.1",

using dependency injecton in my controller function with SerializerInterface $serializer

in my config > packages > jms_serializer.yaml I have the following code but it has not effect whatsoever. Is this normal ? How do we override the default _ naming strategy to identical ?

I am NOT using any other settings anywhere. I literally just installed the bundle and try to configure it.

jms_serializer:
  visitors:
    xml_serialization:
      format_output: '%kernel.debug%'
  property_naming:
     id: 'jms_serializer.identical_property_naming_strategy'
@goetas
Copy link
Collaborator

goetas commented Feb 4, 2019

I tested:

composer create-project symfony/skeleton blog
cd blog
composer require jms/serializer-bundle

Edit config/packages/jms_serializer.yaml with :

jms_serializer:
    property_naming:
        id: jms_serializer.identical_property_naming_strategy
    visitors:
        xml_serialization:
            format_output: '%kernel.debug%'

and the naming strategy was applied as expected.

@goetas goetas closed this as completed Feb 4, 2019
@goetas
Copy link
Collaborator

goetas commented Feb 4, 2019

Please provide more info to test your issue (as example a skeleton project on a github repo)

@signmeuptwice
Copy link
Author

signmeuptwice commented Feb 4, 2019

Hi goetas;

Thanks for the reply
this is a big project; I am not sure how I could provide a "skeleton project" other than what you tested already.. I tried again and I still cannot get it to work. I have the exact same settings as you posted above and I flushed my cache multiple times.

a specificity maybe not mentionned is that I am Using AbstractController since Controller is deprecated in SF4

here is my non working instantiation:

use JMS\Serializer\SerializerInterface;

    /**
     * @param SerializerInterface $serializer
     */

    public function searchBarLookup (SerializerInterface $serializer) {
     ....
      $data = $serializer->serialize($list, 'json');
                return $this->json($data);
      }

It works if I instantiate a new serializer only (like the following) but as a global setting I have no luck at all.

$serializer = \JMS\Serializer\SerializerBuilder::create()
                    ->setPropertyNamingStrategy(
                        new SerializedNameAnnotationStrategy(
                            new IdenticalPropertyNamingStrategy()
                        )
                    )
                    ->build();

If I intentionally put a bad character in jms_serializer.identical_property_naming_strategy of the yaml file then it crashes so I guess Symfony is reading the file and "loading" the setting.

Is it possible that the global setting gets overridden somewhere down the line ?

@danilphd
Copy link

danilphd commented Aug 9, 2019

I have the same issue. Symfony 4.3.2.

@goetas
Copy link
Collaborator

goetas commented Aug 9, 2019

If you are using the bundle, consider schmittjoh/JMSSerializerBundle#767

@marcbln
Copy link

marcbln commented Sep 9, 2020

I had to manually delete the cache (var/cache/dev|prod) to get the new naming strategy working. bin/console cache:clear was not enough.

@99hops
Copy link

99hops commented Apr 17, 2022

I think this is recurring jms/serializer-bundle 4.0.1 and Symfony 6.0.7

jms_serializer:
    default_context:
        serialization:
            serialize_null: true
    visitors:
        json_serialization:
            options:
                - JSON_UNESCAPED_SLASHES
                - JSON_PRESERVE_ZERO_FRACTION
    property_naming:
        id: jms_serializer.identical_property_naming_strategy
    handlers:
        datetime:
            default_format: "Y-m-d\\TH:i:sP" # ATOM
            default_timezone: "UTC"

This works

public function __construct() {
        $this->serializer = SerializerBuilder::create()
            ->setPropertyNamingStrategy(
                new SerializedNameAnnotationStrategy(
                    new IdenticalPropertyNamingStrategy()
                )
            )
            ->setExpressionEvaluator(new ExpressionEvaluator(new ExpressionLanguage()))
            ->build();
    }

This works

public function __construct(SerializerInterface $serializer) {
        $this->serializer = $serializer;
    }

This does not work like settings in yaml are ignored

$this->serializer = SerializerBuilder::create()
            ->setExpressionEvaluator(new ExpressionEvaluator(new ExpressionLanguage()))
            ->build();

Could be I am doing it wrong but would one have expressions enabled with respect to jms_serializer.yaml ?

Just a side note:
I have the same issue with setSerializeNull not respected from default_context

$context = SerializationContext::create()->setGroups($groups)->setSerializeNull(true);
$this->serializer->serialize($x);

@goetas
Copy link
Collaborator

goetas commented Apr 19, 2022

SerializerBuilder and SerializationContext build the context and the serializer instances by ignoring any symfony configuration. The should never be used when using the serializer bundle integration.

@HolgerBanse
Copy link

I was pointed to this by google because I am trying to find out how to set groups when calling serialize() without the need to programmatically set the options I already have configured in the bundle.
Is that possible? From what I found, the only option is to create a new SerializationContext to set the groups, which has the effect of losing the default settings from serializer.yaml

@sandyCooks
Copy link

sandyCooks commented Feb 8, 2024

If i autowire the SerializerInterface in Symfony 6.4 the settings from the yaml don't seem to be respected.
For me only creating this works:

        $this->serializer = SerializerBuilder::create()
            ->setPropertyNamingStrategy(
                new SerializedNameAnnotationStrategy(
                    new IdenticalPropertyNamingStrategy()
                )
            )
            ->setExpressionEvaluator(new ExpressionEvaluator(new ExpressionLanguage()))
            ->build();
    }

Would appreciate it if somebody could point me to a solution here. Don't like that constructor-thingy very much. Though I really do like the Serializer otherwise!

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

7 participants