From 987f2e8858231352505e22e0250ecb37af624a95 Mon Sep 17 00:00:00 2001 From: shemogumbe Date: Fri, 23 Aug 2024 19:40:22 +0300 Subject: [PATCH 1/8] Add regex to be skipped by pendulum when converting strings to dates --- kiota_serialization_json/json_parse_node.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kiota_serialization_json/json_parse_node.py b/kiota_serialization_json/json_parse_node.py index d1e6751..13b5efc 100644 --- a/kiota_serialization_json/json_parse_node.py +++ b/kiota_serialization_json/json_parse_node.py @@ -8,6 +8,8 @@ from uuid import UUID import pendulum +import re + from kiota_abstractions.serialization import Parsable, ParsableFactory, ParseNode T = TypeVar("T") @@ -287,6 +289,10 @@ def _assign_field_values(self, item: U) -> None: deserialize but the model doesn't support additional data" ) + def is_four_digit_number(self, value: str) -> bool: + pattern = r'^\d{4}$' + return bool(re.match(pattern, value)) + def try_get_anything(self, value: Any) -> Any: if isinstance(value, (int, float, bool)) or value is None: return value From da59b3483222f42928bb8afcbfa3082528657f50 Mon Sep 17 00:00:00 2001 From: shemogumbe Date: Fri, 23 Aug 2024 19:43:31 +0300 Subject: [PATCH 2/8] integrate regex to bypass pendulum bug where pendulum assumes 4 digit strings are datetime objects --- kiota_serialization_json/json_parse_node.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/kiota_serialization_json/json_parse_node.py b/kiota_serialization_json/json_parse_node.py index 13b5efc..e60fa02 100644 --- a/kiota_serialization_json/json_parse_node.py +++ b/kiota_serialization_json/json_parse_node.py @@ -302,10 +302,14 @@ def try_get_anything(self, value: Any) -> Any: return dict(map(lambda x: (x[0], self.try_get_anything(x[1])), value.items())) if isinstance(value, str): try: - datetime_obj = pendulum.parse(value) - if isinstance(datetime_obj, pendulum.Duration): - return datetime_obj.as_timedelta() - return datetime_obj + if self.is_four_digit_number(value): + print(value) + return value + else: + datetime_obj = pendulum.parse(value) + if isinstance(datetime_obj, pendulum.Duration): + return datetime_obj.as_timedelta() + return datetime_obj except ValueError: pass try: From 96b4615bc82fe2c2707480a18b6af8c2322d35e5 Mon Sep 17 00:00:00 2001 From: shemogumbe Date: Fri, 23 Aug 2024 20:01:00 +0300 Subject: [PATCH 3/8] fix code format --- kiota_serialization_json/json_parse_node.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/kiota_serialization_json/json_parse_node.py b/kiota_serialization_json/json_parse_node.py index e60fa02..3242072 100644 --- a/kiota_serialization_json/json_parse_node.py +++ b/kiota_serialization_json/json_parse_node.py @@ -305,11 +305,11 @@ def try_get_anything(self, value: Any) -> Any: if self.is_four_digit_number(value): print(value) return value - else: - datetime_obj = pendulum.parse(value) - if isinstance(datetime_obj, pendulum.Duration): - return datetime_obj.as_timedelta() - return datetime_obj + + datetime_obj = pendulum.parse(value) + if isinstance(datetime_obj, pendulum.Duration): + return datetime_obj.as_timedelta() + return datetime_obj except ValueError: pass try: From 8eef3e6accdcf5b18cd837b0d912d05990f9dab8 Mon Sep 17 00:00:00 2001 From: shemogumbe Date: Fri, 23 Aug 2024 20:05:46 +0300 Subject: [PATCH 4/8] fix conflicts --- kiota_serialization_json/json_parse_node.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/kiota_serialization_json/json_parse_node.py b/kiota_serialization_json/json_parse_node.py index 4366285..c60dcd8 100644 --- a/kiota_serialization_json/json_parse_node.py +++ b/kiota_serialization_json/json_parse_node.py @@ -309,12 +309,12 @@ def try_get_anything(self, value: Any) -> Any: if self.is_four_digit_number(value): print(value) return value - else: - datetime_obj = pendulum.parse(value) - if isinstance(datetime_obj, pendulum.Duration): - return datetime_obj.as_timedelta() - return datetime_obj - except: + + datetime_obj = pendulum.parse(value) + if isinstance(datetime_obj, pendulum.Duration): + return datetime_obj.as_timedelta() + return datetime_obj + except ValueError: pass try: return UUID(value) From 626f2e324473e1647d673bfd859c4b65de705de4 Mon Sep 17 00:00:00 2001 From: Shem Ogumbe Date: Fri, 23 Aug 2024 20:24:41 +0300 Subject: [PATCH 5/8] Update kiota_serialization_json/json_parse_node.py Co-authored-by: Vincent Biret --- kiota_serialization_json/json_parse_node.py | 1 - 1 file changed, 1 deletion(-) diff --git a/kiota_serialization_json/json_parse_node.py b/kiota_serialization_json/json_parse_node.py index c60dcd8..deb4994 100644 --- a/kiota_serialization_json/json_parse_node.py +++ b/kiota_serialization_json/json_parse_node.py @@ -307,7 +307,6 @@ def try_get_anything(self, value: Any) -> Any: if isinstance(value, str): try: if self.is_four_digit_number(value): - print(value) return value datetime_obj = pendulum.parse(value) From 1b81019639146a287ce3d459d469234e80ef1fd9 Mon Sep 17 00:00:00 2001 From: shemogumbe Date: Fri, 23 Aug 2024 20:25:19 +0300 Subject: [PATCH 6/8] updated change log and version --- CHANGELOG.md | 8 ++++++++ kiota_serialization_json/_version.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85cf7a3..6cf3880 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [1.4.0] - 2024-08-23 + +### Added + +- Fixed 4-digit numeric strings from being parsed as Datetime objects to being parsed as strings. + + ## [1.3.0] - 2024-07-26 ### Added diff --git a/kiota_serialization_json/_version.py b/kiota_serialization_json/_version.py index 3810552..4b74830 100644 --- a/kiota_serialization_json/_version.py +++ b/kiota_serialization_json/_version.py @@ -1 +1 @@ -VERSION: str = '1.3.0' +VERSION: str = '1.4.0' From 5f8b03acd90cc4dfbf4084273dbc30fe332a6fce Mon Sep 17 00:00:00 2001 From: shemogumbe Date: Fri, 23 Aug 2024 20:42:01 +0300 Subject: [PATCH 7/8] Added unit tests to fix --- tests/unit/test_json_parse_node.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/unit/test_json_parse_node.py b/tests/unit/test_json_parse_node.py index b50e7e4..7bec942 100644 --- a/tests/unit/test_json_parse_node.py +++ b/tests/unit/test_json_parse_node.py @@ -1,6 +1,7 @@ import json from datetime import date, datetime, time, timedelta from uuid import UUID +import pendulum import pytest from pendulum import DateTime, FixedTimezone @@ -54,17 +55,20 @@ def test_get_float_value(value: int): 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() 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') result = parse_node.get_datetime_value() @@ -130,6 +134,20 @@ def test_get_enum_value_for_key_not_found(): assert result is None +def test_get_anythin_does_not_convert_numeric_chars_to_datetime(): + parse_node = JsonParseNode("1212") + result = parse_node.try_get_anything("1212") + assert isinstance(result, str) + assert result == "1212" + + +def test_get_anythin_does_convert_date_string_to_datetime(): + parse_node = JsonParseNode("2023-10-05T14:48:00.000Z") + result = parse_node.try_get_anything("2023-10-05T14:48:00.000Z") + assert isinstance(result, pendulum.DateTime) + assert result == pendulum.parse("2023-10-05T14:48:00.000Z") + + def test_get_object_value(user1_json): parse_node = JsonParseNode(json.loads(user1_json)) result = parse_node.get_object_value(User) From 27e639ace6aac2a371c9c6f19553f90263f27e46 Mon Sep 17 00:00:00 2001 From: shemogumbe Date: Fri, 23 Aug 2024 20:47:11 +0300 Subject: [PATCH 8/8] correct version number --- CHANGELOG.md | 2 +- kiota_serialization_json/_version.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cf3880..fb6a78f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [1.4.0] - 2024-08-23 +## [1.3.1] - 2024-08-23 ### Added diff --git a/kiota_serialization_json/_version.py b/kiota_serialization_json/_version.py index 4b74830..d045b44 100644 --- a/kiota_serialization_json/_version.py +++ b/kiota_serialization_json/_version.py @@ -1 +1 @@ -VERSION: str = '1.4.0' +VERSION: str = '1.3.1'