Skip to content

Named serializer get default context from the default serializer #59639

Closed
@adri

Description

@adri

Symfony version(s) affected

7.2.x

Description

Hi! It looks like the default context is not used for some normalizers, instead the "default context" of the default serializer is used.

How to reproduce

    serializer:
        default_context:
            preserve_empty_objects: true

        named_serializers:
            my:
                default_context:
                    datetime_format: U
                    datetime_cast: int
                    yaml_indent: 1

The resulting container sets the preserve_empty_objects setting on the my version of the serializer.normalizer.object service. I'd expect it to use datetime_format etc. instead.

var/cache/dev/App_KernelDevDebugContainer.xml

+    <service id="serializer.normalizer.object.my" class="Symfony\Component\Serializer\Normalizer\ObjectNormalizer">
      <argument type="service" id="serializer.mapping.class_metadata_factory"/>
      <argument type="service" id="serializer.name_converter.metadata_aware"/>
      <argument type="service" id="property_accessor"/>
      <argument type="service" id="property_info" on-invalid="ignore"/>
      <argument type="service" id="serializer.mapping.class_discriminator_resolver" on-invalid="ignore"/>
      <argument>null</argument>
      <argument type="collection">
+        <argument key="preserve_empty_objects">true</argument>
      </argument>
      <argument type="service" id="property_info" on-invalid="ignore"/>
    </service>

Possible Solution

No response

Additional Context

SerializerPass seems to correctly set the binding in bindDefaultContext. But something must change/overwrite it later.
Image

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