From c29e3c716f595fc8073748cfbc88dc26ba421a4b Mon Sep 17 00:00:00 2001 From: Wolfgang Noichl Date: Fri, 10 Apr 2026 19:01:18 +0200 Subject: [PATCH] BUG: If a float-specified field holds an integer number, the resulting object is int instead of float --- openapi_python_client/templates/model.py.jinja | 9 +++++++++ .../templates/property_templates/union_property.py.jinja | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/openapi_python_client/templates/model.py.jinja b/openapi_python_client/templates/model.py.jinja index 50bc36ca3..362025c76 100644 --- a/openapi_python_client/templates/model.py.jinja +++ b/openapi_python_client/templates/model.py.jinja @@ -5,6 +5,7 @@ from typing import Any, TypeVar, BinaryIO, TextIO, TYPE_CHECKING, Generator from attrs import define as _attrs_define from attrs import field as _attrs_field + {% if model.is_multipart_body %} import json from .. import types @@ -24,6 +25,14 @@ if TYPE_CHECKING: {% endfor %} +def json_aware_cast(typ, val): + # json does not distinguish between float and int, but the scheme carries this information. + # So cast it here, to actually get correct types. + # https://json-schema.org/understanding-json-schema/reference/numeric + if issubclass(float, typ) and not issubclass(int, typ) and isinstance(val, int): + val = float(val) + return cast(typ, val) + {% if model.additional_properties %} {% set additional_property_type = 'Any' if model.additional_properties == True else model.additional_properties.get_type_string() %} {% endif %} diff --git a/openapi_python_client/templates/property_templates/union_property.py.jinja b/openapi_python_client/templates/property_templates/union_property.py.jinja index f4fd19f34..94b8c342f 100644 --- a/openapi_python_client/templates/property_templates/union_property.py.jinja +++ b/openapi_python_client/templates/property_templates/union_property.py.jinja @@ -33,7 +33,7 @@ def _parse_{{ property.python_name }}(data: object) -> {{ property.get_type_stri {% endif %} {% endfor %} {% if ns.contains_unmodified_properties %} - return cast({{ property.get_type_string() }}, data) + return json_aware_cast({{ property.get_type_string() }}, data) {% endif %} {{ property.python_name }} = _parse_{{ property.python_name }}({{ source }})