Skip to content

React controller issue with serializer and groups #50045

Closed
@pomporov

Description

@pomporov

Symfony version(s) affected

6.2.*

Description

An issue occurs in


           {{ react_component('LoginComponent',{
                site: site|serialize('json', { groups: 'site:read'})
            }) }}>


it does not work as serialize return a string {'name':'value'} and it get escaped again in react_controller.
this lead to error when using it in react , site.name for example.

this work fine:


           {{ react_component('LoginComponent',{
                site: {'name':'value'}
            }) }}>

because its an array and it get not escaped twice .

How to reproduce

           {{ react_component('LoginComponent',{
                site: site|serialize('json', { groups: 'site:read'})
            }) }}>

Possible Solution

            {{ react_component('LoginComponent',{
                site: site|serialize('json', { groups: 'site:read'})|json_decode
            }) }}

the json_decode si a custom twig filter that turn the json string back to array .

class JsonDecode extends AbstractExtension
{
    public function getFilters()
    {
        return [
            new TwigFilter('json_decode', [$this, 'jsonDecode']),
        ];
    }


    public function jsonDecode($string)
    {
        return json_decode($string);
    }
}

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions