Skip to content

fix(idempotency): serialize UUID and datetime fields in dataclass payloads - #8501

Merged
leandrodamascena merged 2 commits into
aws-powertools:developfrom
Om-singhaI:fix/idempotency-dataclass-json-serialization
Sep 29, 2026
Merged

leandrodamascena merged 2 commits into
aws-powertools:developfrom
Om-singhaI:fix/idempotency-dataclass-json-serialization

Conversation

@Om-singhaI

@Om-singhaI Om-singhaI commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Issue number: closes #8500

Summary

Changes

Adds an encoder used only when generating idempotency key and payload validation hashes. It supports UUID, date, datetime, time, plain Enum, and path values while giving the existing encoder priority. This preserves existing representations, including enums based on Decimal.

Dataclass inputs still go through dataclasses.asdict(). The additional conversions happen after event_key_jmespath and payload_validation_jmespath are evaluated, so built-in and custom functions receive the original field types. Dictionary inputs use the same hash encoder. Response serialization is unchanged.

User experience

A dataclass containing UUID or datetime fields currently fails while hashing the input, before the function runs. These inputs can now be processed and subsequent calls reuse the saved result.

Compatibility tests assert the exact DynamoDB keys and validation hashes for previously supported inputs, then return an existing record without executing the function. Additional tests cover replay with local caching enabled and disabled, changed-payload rejection, and unsupported types.

Validation

  • Python 3.14: 191 idempotency tests passed, including DynamoDB, Pydantic, and Redis cases.
  • Python 3.10 without Pydantic installed: 149 tests passed.
  • Five compatibility regression cases fail on the original PR and pass with the follow-up.
  • Idempotency coverage: 98.30%; the new encoder has 100% line and branch coverage.
  • Ruff, mypy, Bandit, complexity checks, and Markdown lint passed.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

…loads

aws-powertools#8075 made _prepare_data dump Pydantic models with mode="json", but the
dataclass branch still returned the raw dataclasses.asdict() output. A
dataclass holding a UUID, date, time, datetime, plain Enum or path
failed on every call with "Object of type UUID is not JSON serializable"
while hashing the idempotency key.

Convert those values to JSON safe ones after asdict(). Values json
already encodes, including str and int enums, are left as they are, so
keys for dataclasses that worked before don't change.
@Om-singhaI
Om-singhaI requested a review from a team as a code owner September 29, 2026 00:51
@boring-cyborg boring-cyborg Bot added the tests label Sep 29, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Sep 29, 2026
@boring-cyborg boring-cyborg Bot added the documentation Improvements or additions to documentation label Sep 29, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Sep 29, 2026
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.80%. Comparing base (562cd2c) to head (8686fc6).
⚠️ Report is 2 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #8501   +/-   ##
========================================
  Coverage    96.80%   96.80%           
========================================
  Files          310      310           
  Lines        15579    15594   +15     
  Branches      1358     1361    +3     
========================================
+ Hits         15081    15096   +15     
  Misses         362      362           
  Partials       136      136           

☔ View full report in Codecov by Harness.
📢 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.

@leandrodamascena leandrodamascena left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this, @Om-singhaI! I pushed a follow-up that moves the conversion to the hashing step, after JMESPath evaluation. This preserves existing encodings, including Decimal-based enums, and lets expressions and custom functions keep working with the original types.

I also added regression tests to verify that existing records are reused without executing the function again.

@mergify

mergify Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again.

@leandrodamascena
leandrodamascena merged commit ed6535d into aws-powertools:develop Sep 29, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: @idempotent_function fails for dataclasses with UUID or datetime fields

2 participants