8000 fix CBOR datetime encoding by alexrashed · Pull Request #6791 · localstack/localstack · GitHub
[go: up one dir, main page]

Skip to content

fix CBOR datetime encoding #6791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix CBOR datetime format
  • Loading branch information
alexrashed committed Aug 31, 2022
commit bdd1af3a0b691fc36695b3b40da2bc54f3fc414d
22 changes: 17 additions & 5 deletions localstack/aws/protocol/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,12 @@ def _get_mime_type(self, headers: Optional[Dict | Headers]) -> str:
mime_type = mime_accept.best_match(self.SUPPORTED_MIME_TYPES)
if not mime_type:
# There is no match between the supported mime types and the requested one(s)
mime_type = self.SUPPORTED_MIME_TYPES[0]
LOG.debug(
"Determined accept type (%s) is not supported by this serializer.", accept_header
"Determined accept type (%s) is not supported by this serializer. Using default of this serializer: %s",
accept_header,
mime_type,
)
mime_type = self.SUPPORTED_MIME_TYPES[0]
return mime_type

# Some extra utility methods subclasses can use.
Expand All @@ -481,6 +483,10 @@ def _timestamp_iso8601(value: datetime) -> str:
def _timestamp_unixtimestamp(value: datetime) -> float:
return value.timestamp()

@staticmethod
def _timestamp_unixtimestampmillis(value: datetime) -> int:
return int(value.timestamp() * 1000)

def _timestamp_rfc822(self, value: datetime) -> str:
if isinstance(value, datetime):
value = self._timestamp_unixtimestamp(value)
Expand Down Expand Up @@ -1288,10 +1294,16 @@ def _serialize_type_list(
def _default_serialize(self, body: dict, value: Any, _, key: str, __):
body[key] = value

def _serialize_type_timestamp(self, body: dict, value: Any, shape: Shape, key: str, _):
body[key] = self._convert_timestamp_to_str(
value, shape.serialization.get("timestampFormat")
def _serialize_type_timestamp(
self, body: dict, value: Any, shape: Shape, key: str, mime_type: str
):
timestamp_format = (
shape.serialization.get("timestampFormat")
# CBOR always uses unix timestamp milliseconds
if mime_type not in self.CBOR_TYPES
else "unixtimestampmillis"
)
body[key] = self._convert_timestamp_to_str(value, timestamp_format)

def _serialize_type_blob(
self, body: dict, value: Union[str, bytes], _, key: str, mime_type: str
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/test_kinesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ def test_get_records(self, kinesis_client, kinesis_create_stream, wait_for_strea
assert select_attributes(json_records[0], attrs) == select_attributes(
result["Records"][0], attrs
)
# ensure that the CBOR datetime format is unix timestamp millis
assert (
int(json_records[0]["ApproximateArrivalTimestamp"].timestamp() * 1000)
== result["Records"][0]["ApproximateArrivalTimestamp"]
)

def test_get_records_empty_stream(
self, kinesis_client, kinesis_create_stream, wait_for_stream_ready
Expand Down
0