schmittjoh / JMSSerializerBundle

Easily serialize, and deserialize data of any complexity (supports XML, JSON, YAML)
http://jmsyst.com/bundles/JMSSerializerBundle
MIT License
1.8k stars 312 forks source link

I Can't configure exclusion groups in Symfony 5 #785

Open fjugaldev opened 4 years ago

fjugaldev commented 4 years ago

Hi guys..

I'm trying to add some excluding groups and i can't make work this configuration.

I'm using symfony 5 and this is my config:

#config/packages/jms_serializer.yaml
jms_serializer:
    visitors:
        xml_serialization:
            format_output: '%kernel.debug%'
        json_deserialization:
            options:
                - JSON_HEX_AMP
                - JSON_HEX_APOS
                - JSON_HEX_QUOT
                - JSON_HEX_TAG
    metadata:
        auto_detection: false
        directories:
            InnovatikLabs:
                namespace_prefix: "InnovatikLabs\\Shared\\Domain\\Model"
                path: "%kernel.project_dir%/src/Shared/Infrastructure/Serializer"
#src/Shared/Infrastructure/Serializer/Address.yml
InnovatikLabs\Shared\Domain\Model\Address:
    exclusion_policy: ALL
    properties:
        id:
            expose: true
            groups: [detail]
        street:
            expose: true
            groups: [detail]
        postalCode:
            expose: true
            groups: [detail]
        phone:
            expose: true
            groups: [detail]

My Model is in InnovatikLabs\Shared\Domain\Model\Address and the mapping is made using xml.

When i execute a doctrine find in my controller i get an Address object and when i return a JsonResponse like this:

$em = $this->getDoctrine()->getManager();
$address = $em->getRepository(Address::class)->find('298a2773-ab9f-4225-877d-1fe80acf60b5');

return JsonResponse::create(
    $serializer->serialize($address, 'json', SerializationContext::create()->setGroups(['detail']))
);

The response is empty.

I think i'm missing something or i guess the bundle is no reading my config in order to get the excluding groups from the yaml's.

Note: If i use the method addMetadataDir like this:

$serializer = SerializerBuilder::create()
    ->addMetadataDir('/var/www/html/src/Shared/Infrastructure/Serializer')
    ->build();

And renaming the .yml containing the serialization groups, in order to include all the namespace like InnovatikLabs.Shared.Domain.Model.Address.yml, it works! but i need an automated way to declare all my config and serialization groups and only stay focus in serializa and deserialize objects.

Can anybody help me please xD

goetas commented 4 years ago

If you are using, SerializerBuilder::create(), you are completely bypassing all the symfony handling and getting a plain serializer. You should use the symfony dependency injection and get the instance from there.

fjugaldev commented 4 years ago

Hi @goetas thank your answer so what i need to do is take advance of the autowiring and set as parameter of the method.

I tried too access to the serializer via the container service but i notice that it’s no anymore declared as a service.. I can define too in the services.yml? If is possible will work just like the autowiring dependencies?

Thank you bro for helping me..

Regards!!

Francisco

goetas commented 4 years ago

autowiring should do the job. If you typehing your controller/service with Jms\Serialier\SerializerInterface, DI should do the Job for you.