Skip to content
Open
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
9 changes: 9 additions & 0 deletions openapi_python_client/templates/model.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }})
Expand Down