How can I expose a customers phone number?

I want the Customer-API to send me the customers phone number as well.

  1. Create config/packages/jms_serializer.yaml if it doesn’t exists.
  2. Add the metadata mapping for your entity namespace
jms_serializer:
    metadata:
        directories:
            app:
                namespace_prefix: "App\\Entity"
                path: "%kernel.project_dir%/config/serializer"
  1. Add the metadata config for your Customer entity in config/serializer/Entity.Customer.yaml
App\Entity\Customer:
 exclusion_policy: ALL
 properties:
   phoneNumber:
     expose: true
     groups: [Default, Detailed]
1 Like

Thanks for your reply!

I followed your steps but the response still doesn’t contain the phone number.

Your Customer entity is probably under another namespace.
Change Èntity.Customer.yaml to Entity.Customer.Customer.yaml and the namespace inside it to App\Entity\Customer\Customer.

Also, change the extension from yaml to yml, looks like JMS Serializer doesn’t work with the first one.

This doesn’t work either.

I think I have to define which JMS Serializer should be used but I don’t know where.

Ok, I’ve figured out a solution:

  • config/packages/jms_serializer.yaml:
jms_serializer:
    visitors:
        xml:
            format_output: '%kernel.debug%'
    metadata:
        directories:
            sylius-customer:
                namespace_prefix: 'Sylius\Component\Customer'
                path: '%kernel.root_dir%/serializer'
  • src/serializer/Model.Customer.yml:
Sylius\Component\Customer\Model\Customer:
    exclusion_policy: ALL
    xml_root_name: sylius_customer
    properties:
        id:
            expose: true
            type: integer
            groups: [Default, Detailed, DetailedCart]
        email:
            expose: true
            type: string
            groups: [Default, Detailed, DetailedCart]
        emailCanonical:
            expose: true
            type: string
            groups: [Detailed]
        firstName:
            expose: true
            type: string
            groups: [Default, Detailed, DetailedCart]
        lastName:
            expose: true
            type: string
            groups: [Default, Detailed, DetailedCart]
        gender:
            expose: true
            type: string
            groups: [Detailed]
        birthday:
            expose: true
            type: DateTime
            groups: [Detailed]
        group:
            expose: true
            groups: [Detailed]
        phoneNumber:
            expose: true
            groups: [Default, Detailed, DetailedCart]

I forgot it works per class only and you cannot configure inherited properties.
Also, the file name should have been Customer.Customer.yaml, without the Entity since it’s already in the prefix.
I suggest to separate vendor metadata from app.
Example:

jms_serializer:
    metadata:
        directories:
            app:
                namespace_prefix: "App\\Entity"
                path: "%kernel.project_dir%/config/serializer/app"
            sylius-customer:
                namespace_prefix: "Sylius\\Component\Customer"
                path: "%kernel.project_dir%/config/serializer/sylius/customer"
            # or for all at once
            # sylius-components:
            #     namespace_prefix: "Sylius\\Component"
            #     path: "%kernel.project_dir%/config/serializer/sylius"
            # in that case the metadata file name will be Customer.Model.Customer.yml

Anyway, just overriding the $phoneNumber property in App\Entity\Customer\Customer should work too.
Configuring it as a virtual property might also work, will try and get back with the result.

It works with virtual property too:

App\Entity\Customer\Customer:
    exclusion_policy: ALL
    virtual_properties:
        getPhoneNumber:
            serialized_name: phoneNumber
            expose: true
            groups: [Default, Detailed]