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

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

Open
fjugaldev opened this issue Jan 6, 2020 · 3 comments
Open

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

fjugaldev opened this issue Jan 6, 2020 · 3 comments

Comments

@fjugaldev
Copy link

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
Copy link
Collaborator

goetas commented Jan 7, 2020

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
Copy link
Author

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
Copy link
Collaborator

goetas commented Jan 8, 2020

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

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

2 participants