-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Corrected OpenAPI schema type for DecimalField #7254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e21156f
9176768
f80d7be
9f99135
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
from rest_framework import exceptions, renderers, serializers | ||
from rest_framework.compat import uritemplate | ||
from rest_framework.fields import _UnvalidatedField, empty | ||
from rest_framework.settings import api_settings | ||
|
||
from .generators import BaseSchemaGenerator | ||
from .inspectors import ViewInspector | ||
|
@@ -442,11 +443,17 @@ def _map_field(self, field): | |
content['format'] = field.protocol | ||
return content | ||
|
||
# DecimalField has multipleOf based on decimal_places | ||
if isinstance(field, serializers.DecimalField): | ||
content = { | ||
'type': 'number' | ||
} | ||
if getattr(field, 'coerce_to_string', api_settings.COERCE_DECIMAL_TO_STRING): | ||
content = { | ||
'type': 'string', | ||
'format': 'decimal', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went through OAI/OpenAPI-Specification#845 (comment). I found that the The rest of the code looks good to me! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I constantly struggle to find answers spelled out clearly and canonically. Do we want to add this? Inclined to take this PR as it is now, and add more later... Opinions? |
||
} | ||
else: | ||
content = { | ||
'type': 'number' | ||
} | ||
|
||
if field.decimal_places: | ||
content['multipleOf'] = float('.' + (field.decimal_places - 1) * '0' + '1') | ||
if field.max_whole_digits: | ||
|
@@ -457,7 +464,7 @@ def _map_field(self, field): | |
|
||
if isinstance(field, serializers.FloatField): | ||
content = { | ||
'type': 'number' | ||
'type': 'number', | ||
} | ||
self._map_min_max(field, content) | ||
return content | ||
|
@@ -556,7 +563,8 @@ def _map_field_validators(self, field, schema): | |
schema['maximum'] = v.limit_value | ||
elif isinstance(v, MinValueValidator): | ||
schema['minimum'] = v.limit_value | ||
clintonb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
elif isinstance(v, DecimalValidator): | ||
elif isinstance(v, DecimalValidator) and \ | ||
not getattr(field, 'coerce_to_string', api_settings.COERCE_DECIMAL_TO_STRING): | ||
if v.decimal_places: | ||
schema['multipleOf'] = float('.' + (v.decimal_places - 1) * '0' + '1') | ||
if v.max_digits: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -838,6 +838,16 @@ def test_serializer_validators(self): | |
assert properties['decimal2']['type'] == 'number' | ||
assert properties['decimal2']['multipleOf'] == .0001 | ||
|
||
assert properties['decimal3'] == { | ||
'type': 'string', 'format': 'decimal', 'maximum': 1000000, 'minimum': -1000000, 'multipleOf': 0.01 | ||
} | ||
assert properties['decimal4'] == { | ||
'type': 'string', 'format': 'decimal', 'maximum': 1000000, 'minimum': -1000000, 'multipleOf': 0.01 | ||
} | ||
assert properties['decimal5'] == { | ||
'type': 'string', 'format': 'decimal', 'maximum': 10000, 'minimum': -10000, 'multipleOf': 0.01 | ||
} | ||
|
||
assert properties['email']['type'] == 'string' | ||
assert properties['email']['format'] == 'email' | ||
assert properties['email']['default'] == '[email protected]' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,9 +119,13 @@ class ExampleValidatedSerializer(serializers.Serializer): | |
MinLengthValidator(limit_value=2), | ||
) | ||
) | ||
decimal1 = serializers.DecimalField(max_digits=6, decimal_places=2) | ||
decimal2 = serializers.DecimalField(max_digits=5, decimal_places=0, | ||
decimal1 = serializers.DecimalField(max_digits=6, decimal_places=2, coerce_to_string=False) | ||
clintonb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
decimal2 = serializers.DecimalField(max_digits=5, decimal_places=0, coerce_to_string=False, | ||
validators=(DecimalValidator(max_digits=17, decimal_places=4),)) | ||
decimal3 = serializers.DecimalField(max_digits=8, decimal_places=2, coerce_to_string=True) | ||
decimal4 = serializers.DecimalField(max_digits=8, decimal_places=2, coerce_to_string=True, | ||
validators=(DecimalValidator(max_digits=17, decimal_places=4),)) | ||
decimal5 = serializers.DecimalField(max_digits=6, decimal_places=2) | ||
email = serializers.EmailField(default='[email protected]') | ||
url = serializers.URLField(default='http://www.example.com', allow_null=True) | ||
uuid = serializers.UUIDField() | ||
|
Uh oh!
There was an error while loading. Please reload this page.