From b99bfe494271dd6ea5935f96c3a82b19de7e2a33 Mon Sep 17 00:00:00 2001 From: Oliver Sauder Date: Thu, 3 Oct 2019 20:00:10 +0400 Subject: [PATCH] Format field names correctly for metadata in OPTIONS requests (#713) Take into account settings JSON_API_FORMAT_FIELD_NAMES --- CHANGELOG.md | 1 + example/tests/test_format_keys.py | 9 +++++++++ rest_framework_json_api/metadata.py | 4 ++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff05d4f9..4ac200cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/example/tests/test_format_keys.py b/example/tests/test_format_keys.py index 22de023d..bfa61b54 100644 --- a/example/tests/test_format_keys.py +++ b/example/tests/test_format_keys.py @@ -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 @@ -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() diff --git a/rest_framework_json_api/metadata.py b/rest_framework_json_api/metadata.py index 8306a9dd..ed0a86ce 100644 --- a/rest_framework_json_api/metadata.py +++ b/rest_framework_json_api/metadata.py @@ -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): @@ -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() ])