Skip to content
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

coerse Decimal to float format explicitly #1134 #1136

Merged
merged 1 commit into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion drf_spectacular/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def multiline_str_representer(dumper, data):

def decimal_representer(dumper, data):
# prevent emitting "!! float" tags on fractionless decimals
value = str(data)
value = f'{data:f}'
if '.' in value:
return dumper.represent_scalar('tag:yaml.org,2002:float', value)
else:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
from datetime import timedelta
from decimal import Decimal
from unittest import mock

import pytest
Expand Down Expand Up @@ -53,6 +54,16 @@ class XSerializer(serializers.Serializer):
decimal_places=1,
validators=[validators.DecimalValidator(max_digits=4, decimal_places=2)],
)
decimal_validator_decimal = serializers.DecimalField(
max_digits=9,
decimal_places=7,
validators=[validators.MinValueValidator(Decimal("0.0000000001"))]
)
decimal_min_value_decimal = serializers.DecimalField(
decimal_places=2,
max_digits=5,
min_value=Decimal('0.000000000000000000000001'),
)

# The following only apply for `array` type:
list_max_length = serializers.ListField(validators=[validators.MaxLengthValidator(200)])
Expand Down
14 changes: 14 additions & 0 deletions tests/test_validators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ components:
minimum: -100
exclusiveMaximum: true
exclusiveMinimum: true
decimal_validator_decimal:
type: number
format: double
maximum: 100
minimum: 0.0000000001
exclusiveMaximum: true
decimal_min_value_decimal:
type: number
format: double
maximum: 1000
minimum: 0.000000000000000000000001
exclusiveMaximum: true
list_max_length:
type: array
items: {}
Expand Down Expand Up @@ -183,6 +195,8 @@ components:
- decimal_decimal
- decimal_max_value
- decimal_min_value
- decimal_min_value_decimal
- decimal_validator_decimal
- dict_max_length
- dict_min_length
- file_extension
Expand Down