Add docs and test

This commit is contained in:
Denis Orehovsky 2020-08-23 11:41:34 +03:00
parent 58f55b466a
commit 65ab47ce55
2 changed files with 18 additions and 0 deletions

View File

@ -583,6 +583,10 @@ class AutoSchema(ViewInspector):
schema['minimum'] = -schema['maximum'] schema['minimum'] = -schema['maximum']
def get_field_name(self, field): 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 return field.field_name
def get_paginator(self): def get_paginator(self):

View File

@ -107,6 +107,20 @@ class TestFieldMapping(TestCase):
assert data['properties']['default_false']['default'] is False, "default must be false" 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" 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.') @pytest.mark.skipif(uritemplate is None, reason='uritemplate not installed.')
class TestOperationIntrospection(TestCase): class TestOperationIntrospection(TestCase):