Skip to content

Collection response + snake case / camelCase issue #17

Open
@guiguiboy

Description

@guiguiboy

Hello,

First of all, thanik you for your work.
I am currently using your bundle for my REST API.
I am currently facing an issue with 2 routes that exports the same kind of objects (user objects):

  • a GET on a list (collection of users)
  • a GET on a single user

For the list route, I use the following mapping file :

mapping:
  class: \Doctrine\Common\Collections\ArrayCollection
  alias: Collection
  hide_properties: []
  urls:
    self: admin_get_users ## @Route name

In my controller, I retrieve a list of users which I output as you described in your doc. In the response, I have :

{
  "data": {
    "type": "collection",
    "id": "",
    "attributes": [],
    "relationships": {
      "elements": [
        {
          "data": {
            "type": "user",
            "id": "1"
          }
        },
        {
          "data": {
            "type": "user",
            "id": "2"
          }
        },
        {
          "data": {
            "type": "user",
            "id": "3"
          }
        }
      ]
    }
  },
  "included": [
    {
      "type": "user",
      "id": "1",
      "attributes": {
        "username": "gbu",
        "email": "[email protected]",
        "initials": null,
        "firstName": "....",
        "surnamePrefix": null,
        "surname": "....",
        "enabled": true,
        "locked": false,
        "lastLogin": null,
        "roles": [
          "ROLE_ADMIN",
          "ROLE_USER"
        ]
      }
    },
...

I was expecting to retrieve the user objects direcly in the "data" node but it's available in the "included" node.
What configuration do I need to use to have such output ?

I also noticed another thing concerning the keys attributes. As you can see above, the attributes names in the "included" section are in camelcase (firstName, lastLogin, ...).

But when I execute the serializer on my route to retrieve a single instance of User, I get :

{
  "data": {
    "type": "user",
    "id": "1",
    "attributes": {
      "username": "gbu",
      "email": "[email protected]",
      "initials": null,
      "first_name": "...",
      "surname_prefix": null,
      "surname": "...",
      "enabled": true,
      "locked": false,
      "last_login": null
    }
  },
  "links": {
    "self": {
      "href": "/app_dev.php/admin/v1/user/1?access_token=ZmI4Nzc5YTlmOTk2Mjg3MDg4NzEwODY4N2NlNjEyOTViMTA4MTYyMjViZjAxZDY5ZWQ1Yzg0YjRmYmJjN2RhMf"
    }
  },
  "jsonapi": {
    "version": "1.0"
  }
}

As you can see, the names are transformed in snake case.
it is due to the call in JsonAPiTransformer::serialization.
It calls DataAttributesHelper::setResponseDataAttributes($this->mappings, $value) which in turn calls the method transformToValidMemberName.

But could this behavior be configurable ? Because my parser must now be aware of the snake case and the camelCase to construct my user object.

Thanks for your help.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions