Skip to content

fix(idempotency): keep reflection-based serialization under JIT when a serialization context is set - #1375

Merged
hjgraca merged 1 commit into
developfrom
fix/idempotency-context-reflection-fallback
Nov 26, 2026
Merged

hjgraca merged 1 commit into
developfrom
fix/idempotency-context-reflection-fallback

Conversation

@hjgraca

@hjgraca hjgraca commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Issue number: closes #1374

Summary

Changes

Under JIT, calling WithJsonSerializationContext() made Idempotency serialize only the types in the user's context. A non-empty TypeInfoResolverChain replaces System.Text.Json's default reflection resolver, so anything else failed with NotSupportedException when a record was saved or read, even a plain string.

  • IdempotencySerializer.AddTypeInfoResolver adds a DefaultJsonTypeInfoResolver after the user's context when reflection-based serialization is available. Types in the context still use the context's metadata, and other types fall back to reflection, the same as without a context.
  • The resolver is added in a small [ExcludeFromCodeCoverage] helper behind JsonSerializer.IsReflectionEnabledByDefault, the same guard as the other reflection calls in this file. ILC removes it in Native AOT and trimmed apps, so they keep using only source-generated metadata.
  • Docs: one sentence under WithJsonSerializationContext() explaining that Native AOT needs every type in the context, while JIT falls back to reflection for the rest.

User experience

  • Before: an app that registers a context for Native AOT and also runs under JIT (unit tests, or a non-AOT deployment) fails on any type missing from the context, for example:
    NotSupportedException: JsonTypeInfo metadata for type 'System.String' was not provided by TypeInfoResolver of type '[MyContext]'
  • After: under JIT those types are serialized with reflection. Native AOT is unchanged.

Testing

New SerializationContextReflectionFallbackTests:

Test Before After
Under JIT, string and a type not in the context round-trip after a context is set fails passes
Under JIT, the user's context is first in the chain and reflection is last fails passes
[Idempotent] with WithJsonSerializationContext() and a result type not in the context replays the stored result fails (NotSupportedException saving the record) passes
Types in the context still round-trip passes passes
With dynamic code unsupported (simulated AOT), no reflection resolver is added and unknown types still throw passes passes
  • dotnet test tests/AWS.Lambda.Powertools.Idempotency.Tests (with DynamoDB Local through Testcontainers): 109/109 on net8.0 and 109/109 on net10.0.
  • Full Release build from libraries/ (dotnet restore && dotnet build --configuration Release --no-restore): 0 errors.
  • Native AOT publish (.NET 10) of Idempotency apps against a local pack of this branch: 0 Powertools-owned trim/AOT warnings for Idempotency and Idempotency-usage.

Merge danger: two-way door. Blast radius: Idempotency serialization under JIT after WithJsonSerializationContext(). Native AOT and trimmed apps are unaffected.

Checklist

Please leave checklist items unchecked if they do not apply to your change.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

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

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@hjgraca hjgraca added bug Unexpected, reproducible and unintended software behaviour area/idempotency labels Nov 26, 2026
@boring-cyborg boring-cyborg Bot added documentation Improvements or additions to documentation tests labels Nov 26, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 26, 2026
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.78%. Comparing base (11b9d9f) to head (f6130c2).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1375      +/-   ##
===========================================
- Coverage    80.78%   80.78%   -0.01%     
===========================================
  Files          306      306              
  Lines        13094    13098       +4     
  Branches      1561     1562       +1     
===========================================
+ Hits         10578    10581       +3     
  Misses        2074     2074              
- Partials       442      443       +1     

☔ 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.

@hjgraca
hjgraca merged commit 3701b0a into develop Nov 26, 2026
12 of 13 checks passed
@hjgraca
hjgraca deleted the fix/idempotency-context-reflection-fallback branch November 26, 2026 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/idempotency bug Unexpected, reproducible and unintended software behaviour 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: WithJsonSerializationContext() disables reflection-based serialization in Idempotency under JIT

1 participant