Skip to content

Commit b7844e9

Browse files
committed
Don't force translation of request attribute names.
This attribute key translation was advertised to be "off by default", but was in fact forced in parser. This commit makes it so that request attributes and relations are only translated back to snake_case format if JSON_API_FORMAT_KEYS setting is set.
1 parent cdb8a52 commit b7844e9

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Jerel Unruh <[email protected]>
22
Greg Aker <[email protected]>
3+
Adam Wróbel <https://adamwrobel.com>
34

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
v2.3.0
2+
3+
* When `JSON_API_FORMAT_KEYS` is False (the default) do not translate request
4+
attributes and relations to snake\_case format. This conversion was unexpected
5+
and there was no way to turn it off.
6+
17
v2.2.0
28

39
* Add support for Django REST Framework 3.5 and 3.6

rest_framework_json_api/parsers.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from rest_framework import parsers
55
from rest_framework.exceptions import ParseError
66

7+
from django.conf import settings
8+
79
from . import utils, renderers, exceptions
810

911

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

3032
@staticmethod
3133
def parse_attributes(data):
32-
return utils.format_keys(data.get('attributes'), 'underscore') if data.get('attributes') else dict()
34+
attributes = data.get('attributes')
35+
uses_format_translation = getattr(settings, 'JSON_API_FORMAT_KEYS', False)
36+
37+
if not attributes:
38+
return dict()
39+
elif uses_format_translation:
40+
# convert back to python/rest_framework's preferred underscore format
41+
return utils.format_keys(attributes, 'underscore')
42+
else:
43+
return attributes
3344

3445
@staticmethod
3546
def parse_relationships(data):
36-
relationships = (utils.format_keys(data.get('relationships'), 'underscore')
37-
if data.get('relationships') else dict())
47+
uses_format_translation = getattr(settings, 'JSON_API_FORMAT_KEYS', False)
48+
relationships = data.get('relationships')
49+
50+
if not relationships:
51+
relationships = dict()
52+
elif uses_format_translation:
53+
# convert back to python/rest_framework's preferred underscore format
54+
relationships = utils.format_keys(relationships, 'underscore')
3855

3956
# Parse the relationships
4057
parsed_relationships = dict()

0 commit comments

Comments
 (0)