Skip to content

Commit

Permalink
Format field names correctly for metadata in OPTIONS requests (#713)
Browse files Browse the repository at this point in the history
Take into account settings JSON_API_FORMAT_FIELD_NAMES
  • Loading branch information
sliverc authored Oct 3, 2019
1 parent b3cacb7 commit b99bfe4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ This release is not backwards compatible. For easy migration best upgrade first

* Avoid printing invalid pointer when api returns 404
* Avoid exception when using `ResourceIdentifierObjectSerializer` with unexisting primary key
* Format metadata field names correctly for OPTIONS request


## [2.8.0] - 2019-06-13
Expand Down
9 changes: 9 additions & 0 deletions example/tests/test_format_keys.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib.auth import get_user_model
from django.urls import reverse
from django.utils import encoding
from rest_framework import status

from example.tests import TestBase

Expand Down Expand Up @@ -51,3 +52,11 @@ def test_camelization(self):
}

assert expected == response.json()


def test_options_format_field_names(db, client):
response = client.options(reverse('author-list'))
assert response.status_code == status.HTTP_200_OK
data = response.json()['data']
expected_keys = {'name', 'email', 'bio', 'entries', 'firstEntry', 'type', 'comments'}
assert expected_keys == data['actions']['POST'].keys()
4 changes: 2 additions & 2 deletions rest_framework_json_api/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from rest_framework.settings import api_settings
from rest_framework.utils.field_mapping import ClassLookupDict

from rest_framework_json_api.utils import get_related_resource_type
from rest_framework_json_api.utils import format_value, get_related_resource_type


class JSONAPIMetadata(SimpleMetadata):
Expand Down Expand Up @@ -83,7 +83,7 @@ def get_serializer_info(self, serializer):
serializer.fields.pop(api_settings.URL_FIELD_NAME, None)

return OrderedDict([
(field_name, self.get_field_info(field))
(format_value(field_name), self.get_field_info(field))
for field_name, field in serializer.fields.items()
])

Expand Down

0 comments on commit b99bfe4

Please sign in to comment.