JMSSerializerBundle icon indicating copy to clipboard operation
JMSSerializerBundle copied to clipboard

Point out aliasing object_constructor to doctrine_object_constructor not necessary

Open drola opened this issue 5 years ago • 8 comments

After upgrading to the newest release of JMSSerializerBundle, Symfony started throwing

ServiceCircularReferenceException

Circular reference detected for service "jms_serializer.doctrine_object_constructor", path: "jms_serializer.doctrine_object_constructor -> jms_serializer.doctrine_object_constructor"

Upon investigation, I found out this was part of my configuration:

services:
    jms_serializer.object_constructor:
        alias: jms_serializer.doctrine_object_constructor
        public: false

It appears this snippet was necessary before introduction of DependencyInjection/Compiler/DoctrinePass.php. However, if I understand things correctly, it is not necessary to alias object_constructor to doctrine_object_constructor anymore. Is this correct? If so, it would be nice to update documentation in relation to this.

drola avatar Jul 07 '20 10:07 drola

I am not able to configure a custom object constructor anymore that uses "jms_serializer.doctrine_object_constructor" as fallback (injected in the constructor).

This also throws a "ServiceCircularReferenceException".

hasc avatar Jul 08 '20 11:07 hasc

I think that this is a unintentional BC break introduced with https://github.com/schmittjoh/JMSSerializerBundle/pull/803

To solve your issue you should use symfony decorators (https://symfony.com/doc/current/service_container/service_decoration.html) and decorate the jms_serializer.object_constructor service.

goetas avatar Jul 09 '20 07:07 goetas

Would be great if you could add a note on the documentation mentioning this :)

goetas avatar Jul 09 '20 07:07 goetas

think that this is a unintentional BC break introduced with #803

To solve your issue you should use symfony decorators (https://symfony.com/doc/current/service_container/service_decoration.html) and decorate the jms_serializer.object_constructor service.

I am struggling with this. I'd like to configure a custom constructor that uses the doctrine constructor as fallback. Would be great if you could show me how this can be achieved.

hasc avatar Aug 04 '20 11:08 hasc

The change made in #803 was actually BC breaking in my case: the bundle started using a different object constructor by default, breaking my implementation.

bobvandevijver avatar Oct 30 '20 16:10 bobvandevijver

@bobvandevijver i'm sorry for that...

goetas avatar Oct 31 '20 09:10 goetas

had similar problem since we upgraded from

"jms/serializer": "3.7.0",
"jms/serializer-bundle": "3.6"

to

"jms/serializer": "3.10.0",
"jms/serializer-bundle": "3.7"

fastmanu avatar Nov 17 '20 15:11 fastmanu

Hello,

Unfortunately, we faced the same BC. It would be nice to update the documentation at https://jmsyst.com/bundles/JMSSerializerBundle because many of the new features introduced in lastest versions are not referenced there.

As described by @goetas, we fixed our implementation using the decoration api.

services:
    jms_serializer.object_constructor:
        stack:
            - App\Service\CustomObjectConstructor: ['@doctrine', '@.inner']
            - JMS\Serializer\Construction\UnserializeObjectConstructor: ~

However, upon investigation, we found that the default constructor is now JMS\Serializer\Construction\DoctrineObjectConstructor when an ORM is injected into the config.

Our implementation is an override of the doctrine constructor, so we additionally disabled the default behavior to only use the fallback instance constructor UnserializeObjectConstructor.

jms_serializer:
    object_constructors:
        doctrine:
            enabled: false

Enjoy 🎉

DaedalusDev avatar Jan 19 '23 10:01 DaedalusDev