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

fix(FormErrorHandler): add method to FormInterface #1430

Closed titoko closed 2 years ago

titoko commented 2 years ago
Q A
Bug fix? yes
New feature? no
Doc updated no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #...
License MIT

Since Symfony 6.1 FormErrorIterator property $form is typehint to FormInterface since https://github.com/symfony/symfony/commit/85065e4f76eb0e75163277498bbc4fadb3ae49f8 .

This PR https://github.com/schmittjoh/serializer/pull/1362 add the possibility to serialize Interface but lead to the following error call_user_func_array(): Argument #1 ($callback) must be a valid callback, class JMS\Serializer\Handler\FormErrorHandler does not have a method "serializeFormInterfaceTojson" ( see also https://github.com/FriendsOfSymfony/FOSRestBundle/pull/2371 )

Adding the method option fix this issue

scyzoryck commented 2 years ago

Hello! Thanks for the contribution! Would it be possible to add some test cases for it, please? Just to make sure that it works for older & new versions.

Best, Marcin.

titoko commented 2 years ago

Well, don't know if the test is really relevant but at least it should avoid regression :) . Tell me if it's enough for you

scyzoryck commented 2 years ago

I think it would be better to check if it is callable - to make sure that it is not dependent on function name :) What do you think about something like:

public function testSerializeFormInterface()
    {
        $registry = new HandlerRegistry([]);
        $registry->registerSubscribingHandler($this->handler);
        $callback = $registry->getHandler(GraphNavigatorInterface::DIRECTION_SERIALIZATION, FormInterface::class, 'json');
        $serialised = $callback($this->visitor, $this->createForm(), []);
        // ...assert result? :) 
    }

It looks like it fails with similar result:

Error: Call to undefined method JMS\Serializer\Handler\FormErrorHandler::serializeFormInterfaceTojson()
goetas commented 2 years ago

done in https://github.com/schmittjoh/serializer/pull/1432