Skip to content

Added capitalize to the inflection options. camelCase now lowercases first letter #93

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
merged 3 commits into from
Sep 17, 2015
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
4 changes: 2 additions & 2 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ JSON_API_FORMAT_KEYS = 'dasherize'
Possible values:

* dasherize
* camelize
* camelize (first letter is lowercase)
* capitalize (camelize but with first letter uppercase)
* underscore
* pluralize

Note: due to the way the inflector works `address_1` can camelize to `address1`
on output but it cannot convert `address1` back to `address_1` on POST or PUT. Keep
Expand Down
2 changes: 1 addition & 1 deletion example/tests/test_format_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_camelization(self):
expected = {
'data': [
{
'type': 'Users',
'type': 'users',
'id': encoding.force_text(user.pk),
'attributes': {
'firstName': user.first_name,
Expand Down
24 changes: 18 additions & 6 deletions example/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from django.conf import settings
from django.contrib.auth import get_user_model
from rest_framework import serializers
from rest_framework.response import Response
Expand All @@ -8,7 +9,7 @@

pytestmark = pytest.mark.django_db

class Resource(APIView):
class ResourceView(APIView):
pass

class ResourceSerializer(serializers.ModelSerializer):
Expand All @@ -17,9 +18,15 @@ class Meta():
model = get_user_model()

def test_get_resource_name():
view = Resource()
view = ResourceView()
context = {'view': view}
assert 'resources' == utils.get_resource_name(context), 'derived from view'
setattr(settings, 'JSON_API_FORMAT_KEYS', None)
assert 'ResourceViews' == utils.get_resource_name(context), 'not formatted'

view = ResourceView()
context = {'view': view}
setattr(settings, 'JSON_API_FORMAT_KEYS', 'dasherize')
assert 'resource-views' == utils.get_resource_name(context), 'derived from view'

view.model = get_user_model()
assert 'users' == utils.get_resource_name(context), 'derived from view model'
Expand All @@ -33,7 +40,7 @@ def test_get_resource_name():
view.response = Response(status=500)
assert 'errors' == utils.get_resource_name(context), 'handles 500 error'

view = Resource()
view = ResourceView()
context = {'view': view}
view.serializer_class = ResourceSerializer
assert 'users' == utils.get_resource_name(context), 'derived from serializer'
Expand All @@ -50,6 +57,9 @@ def test_format_keys():
output = {'firstName': 'a', 'lastName': 'b'}
assert utils.format_keys(underscored, 'camelize') == output

output = {'FirstName': 'a', 'LastName': 'b'}
assert utils.format_keys(underscored, 'capitalize') == output

output = {'first-name': 'a', 'last-name': 'b'}
assert utils.format_keys(underscored, 'dasherize') == output

Expand All @@ -60,12 +70,14 @@ def test_format_keys():
assert utils.format_keys([underscored], 'dasherize') == output

def test_format_value():
assert utils.format_value('first_name', 'camelize') == 'FirstName'
assert utils.format_value('first_name', 'camelize') == 'firstName'
assert utils.format_value('first_name', 'capitalize') == 'FirstName'
assert utils.format_value('first_name', 'dasherize') == 'first-name'
assert utils.format_value('first-name', 'underscore') == 'first_name'

def test_format_relation_name():
assert utils.format_relation_name('first_name', 'camelize') == 'FirstNames'
assert utils.format_relation_name('first_name', 'capitalize') == 'FirstNames'
assert utils.format_relation_name('first_name', 'camelize') == 'firstNames'

def test_build_json_resource_obj():
resource = {
Expand Down
22 changes: 13 additions & 9 deletions rest_framework_json_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ def get_resource_name(context):
if not isinstance(resource_name, six.string_types):
return resource_name

resource_name = inflection.pluralize(resource_name.lower())

resource_name = format_value(resource_name)

resource_name = inflection.pluralize(resource_name)

return resource_name


Expand All @@ -94,17 +94,22 @@ def format_keys(obj, format_type=None):
if format_type is None:
format_type = getattr(settings, 'JSON_API_FORMAT_KEYS', False)

if format_type in ('dasherize', 'camelize', 'underscore'):
if format_type in ('dasherize', 'camelize', 'underscore', 'capitalize'):

if isinstance(obj, dict):
formatted = OrderedDict()
for key, value in obj.items():
if format_type == 'dasherize':
# inflection can't dasherize camelCase
key = inflection.underscore(key)
formatted[inflection.dasherize(key)] \
= format_keys(value, format_type)
elif format_type == 'camelize':
formatted[inflection.camelize(key, False)] \
= format_keys(value, format_type)
elif format_type == 'capitalize':
formatted[inflection.camelize(key)] \
= format_keys(value, format_type)
elif format_type == 'underscore':
formatted[inflection.underscore(key)] \
= format_keys(value, format_type)
Expand All @@ -121,8 +126,12 @@ def format_value(value, format_type=None):
if format_type is None:
format_type = getattr(settings, 'JSON_API_FORMAT_KEYS', False)
if format_type == 'dasherize':
# inflection can't dasherize camelCase
value = inflection.underscore(value)
value = inflection.dasherize(value)
elif format_type == 'camelize':
value = inflection.camelize(value, False)
elif format_type == 'capitalize':
value = inflection.camelize(value)
elif format_type == 'underscore':
value = inflection.underscore(value)
Expand All @@ -132,15 +141,10 @@ def format_value(value, format_type=None):
def format_relation_name(value, format_type=None):
if format_type is None:
format_type = getattr(settings, 'JSON_API_FORMAT_RELATION_KEYS', False)

if not format_type:
# let's keep it the way it was
return value

# in case the value is going to be changed, make it underscored first
# because dasherize does not work with a camel cased string
value = inflection.underscore(value)

# format_type will never be None here so we can use format_value
value = format_value(value, format_type)

Expand Down