FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

BUG: If a float-specified field holds an integer number, the resultin… by wolfgang-noichl · Pull Request #1421 · openapi-generators/openapi-python-client · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .jinja  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
9 changes: 9 additions & 0 deletions openapi_python_client/templates/model.py.jinja
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL