Skip to content

Dasherize field names. #754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ro70 opened this issue Dec 15, 2019 · 2 comments
Closed

Dasherize field names. #754

ro70 opened this issue Dec 15, 2019 · 2 comments

Comments

@ro70
Copy link
Contributor

ro70 commented Dec 15, 2019

I use the settings

JSON_API_FORMAT_KEYS = 'dasherize'
JSON_API_FORMAT_FIELD_NAMES = 'dasherize'
JSON_API_FORMAT_TYPES = 'dasherize'
JSON_API_PLURALIZE_TYPES = True

With DJA 2.8.0 all keys are converted according to the settings.

{
  "data": {
    "type": "users",
    "id": "1,
    "attributes": {
      "permissions": {
        "update-first-name": false,
        ...
      },
      "first-name": "a",
      ...
    },
    ...
}

With DJA 3.0.0 this does not happen any more (not for nested keys?). Is that intended?

{
  "data": {
    "type": "users",
    "id": "1",
    "attributes": {
      "permissions": {
        "update_first_name": false,  // <=======
        ...
      },
      "first-name": "a",
      ...
    },
    ...
  }
}
@sliverc
Copy link
Member

sliverc commented Dec 16, 2019

Yes this is intended. JSON_API_FORMAT_KEYS actually got deprecated in version 2.5.0 which had this behavior and got removed in the new major release 3.0.0. Only option JSON_API_FORMAT_FIELD_NAMES now exists which only formats the field names.

For better understanding:
In the json api specification there are no nested fields/keys, but only an attribute field name and its value. And a value if desired can be any valid json.

So before when DJA formatted all nested keys it actually adjusted the value. This led to invalid data see #313 and #420 for more details - basically this was a bug and not intended by the specification.

So hence JSON_API_FORMAT_FIELD_NAMES only formats the field names but not the keys of a value. If you still want to format the nested keys in a JSONField or DictField you would need to adjust your serializer.

Hope this explains?

@ro70
Copy link
Contributor Author

ro70 commented Dec 17, 2019

@sliverc The description in #313 makes totally sense. I will adjust my serializer for the permissions dict to have dasherized keys. Thanks for your reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants