Skip to content

fix(dynamodb): migrate to ActionResult serialization and fix timestamp/binary handling#9773

Open
yusuke-koyoshi wants to merge 5 commits intogetmoto:masterfrom
yusuke-koyoshi:fix/timestamp-dynamodb
Open

fix(dynamodb): migrate to ActionResult serialization and fix timestamp/binary handling#9773
yusuke-koyoshi wants to merge 5 commits intogetmoto:masterfrom
yusuke-koyoshi:fix/timestamp-dynamodb

Conversation

@yusuke-koyoshi
Copy link
Contributor

@yusuke-koyoshi yusuke-koyoshi commented Feb 22, 2026

Summary

Migrates all DynamoDB responses from dynamo_json_dump() / json.dumps() to ActionResult serialization, fixing timestamp and binary value handling.

Changes

Timestamp handling improvements

  • moto/dynamodb/models/table.py: Remove premature unix_time() / isoformat() conversions; keep raw datetime objects and let the ActionResult serialization layer handle conversion uniformly
  • moto/awslambda/models.py: Convert datetime → epoch at the Lambda event payload boundary, since Lambda events require epoch timestamps

Unified response serialization

  • moto/dynamodb/responses.py: Change all response method return types from str to ActionResult, replacing dynamo_json_dump() / json.dumps() with ActionResult() + dynamo_to_dict()
  • Update include_consumed_capacity decorator to work with ActionResult
  • Replace inline validation error response tuples with MockValidationException

New dynamo_to_dict() helper

  • moto/dynamodb/models/utilities.py: Add dynamo_to_dict() that recursively converts DynamoDB model objects to plain Python types
    • Preserves DynamoType B/BS values as raw bytes for correct base64 encoding by ActionResult serializer
    • Preserves datetime objects for serialization layer conversion

Behavior fixes

  • Stop returning empty Attributes: {} when ReturnValues=NONE; omit the Attributes key entirely to match actual AWS behavior
  • Fix update_item with ReturnValues=ALL_NEW to return attributes correctly

Tests

  • Add test for binary attribute (B/BS) serialization round-trip through put_item, delete_item, and update_item with ReturnValues
  • Fix assertions for stream record timestamp and size changes

…rializer

StreamRecord.ApproximateCreationDateTime, Table.CreationDateTime,
Backup.BackupCreationDateTime, and RestoreDateTime now store raw
datetime objects instead of pre-converted epoch/isoformat values,
letting the ActionResult serializer handle the conversion.

Added dynamo_to_dict() to recursively convert DynamoDB model objects
(DynamoType, to_json objects) to plain dicts while preserving datetime
for ActionResult and decoding B/BS to raw bytes to avoid double
base64 encoding.

Updated DynamoJsonEncoder to handle datetime natively (for SizeBytes
calculation) and reordered type checks so datetime is checked before
to_json in both DynamoJsonEncoder and dynamo_to_dict for consistency.
Replace dynamo_json_dump()/json.dumps() with ActionResult() across
all 38 handler methods in responses.py, letting the core serializer
handle JSON encoding and timestamp formatting.

Refactored include_consumed_capacity decorator to work with
ActionResult directly and use MockValidationException instead of
manual HTTP tuple construction.

Fixed ReturnValues=NONE behavior for put_item, delete_item, and
update_item to not include an Attributes key, matching AWS behavior.
Added explicit ALL_NEW handling in update_item.
Fixed send_dynamodb_items in awslambda to call item.to_json() once
instead of 5 times, and explicitly convert datetime to epoch at the
Lambda event payload boundary.

Added test_binary_attr_put_delete_update_return_values covering B/BS
types through put_item, delete_item, and update_item with various
ReturnValues options.

Updated existing tests: freeze_time for SizeBytes stability,
ReturnValues=NONE assertions to expect no Attributes key.
The long line in dynamo_to_dict was failing the ruff format --check
in the CI lint job. Split it into multiple lines to comply with the
formatting rules.
@codecov
Copy link

codecov bot commented Feb 24, 2026

Codecov Report

❌ Patch coverage is 98.55072% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.11%. Comparing base (7b5597b) to head (bbdf73a).

Files with missing lines Patch % Lines
moto/dynamodb/models/utilities.py 91.66% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9773      +/-   ##
==========================================
- Coverage   93.11%   93.11%   -0.01%     
==========================================
  Files        1308     1308              
  Lines      118845   118872      +27     
==========================================
+ Hits       110665   110686      +21     
- Misses       8180     8186       +6     
Flag Coverage Δ
servertests 28.88% <35.50%> (-0.01%) ⬇️
unittests 93.09% <98.55%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants