From 0075e771c05d3b301344948f4dab83e950136db3 Mon Sep 17 00:00:00 2001 From: Patrick Magee Date: Fri, 26 Jul 2024 11:05:33 +0100 Subject: [PATCH 1/2] In the ISO 8601 date format (YYYY-MM-DD), the shortest possible valid date string is 10 characters long. Therefore, if the length of the string is less than 10, it cannot be a valid date in this format, and the function can immediately return None without attempting to parse the date, which would be a more expensive operation. --- kiota_serialization_json/json_parse_node.py | 4 ++++ tests/unit/test_json_parse_node.py | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/kiota_serialization_json/json_parse_node.py b/kiota_serialization_json/json_parse_node.py index 2a2dd4f..9d1782b 100644 --- a/kiota_serialization_json/json_parse_node.py +++ b/kiota_serialization_json/json_parse_node.py @@ -89,7 +89,11 @@ def get_datetime_value(self) -> Optional[datetime]: """ if isinstance(self._json_node, datetime): return self._json_node + if isinstance(self._json_node, str): + if len(self._json_node) < 10: + return None + datetime_obj = pendulum.parse(self._json_node, exact=True) if isinstance(datetime_obj, pendulum.DateTime): return datetime_obj diff --git a/tests/unit/test_json_parse_node.py b/tests/unit/test_json_parse_node.py index d5c605a..f5e196c 100644 --- a/tests/unit/test_json_parse_node.py +++ b/tests/unit/test_json_parse_node.py @@ -39,6 +39,11 @@ def test_get_uuid_value(): result = parse_node.get_uuid_value() assert result == UUID("f58411c7-ae78-4d3c-bb0d-3f24d948de41") +@pytest.mark.parametrize("value", ["", " ", " ", "2022-01-0"]) +def test_get_datetime_value_returns_none_with_invalid_str(value: str): + parse_node = JsonParseNode(value) + result = parse_node.get_datetime_value() + assert result is None def test_get_datetime_value(): parse_node = JsonParseNode('2022-01-27T12:59:45.596117') From acf513b9691dde6c34cf3f1c721855cc742db3c9 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 26 Jul 2024 09:03:16 -0400 Subject: [PATCH 2/2] chore: adds changelog entry for date time fix --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 244cbe5..46f976f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,10 +8,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [1.3.0] - 2024-07-26 ### Added + - Support `dict[str, Any]` and `list[dict[str, Any]]` when writing additional data. ### Changed +- Fixed a bug where date time deserialization would fail because of empty strings. + ## [1.2.0] - 2024-04-09 ### Added