Skip to content

Render decimals as numbers in the OpenAPI YAML schema - #10112

Open
LindseyZ1205 wants to merge 5 commits into
encode:mainfrom
LindseyZ1205:fix/openapi-yaml-decimal
Open

LindseyZ1205 wants to merge 5 commits into
encode:mainfrom
LindseyZ1205:fix/openapi-yaml-decimal

Conversation

@LindseyZ1205

Copy link
Copy Markdown
Contributor

manage.py generateschema (and the OpenAPIRenderer) writes Decimal values in the schema as Python-specific YAML tags. For example, a model field such as

price = models.DecimalField(
    max_digits=6, decimal_places=2,
    validators=[MinValueValidator(Decimal('0.01')), MaxValueValidator(Decimal('500'))],
)

gives

          maximum: !!python/object/apply:decimal.Decimal
          - '500'
          minimum: !!python/object/apply:decimal.Decimal
          - '0.01'

which YAML loaders other than PyYAML's unsafe one can't read: yaml.safe_load() fails with "could not determine a constructor for the tag 'tag:yaml.org,2002:python/object/apply:decimal.Decimal'". Since #9997, a model DecimalField with a decimal default ends up in the schema in the same way.

This is the same problem as #8063 for timedelta, which was fixed with a representer for the YAML Dumper. This adds one for Decimal, which renders the value as a number, as the JSONEncoder of the JSONOpenAPIRenderer does (maximum: 500.0, minimum: 0.01).

I added a DecimalField with these validators to ExampleValidatedSerializer, and a YAML rendering test next to the timedelta one; the test fails without the change. The full test suite passes with Django 6.1.2 and Django main, and flake8, isort and codespell pass on the changed files.

The `OpenAPIRenderer` dumped `Decimal` values, such as the limits of a
`MaxValueValidator(Decimal('500'))` on a `DecimalField`, as
`!!python/object/apply:decimal.Decimal` tags, which YAML loaders other
than PyYAML's unsafe one can't read. Render them as numbers, as the
`JSONEncoder` used by the `JSONOpenAPIRenderer` does.
@auvipy
auvipy self-requested a review November 19, 2026 06:03
@auvipy auvipy added this to the 3.18 milestone Nov 23, 2026
@auvipy

auvipy commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

@M2004GV would you mind cross checking this, please?

@M2004GV

M2004GV commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

@auvipy, I can take a look!!

Comment thread rest_framework/utils/encoders.py Outdated
@classmethod
def represent_decimal(cls, dumper, data):
# As in JSONEncoder, render the decimal as a number.
return dumper.represent_float(float(data))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's right.

When coerce_to_string=true, then Decimal are actually represented as string in the API responses, not floats.

Casting to float loses precision: e.g. Decimal('0.12345678901234567890') renders 0.12345678901234568. Decimal('1E+400') renders .inf, NaN renders .nan

LindseyZ1205 and others added 3 commits November 24, 2026 16:48
Converting to `float` rounded values such as `0.12345678901234567890`
and turned `1E+400` into `.inf`. Write the decimal digits as a YAML
number instead.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants