Skip to content

Don't force translation of request attribute names #284

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Jerel Unruh <[email protected]>
Greg Aker <[email protected]>
Adam Wróbel <https://adamwrobel.com>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I totally understand not wanting to put your email address. I don't like doing that either for the same reason you described. Thanks for adding your name.


6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
v2.3.0

* When `JSON_API_FORMAT_KEYS` is False (the default) do not translate request
attributes and relations to snake\_case format. This conversion was unexpected
and there was no way to turn it off.

v2.2.0

* Add support for Django REST Framework 3.5 and 3.6
Expand Down
23 changes: 20 additions & 3 deletions rest_framework_json_api/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from rest_framework import parsers
from rest_framework.exceptions import ParseError

from django.conf import settings
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this import line above the rest_framework lines without a newline between them? This project groups third party libraries together alphabetically.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the style is a bit of a mess because it's not consistent. Don't worry about it. I created issue #344 after I left this comment above so it can be cleaned up in the future. I'll get this line later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend using isort for this kind of churn. Give it a try.


from . import utils, renderers, exceptions


Expand All @@ -29,12 +31,27 @@ class JSONParser(parsers.JSONParser):

@staticmethod
def parse_attributes(data):
return utils.format_keys(data.get('attributes'), 'underscore') if data.get('attributes') else dict()
attributes = data.get('attributes')
uses_format_translation = getattr(settings, 'JSON_API_FORMAT_KEYS', False)

if not attributes:
return dict()
elif uses_format_translation:
# convert back to python/rest_framework's preferred underscore format
return utils.format_keys(attributes, 'underscore')
else:
return attributes

@staticmethod
def parse_relationships(data):
relationships = (utils.format_keys(data.get('relationships'), 'underscore')
if data.get('relationships') else dict())
uses_format_translation = getattr(settings, 'JSON_API_FORMAT_KEYS', False)
relationships = data.get('relationships')

if not relationships:
relationships = dict()
elif uses_format_translation:
# convert back to python/rest_framework's preferred underscore format
relationships = utils.format_keys(relationships, 'underscore')

# Parse the relationships
parsed_relationships = dict()
Expand Down