Skip to content

Commit ab3dfda

Browse files
committed
Unified camelize behavior and added capitalize.
Helps fix #92 and fix #77
1 parent 28833bf commit ab3dfda

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

example/tests/test_format_keys.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_camelization(self):
3737
expected = {
3838
'data': [
3939
{
40-
'type': 'Users',
40+
'type': 'users',
4141
'id': encoding.force_text(user.pk),
4242
'attributes': {
4343
'firstName': user.first_name,

example/tests/test_utils.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ def test_format_keys():
5050
output = {'firstName': 'a', 'lastName': 'b'}
5151
assert utils.format_keys(underscored, 'camelize') == output
5252

53+
output = {'FirstName': 'a', 'LastName': 'b'}
54+
assert utils.format_keys(underscored, 'capitalize') == output
55+
5356
output = {'first-name': 'a', 'last-name': 'b'}
5457
assert utils.format_keys(underscored, 'dasherize') == output
5558

@@ -60,12 +63,14 @@ def test_format_keys():
6063
assert utils.format_keys([underscored], 'dasherize') == output
6164

6265
def test_format_value():
63-
assert utils.format_value('first_name', 'camelize') == 'FirstName'
66+
assert utils.format_value('first_name', 'camelize') == 'firstName'
67+
assert utils.format_value('first_name', 'capitalize') == 'FirstName'
6468
assert utils.format_value('first_name', 'dasherize') == 'first-name'
6569
assert utils.format_value('first-name', 'underscore') == 'first_name'
6670

6771
def test_format_relation_name():
68-
assert utils.format_relation_name('first_name', 'camelize') == 'FirstNames'
72+
assert utils.format_relation_name('first_name', 'capitalize') == 'FirstNames'
73+
assert utils.format_relation_name('first_name', 'camelize') == 'firstNames'
6974

7075
def test_build_json_resource_obj():
7176
resource = {

rest_framework_json_api/utils.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,22 @@ def format_keys(obj, format_type=None):
9494
if format_type is None:
9595
format_type = getattr(settings, 'JSON_API_FORMAT_KEYS', False)
9696

97-
if format_type in ('dasherize', 'camelize', 'underscore'):
97+
if format_type in ('dasherize', 'camelize', 'underscore', 'capitalize'):
9898

9999
if isinstance(obj, dict):
100100
formatted = OrderedDict()
101101
for key, value in obj.items():
102102
if format_type == 'dasherize':
103+
# inflection can't dasherize camelCase
104+
key = inflection.underscore(key)
103105
formatted[inflection.dasherize(key)] \
104106
= format_keys(value, format_type)
105107
elif format_type == 'camelize':
106108
formatted[inflection.camelize(key, False)] \
107109
= format_keys(value, format_type)
110+
elif format_type == 'capitalize':
111+
formatted[inflection.camelize(key)] \
112+
= format_keys(value, format_type)
108113
elif format_type == 'underscore':
109114
formatted[inflection.underscore(key)] \
110115
= format_keys(value, format_type)
@@ -121,8 +126,12 @@ def format_value(value, format_type=None):
121126
if format_type is None:
122127
format_type = getattr(settings, 'JSON_API_FORMAT_KEYS', False)
123128
if format_type == 'dasherize':
129+
# inflection can't dasherize camelCase
130+
value = inflection.underscore(value)
124131
value = inflection.dasherize(value)
125132
elif format_type == 'camelize':
133+
value = inflection.camelize(value, False)
134+
elif format_type == 'capitalize':
126135
value = inflection.camelize(value)
127136
elif format_type == 'underscore':
128137
value = inflection.underscore(value)
@@ -132,15 +141,10 @@ def format_value(value, format_type=None):
132141
def format_relation_name(value, format_type=None):
133142
if format_type is None:
134143
format_type = getattr(settings, 'JSON_API_FORMAT_RELATION_KEYS', False)
135-
144+
136145
if not format_type:
137-
# let's keep it the way it was
138146
return value
139147

140-
# in case the value is going to be changed, make it underscored first
141-
# because dasherize does not work with a camel cased string
142-
value = inflection.underscore(value)
143-
144148
# format_type will never be None here so we can use format_value
145149
value = format_value(value, format_type)
146150

0 commit comments

Comments
 (0)