Skip to content

fix: fix the Converting circular structure to JSON error #13482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/shared/src/toDisplayString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@ const isRef = (val: any): val is { value: unknown } => {
return !!(val && val[ReactiveFlags.IS_REF] === true)
}

const isVNodeLike = (val: unknown): boolean =>
!!(val && (val as any).__v_isVNode === true)
/**
* For converting {{ interpolation }} values to displayed strings.
* @private
*/
export const toDisplayString = (val: unknown): string => {
// fix: https://github.com/vuejs/core/issues/13481
if(isVNodeLike(val)){
if(__DEV__){
console.warn('[Vue warn]: Interpolated value is a VNode – this is unsupported and was likely passed by mistake. Returning an empty string.');
}
return '';
}
Comment on lines +27 to +33
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

⚠️ Potential issue

Style nits aside, nested VNodes can still explode JSON.stringify

  1. Minor: the current formatting (if(__DEV__){) diverges from the rest of the file (if (__DEV__) {).
  2. Major: the early return only protects when the root value is a VNode.
      toDisplayString([h('div')]) will still reach JSON.stringify and throw the exact same “Converting circular structure to JSON” error because replacer does not recognise VNodes.

Proposed fix (includes style clean-up + VNode handling in replacer):

@@
-    // fix: https://github.com/vuejs/core/issues/13481
-    if(isVNodeLike(val)){
-      if(__DEV__){
-        console.warn('[Vue warn]: Interpolated value is a VNode – this is unsupported and was likely passed by mistake. Returning an empty string.');
-      }
-      return '';
-   }
+  // #13481 – avoid JSON-stringifying VNodes (circular refs)
+  if (isVNodeLike(val)) {
+    if (__DEV__) {
+      console.warn(
+        '[Vue warn]: Interpolated value is a VNode – this is unsupported ' +
+        'and was likely passed by mistake. Returning an empty string.'
+      )
+    }
+    return ''
+  }
@@
   if (isRef(val)) {
     return replacer(_key, val.value)
+  } else if (isVNodeLike(val)) {
+    // Prevent circular reference blow-ups when VNodes are nested inside
+    // arrays / objects passed to toDisplayString.
+    return '[VNode]'
   } else if (isMap(val)) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// fix: https://github.com/vuejs/core/issues/13481
if(isVNodeLike(val)){
if(__DEV__){
console.warn('[Vue warn]: Interpolated value is a VNode – this is unsupported and was likely passed by mistake. Returning an empty string.');
}
return '';
}
// #13481 – avoid JSON-stringifying VNodes (circular refs)
if (isVNodeLike(val)) {
if (__DEV__) {
console.warn(
'[Vue warn]: Interpolated value is a VNode – this is unsupported ' +
'and was likely passed by mistake. Returning an empty string.'
)
}
return ''
}
if (isRef(val)) {
return replacer(_key, val.value)
} else if (isVNodeLike(val)) {
// Prevent circular reference blow-ups when VNodes are nested inside
// arrays / objects passed to toDisplayString.
return '[VNode]'
} else if (isMap(val)) {
// ...
}
🤖 Prompt for AI Agents
In packages/shared/src/toDisplayString.ts around lines 27 to 33, fix the style
of the if statement to match the file's convention by adding spaces: change
`if(__DEV__){` to `if (__DEV__) {`. Also, extend the VNode handling by updating
the JSON.stringify replacer function to detect and handle nested VNodes,
preventing circular structure errors when VNodes appear inside arrays or
objects. This involves adding a check for VNode-like objects within the replacer
and returning an empty string for them, ensuring no VNode causes JSON.stringify
to throw.

return isString(val)
? val
: val == null
Expand Down