Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
Merge pull request #333 from pjmagee/floats-bug-fix-331
Browse files Browse the repository at this point in the history
Fix data loss with 'None' being returned in cases where an integer is returned.
  • Loading branch information
baywet authored Jul 26, 2024
2 parents db233a0 + 04220ac commit bc00609
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Fixed a bug where date time deserialization would fail because of empty strings.
- Fixed a bug where float deserialization if the number represented qualified as an int.

## [1.2.0] - 2024-04-09

Expand Down
4 changes: 2 additions & 2 deletions kiota_serialization_json/json_parse_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ def get_int_value(self) -> Optional[int]:
def get_float_value(self) -> Optional[float]:
"""Gets the float value of the json node
Returns:
float: The integer value of the node
float: The number value of the node
"""
return self._json_node if isinstance(self._json_node, float) else None
return float(self._json_node) if isinstance(self._json_node, (float, int)) else None

def get_uuid_value(self) -> Optional[UUID]:
"""Gets the UUID value of the json node
Expand Down
22 changes: 21 additions & 1 deletion tests/unit/test_json_parse_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,32 @@ def test_get_bool_value():
assert result is False


def test_get_float_value():
def test_get_float_value_from_float():
"""
This test is to ensure that the get_float_value method returns a float when the value is a float
"""
parse_node = JsonParseNode(44.6)
result = parse_node.get_float_value()
assert isinstance(result, float)
assert result == 44.6


@pytest.mark.parametrize("value", [0, 10, 100])
def test_get_float_value(value: int):
"""
Consider an OpenAPI Specification using the type: number and format: float or double
Note: The OpenAPI Specification also allows for the use of the type: integer and format: int32 or int64
Consider an API with Price data [0, 0.5, 1, 1.5, 2] and so on
In this case, the contract must define the type as a number, with a hint of float or double as the format
Kiota should be able to parse the response as a float, even if the value is an integer, because it's still a number.
"""
parse_node = JsonParseNode(value)
result = parse_node.get_float_value()
assert isinstance(result, float)
assert result == float(value)

def test_get_uuid_value():
parse_node = JsonParseNode("f58411c7-ae78-4d3c-bb0d-3f24d948de41")
result = parse_node.get_uuid_value()
Expand Down

0 comments on commit bc00609

Please sign in to comment.