schmittjoh / JMSSerializerBundle

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

Version 5.3.0 does not work with annotations anymore #931

Closed sylviavdv closed 1 year ago

sylviavdv commented 1 year ago
Q A
Bug report? yes
Feature request? no
BC Break report? yes
RFC? no

Steps required to reproduce the problem

  1. update from 5.2.1 to 5.3.0
  2. have objects that use jms annotations

Expected Result

Actual Result

In DependencyInjection/JMSSerializerExtension.php, a new driver is added, the attribute_driver, which has priority over the annotation driver.

                $metadataDrivers = [
                    new Reference('jms_serializer.metadata.yaml_driver'),
                    new Reference('jms_serializer.metadata.xml_driver'),
                    new Reference('jms_serializer.metadata.attribute_driver'),
                    new Reference('jms_serializer.metadata.annotation_driver'),
                ]; 

Now in vendor/jms/metadata/src/Driver/DriverChain.php, the annotation driver always produces a metadata object, even though no jms annotations are used. The result object has no metadata, but it's not null.

JMS\Serializer\Metadata\ClassMetadata {#7855
  +name: "stdClass"
  +methodMetadata: []
  +propertyMetadata: []
  +fileResources: []
  +createdAt: 1686566431
  +preSerializeMethods: []
  +postSerializeMethods: []
  +postDeserializeMethods: []
  +xmlRootName: null
  +xmlRootNamespace: null
  +xmlRootPrefix: null
  +xmlNamespaces: []
  +accessorOrder: null
  +customOrder: null
  +usingExpression: false
  +isList: false
  +isMap: false
  +discriminatorDisabled: false
  +discriminatorBaseClass: null
  +discriminatorFieldName: null
  +discriminatorValue: null
  +discriminatorMap: []
  +discriminatorGroups: []
  +xmlDiscriminatorAttribute: false
  +xmlDiscriminatorCData: true
  +xmlDiscriminatorNamespace: null
  +excludeIf: null
}

Therefore we never reach the AnnotationDriver and all jms annotations are subsequently ignored.

mbabker commented 1 year ago

I don't have a clean way of testing this since the main app I work in has the jms_serializer.metadata_driver service overloaded to fully remove annotation support (we have them completely disabled), but looking over #920, the one thing I can think to suggest is to flip the order of the drivers here.

But, I get the feeling this is exposing a bigger problem with those drivers if the annotation or attribute drivers are returning unconfigured Metadata\ClassMetadata objects instead of null like the XML or YAML drivers do when there is no metadata, and it's only showing up because of the changes introduced with that PR.

mbabker commented 1 year ago

schmittjoh/serializer#1494 looks to clean up the underlying root cause, if you're able to test that.