Skip to content

Commit

Permalink
Merge pull request #1136 from tfranzel/fix_decimal_yaml_round2
Browse files Browse the repository at this point in the history
coerse Decimal to float format explicitly #1134
  • Loading branch information
tfranzel authored Dec 26, 2023
2 parents 36e4e18 + 98eff0c commit 200b123
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
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

0 comments on commit 200b123

Please sign in to comment.