FOSRestBundle camel keys normalizer not working
Asked Answered
A

2

6

This is my config file:

// app/config/config.yml
fos_rest:
    body_listener:
        array_normalizer: fos_rest.normalizer.camel_keys

I am using the latest version of FOSRestBundle:

// composer.json
"friendsofsymfony/rest-bundle": "dev-master"

These are my post parameters:

// Post parameters
"first_name": "First name",
"last_name": "Last name",
"phone": "Phone"

This is my controller:

/**
 * @ApiDoc(
 *      resource=true,
 *      description="Create a new user"
 * )
 *
 * @View()
 */
public function postAction(Request $request)
{
    $user = new User();
    $form = $this->createForm(new UserType(), $user);

    // Request post parameters are not camel cased
    // Parameters expected: firstName, lastName, phone
    // Parameters got: first_name, last_name, phone
    $form->handleRequest($request);

    if ($form->isValid()) {
        return 'valid';
    }

    return $user;
}

The problem:

The request post parameters are not camel cased. I am getting first_name, last_name, phone instead of firstName, lastName, phone. I did the same as https://florian.voutzinos.com/blog/handling-camelcase-with-fosrestbundle/. But, it didn't work. Am I missing something? Any ideas? Thanks.

Administrator answered 11/9, 2014 at 17:53 Comment(0)
J
6

That's the JMSSerializer renaming your field names. Try to change it's naming strategy. Put this into your service configuration file

XML

<services>
  <service id="jms_serializer.naming_strategy" class="JMS\Serializer\Naming\IdenticalPropertyNamingStrategy" />
</services>

YML

services:
  jms_serializer.naming_strategy:
    class: 'JMS\Serializer\Naming\IdenticalPropertyNamingStrategy'
Jingoism answered 23/10, 2014 at 12:48 Comment(0)
M
1

Is there a typo in your config? You've missed four spaces before array_normalizer cause it's a property of body_listener

fos_rest:
    body_listener:
        array_normalizer: fos_rest.normalizer.camel_keys
Mcardle answered 16/9, 2014 at 21:9 Comment(2)
It is not required. Tried, the same thing. Anyway, thanks for your answer.Administrator
This is the correct one, no need to change JMS Serializer configuration in services.yml!Curator

© 2022 - 2024 — McMap. All rights reserved.