Skip to content

fix(web-core): JSON-stringify objects/arrays in formatString per spec#1430

Open
BBC6BAE9 wants to merge 5 commits into
google:mainfrom
BBC6BAE9:bugfix/string_display
Open

fix(web-core): JSON-stringify objects/arrays in formatString per spec#1430
BBC6BAE9 wants to merge 5 commits into
google:mainfrom
BBC6BAE9:bugfix/string_display

Conversation

@BBC6BAE9
Copy link
Copy Markdown
Contributor

@BBC6BAE9 BBC6BAE9 commented May 14, 2026

Description

FormatStringImplementation used Array.prototype.join('') which relied on JS default String(value) coercion, producing [object Object] for objects and flattened comma-separated values for arrays. The a2ui_protocol.md §"Type conversion" requires objects/arrays to be stringified as JSON.

Changes:

  • Add coerceToString helper that implements the spec's type conversion rules: null/undefined"", objects/arrays → JSON.stringify(), scalars → String()
  • Update FormatStringImplementation to use coerceToString
  • Add 5 unit tests covering object, array, nested array, array-with-null, and null interpolation
  • Update CHANGELOG

Reproduction (from issue):

Template Spec says web_core before fix
"Tags: ${/tags}" Tags: ["swift","ios"] Tags: swift,ios
"User: ${/user}" User: {"name":"Alice","age":30} User: [object Object]
"M = ${/matrix}" M = [[1,2],[3,4]] M = 1,2,3,4
"V = ${/vals}" V = [1,null,3] V = 1,,3

Fixes #1386

Pre-launch Checklist

If you need help, consider asking for advice on the discussion board.

@google-cla
Copy link
Copy Markdown

google-cla Bot commented May 14, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the formatString function to align with the A2UI protocol by JSON-stringifying objects and arrays and converting null/undefined values to empty strings. A potential issue was identified where JSON.stringify could cause the UI to crash if it encounters circular references or BigInt values; a more robust implementation with error handling and a fallback was suggested.

Comment thread renderers/web_core/src/v0_9/basic_catalog/functions/basic_functions.ts Outdated
@BBC6BAE9 BBC6BAE9 force-pushed the bugfix/string_display branch from b749dcc to befef6b Compare May 14, 2026 05:52
FormatStringImplementation used Array.prototype.join('') which relied on
JS default String(value) coercion, producing '[object Object]' for objects
and flattened comma-separated values for arrays. The a2ui_protocol.md
§"Type conversion" requires objects/arrays to be stringified as JSON.

Fixes google#1386
@BBC6BAE9 BBC6BAE9 force-pushed the bugfix/string_display branch from befef6b to 800e435 Compare May 15, 2026 04:03
Wrap JSON.stringify in try-catch to handle circular references and
BigInt values gracefully, falling back to String(value) on failure.
Also use nullish coalescing to guard against toJSON returning undefined.
@BBC6BAE9 BBC6BAE9 force-pushed the bugfix/string_display branch from 800e435 to 0031462 Compare May 15, 2026 04:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

[web_core] formatString should JSON-stringify objects/arrays per spec

2 participants