schmittjoh / serializer

Library for (de-)serializing data of any complexity (supports JSON, and XML)
http://jmsyst.com/libs/serializer
MIT License
2.32k stars 585 forks source link

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

Closed signmeuptwice closed 5 years ago

signmeuptwice commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

signmeuptwice commented 5 years ago

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 commented 5 years ago

I have the same issue. Symfony 4.3.2.

goetas commented 5 years ago

If you are using the bundle, consider https://github.com/schmittjoh/JMSSerializerBundle/pull/767

marcbln commented 4 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 1 year ago

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 commented 8 months ago

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!