From 58f55b466adf0401cbfaac79c04b9c57dfcbcd39 Mon Sep 17 00:00:00 2001 From: Denis Orehovsky Date: Sun, 23 Aug 2020 09:02:49 +0300 Subject: [PATCH 1/2] Add a method for getting serializer field name --- rest_framework/schemas/openapi.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index 9774a94c76..f16b3d1e97 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -516,7 +516,7 @@ def map_serializer(self, serializer): continue if field.required: - required.append(field.field_name) + required.append(self.get_field_name(field)) schema = self.map_field(field) if field.read_only: @@ -531,7 +531,7 @@ def map_serializer(self, serializer): schema['description'] = str(field.help_text) self.map_field_validators(field, schema) - properties[field.field_name] = schema + properties[self.get_field_name(field)] = schema result = { 'type': 'object', @@ -582,6 +582,9 @@ def map_field_validators(self, field, schema): schema['maximum'] = int(digits * '9') + 1 schema['minimum'] = -schema['maximum'] + def get_field_name(self, field): + return field.field_name + def get_paginator(self): pagination_class = getattr(self.view, 'pagination_class', None) if pagination_class: From 65ab47ce5527e10743b0f5729a93e2d69fbc12cd Mon Sep 17 00:00:00 2001 From: Denis Orehovsky Date: Sun, 23 Aug 2020 11:41:34 +0300 Subject: [PATCH 2/2] Add docs and test --- rest_framework/schemas/openapi.py | 4 ++++ tests/schemas/test_openapi.py | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index f16b3d1e97..4f6f180675 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -583,6 +583,10 @@ def map_field_validators(self, field, schema): schema['minimum'] = -schema['maximum'] def get_field_name(self, field): + """ + Override this method if you want to change schema field name. + For example, convert snake_case field name to camelCase. + """ return field.field_name def get_paginator(self): diff --git a/tests/schemas/test_openapi.py b/tests/schemas/test_openapi.py index d483f3d456..d3f1629758 100644 --- a/tests/schemas/test_openapi.py +++ b/tests/schemas/test_openapi.py @@ -107,6 +107,20 @@ class Serializer(serializers.Serializer): assert data['properties']['default_false']['default'] is False, "default must be false" assert 'default' not in data['properties']['without_default'], "default must not be defined" + def test_custom_field_name(self): + class CustomSchema(AutoSchema): + def get_field_name(self, field): + return 'custom_' + field.field_name + + class Serializer(serializers.Serializer): + text_field = serializers.CharField() + + inspector = CustomSchema() + + data = inspector.map_serializer(Serializer()) + assert 'custom_text_field' in data['properties'] + assert 'text_field' not in data['properties'] + @pytest.mark.skipif(uritemplate is None, reason='uritemplate not installed.') class TestOperationIntrospection(TestCase):