Skip to content

fix: handle intrinsic functions in WebSocket API StageName property#3920

Open
licjun wants to merge 3 commits intodevelopfrom
fix/websocket-intrinsic-support
Open

fix: handle intrinsic functions in WebSocket API StageName property#3920
licjun wants to merge 3 commits intodevelopfrom
fix/websocket-intrinsic-support

Conversation

@licjun
Copy link
Copy Markdown
Contributor

@licjun licjun commented May 5, 2026

Problem

When StageName is set to an intrinsic function (e.g. !Ref Param), SAM Translator crashes with:

TypeError: can only concatenate str (not "dict") to str

This happens in _construct_permission where self.stage_name is concatenated directly into a string to build the SourceArn.

Root Cause

Intrinsic functions like {"Ref": "Param"} are Python dicts. String concatenation with + fails when self.stage_name is a dict instead of a str. The validate_properties() bypass for intrinsics means the dict reaches the generator code without being caught.

Fix

Use isinstance(self.stage_name, str) to branch:

  • Plain string: keep original behavior (no change to existing CloudFormation templates)
  • Intrinsic: use fnSub variable substitution so CloudFormation resolves it at deploy time
if isinstance(self.stage_name, str):
    perms.SourceArn = fnSub(".../" + self.stage_name + "/...")
else:
    perms.SourceArn = fnSub(".../${__StageName__}/...", {"__StageName__": self.stage_name})

Also updated:

  • property_types for StageName to accept one_of(IS_STR, IS_DICT) for better error messages (consistent with HttpApi)
  • Type annotation to Intrinsicable[str] | None (consistent with HttpApi)
  • stage_name parameter type in WebSocketApiGenerator.__init__ to Intrinsicable[str] | None

Testing

Added test_perms_with_intrinsic_stage_name unit test verifying the fix works for intrinsic StageName.

licjun added 3 commits May 4, 2026 15:24
When StageName is set to an intrinsic function (e.g. !Ref Param),
string concatenation in _construct_permission caused TypeError.

Fix: use fnSub variable substitution when stage_name is an intrinsic,
preserving the original string format for plain string values.

Also update property_types to accept one_of(IS_STR, IS_DICT) and
update type annotation to Intrinsicable[str] for consistency.
@licjun licjun requested a review from a team as a code owner May 5, 2026 23:51
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