1
votes

I'm working on creating a custom (de)normalizer to handle entities. I have created the normalizer and allowed the service container to autowire/autoconfig. The service is selected correctly during deserialization, but I'm having trouble with the name converter. I want to use the MetadataAwareNameConverter service since I'm using the @SerializedName annotation in my entity. No matter what I do, it is always null in the custom normalizer. I have tried a number of methods of getting the name converter service:

  1. Setting it explicitly in my class constructor
  2. Setting it in the service definition (effectively getting rid of autowire/autoconfig)
  3. Setting MetadataAwareNameConverter as the default in framework.yaml (I discovered it is the default already).
  4. Copied an existing normalizer into my src and renamed it to see if it got the correct name converter (it still didn't work)

Built in normalizers are getting a name converter without issue, it is just my custom normalizer that is having this issue.

Is there anything else I should try? Am I missing a step in setting up my service? Any direction is appreciated.

UPDATE - when I dump the service container, the name converter service is missing from the arguments list

---------------- ----------------------------------------------------------
  Option           Value
 ---------------- ----------------------------------------------------------
  Service ID       App\Normalizer\QNormalizer
  Class            App\Normalizer\QNormalizer
  Tags             serializer.normalizer
  Public           no
  Synthetic        no
  Lazy             no
  Shared           yes
  Abstract         no
  Autowired        yes
  Autoconfigured   yes
  Arguments        Service(serializer.mapping.class_metadata_factory)
                   -----THIS IS WHERE THE NAME CONVERTER SHOULD BE----
                   Service(property_accessor)
                   Service(property_info)
                   Service(serializer.mapping.class_discriminator_resolver)
2

2 Answers

0
votes

Manually injecting MetadataAwareNameConverter in services.yaml solved problem for me.

App\Serializer\CustomNormalizer:
    arguments:
        $nameConverter: '@serializer.name_converter.metadata_aware'
0
votes

I faced same issue.

In my case it was a missconfiguration of services happened because of framework was configured to autoconfigure services (It's default framework configuration).

In result I had my custom normalizer duplicated in list of services. First one is autoconfigured without priority Second one is declared by me and having name converter injected:

Service Id Priority Class Name
App\Adapter\Symfony\Serializer\Normalizer\TranslationNormalizer App\Adapter\ApiPlatform\Serializer\Normalizer\ItemNormalizer
api_platform.serializer.normalizer.item -895 App\Adapter\ApiPlatform\Serializer\Normalizer\ItemNormalizer

Declaration:

api_platform.serializer.normalizer.item:
        class: App\Adapter\ApiPlatform\Serializer\Normalizer\ItemNormalizer
        arguments:
            $nameConverter: '@serializer.name_converter.metadata_aware'
        autoconfigure: false
        tags:
            - {name: serializer.normalizer, priority: -895}

Since autoconfigured normalizer have higher priority in list - it was picked by serializer so my SerializedName annotation wasn't working.

Solution is to disable autoconfiguration for first on service:

App\Adapter\ApiPlatform\Serializer\Normalizer\ItemNormalizer:
    autoconfigure: false