diff --git a/src/betterproto/__init__.py b/src/betterproto/__init__.py index b26ec4b..9a337c4 100644 --- a/src/betterproto/__init__.py +++ b/src/betterproto/__init__.py @@ -961,7 +961,15 @@ class Message(ABC): output[cased_name] = value elif field_is_repeated: # Convert each item. - value = [i.to_dict(casing, include_default_values) for i in value] + cls = self._betterproto.cls_by_field[field_name] + if cls == datetime: + value = [_Timestamp.timestamp_to_json(i) for i in value] + elif cls == timedelta: + value = [_Duration.delta_to_json(i) for i in value] + else: + value = [ + i.to_dict(casing, include_default_values) for i in value + ] if value or include_default_values: output[cased_name] = value elif ( @@ -1042,8 +1050,18 @@ class Message(ABC): v = getattr(self, field_name) if isinstance(v, list): cls = self._betterproto.cls_by_field[field_name] - for item in value[key]: - v.append(cls().from_dict(item)) + if cls == datetime: + v = [ + datetime.fromisoformat(item.replace("Z", "+00:00")) + for item in value[key] + ] + elif cls == timedelta: + v = [ + timedelta(seconds=float(item[:-1])) + for item in value[key] + ] + else: + v = [cls().from_dict(item) for item in value[key]] elif isinstance(v, datetime): v = datetime.fromisoformat(value[key].replace("Z", "+00:00")) setattr(self, field_name, v) diff --git a/tests/inputs/repeated_duration_timestamp/repeated_duration_timestamp.json b/tests/inputs/repeated_duration_timestamp/repeated_duration_timestamp.json new file mode 100644 index 0000000..6ce7b34 --- /dev/null +++ b/tests/inputs/repeated_duration_timestamp/repeated_duration_timestamp.json @@ -0,0 +1,4 @@ +{ + "times": ["1972-01-01T10:00:20.021Z", "1972-01-01T10:00:20.021Z"], + "durations": ["1.200s", "1.200s"] +} diff --git a/tests/inputs/repeated_duration_timestamp/repeated_duration_timestamp.proto b/tests/inputs/repeated_duration_timestamp/repeated_duration_timestamp.proto new file mode 100644 index 0000000..7b7bf36 --- /dev/null +++ b/tests/inputs/repeated_duration_timestamp/repeated_duration_timestamp.proto @@ -0,0 +1,10 @@ +syntax = "proto3"; + +import "google/protobuf/duration.proto"; +import "google/protobuf/timestamp.proto"; + + +message Test { + repeated google.protobuf.Timestamp times = 1; + repeated google.protobuf.Duration durations = 2; +} diff --git a/tests/inputs/repeated_duration_timestamp/test_repeated_duration_timestamp.py b/tests/inputs/repeated_duration_timestamp/test_repeated_duration_timestamp.py new file mode 100644 index 0000000..b1b13e5 --- /dev/null +++ b/tests/inputs/repeated_duration_timestamp/test_repeated_duration_timestamp.py @@ -0,0 +1,9 @@ +from datetime import datetime, timedelta + +from tests.output_betterproto.repeated_duration_timestamp import Test + + +def test_roundtrip(): + message = Test() + message.times = [datetime.now(), datetime.now()] + message.durations = [timedelta(), timedelta()]