Skip to content

Commit

Permalink
Fixed min/max attributes for serializers.ListField (#6866)
Browse files Browse the repository at this point in the history
  • Loading branch information
knivets authored and carltongibson committed Sep 3, 2019
1 parent f8c1644 commit 1cc4be4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
18 changes: 11 additions & 7 deletions rest_framework/schemas/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def _map_serializer(self, serializer):
schema['default'] = field.default
if field.help_text:
schema['description'] = str(field.help_text)
self._map_field_validators(field.validators, schema)
self._map_field_validators(field, schema)

properties[field.field_name] = schema

Expand All @@ -389,13 +389,11 @@ def _map_serializer(self, serializer):

return result

def _map_field_validators(self, validators, schema):
def _map_field_validators(self, field, schema):
"""
map field validators
:param list:validators: list of field validators
:param dict:schema: schema that the validators get added to
"""
for v in validators:
for v in field.validators:
# "Formats such as "email", "uuid", and so on, MAY be used even though undefined by this specification."
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#data-types
if isinstance(v, EmailValidator):
Expand All @@ -405,9 +403,15 @@ def _map_field_validators(self, validators, schema):
if isinstance(v, RegexValidator):
schema['pattern'] = v.regex.pattern
elif isinstance(v, MaxLengthValidator):
schema['maxLength'] = v.limit_value
attr_name = 'maxLength'
if isinstance(field, serializers.ListField):
attr_name = 'maxItems'
schema[attr_name] = v.limit_value
elif isinstance(v, MinLengthValidator):
schema['minLength'] = v.limit_value
attr_name = 'minLength'
if isinstance(field, serializers.ListField):
attr_name = 'minItems'
schema[attr_name] = v.limit_value
elif isinstance(v, MaxValueValidator):
schema['maximum'] = v.limit_value
elif isinstance(v, MinValueValidator):
Expand Down
3 changes: 3 additions & 0 deletions tests/schemas/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,9 @@ def test_serializer_validators(self):
assert properties['string']['minLength'] == 2
assert properties['string']['maxLength'] == 10

assert properties['lst']['minItems'] == 2
assert properties['lst']['maxItems'] == 10

assert properties['regex']['pattern'] == r'[ABC]12{3}'
assert properties['regex']['description'] == 'must have an A, B, or C followed by 1222'

Expand Down
6 changes: 6 additions & 0 deletions tests/schemas/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ class ExampleValidatedSerializer(serializers.Serializer):
),
help_text='must have an A, B, or C followed by 1222'
)
lst = serializers.ListField(
validators=(
MaxLengthValidator(limit_value=10),
MinLengthValidator(limit_value=2),
)
)
decimal1 = serializers.DecimalField(max_digits=6, decimal_places=2)
decimal2 = serializers.DecimalField(max_digits=5, decimal_places=0,
validators=(DecimalValidator(max_digits=17, decimal_places=4),))
Expand Down

0 comments on commit 1cc4be4

Please sign in to comment.