diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index f16b3d1e9..4f6f18067 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -583,6 +583,10 @@ class AutoSchema(ViewInspector): 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 d483f3d45..d3f162975 100644 --- a/tests/schemas/test_openapi.py +++ b/tests/schemas/test_openapi.py @@ -107,6 +107,20 @@ class TestFieldMapping(TestCase): 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):