Skip to content

Allow ClientContext.Custom unmarshaling for non-string (JSON) values#620

Open
M-Elsaeed wants to merge 2 commits intomainfrom
moehabe/supportjsonpayloads
Open

Allow ClientContext.Custom unmarshaling for non-string (JSON) values#620
M-Elsaeed wants to merge 2 commits intomainfrom
moehabe/supportjsonpayloads

Conversation

@M-Elsaeed
Copy link

Issue #, if available: -

Description of changes:

When AWS services like Bedrock Agentcore Gateway send nested JSON objects as values in ClientContext.custom (e.g. propagated headers), the Go Lambda runtime fails with: cannot unmarshal object into Go struct field ClientContext.custom of type string. This blocks all Go Lambda functions used with Agentcore Gateway propagated headers.

Root cause
ClientContext.Custom is map[string]string, which rejects non-string JSON values during json.Unmarshal in parseClientContext() before the handler is ever invoked.

How this Fixes that
Add a custom UnmarshalJSON to ClientContext that parses Custom values via json.RawMessage. String values are stored directly (backward compatible) while Non-string values (objects, arrays) are serialized to their JSON string representation instead of failing.

Fully backward compatible - no struct signature changes. All existing tests pass.

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

@codecov-commenter
Copy link

codecov-commenter commented Mar 4, 2026

Codecov Report

❌ Patch coverage is 0% with 18 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.99%. Comparing base (9c32960) to head (3de2206).

Files with missing lines Patch % Lines
lambdacontext/context.go 0.00% 18 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #620      +/-   ##
==========================================
- Coverage   74.94%   73.99%   -0.96%     
==========================================
  Files          36       36              
  Lines        1401     1419      +18     
==========================================
  Hits         1050     1050              
- Misses        273      291      +18     
  Partials       78       78              

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

if err := json.Unmarshal(v, &s); err == nil {
cc.Custom[k] = s
} else {
cc.Custom[k] = string(v)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe you can add a test when JSON parsing is failing? (I think a non-valid JSON will trigger that code path?)

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.

3 participants