Repository navigation
fix: honor reference detection defaults in compatible JSON instances #7933
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
Open
hutiefang76
wants to merge
1
commit into
alibaba:main
Choose a base branch
from
hutiefang76:frank/fastjson7932-default-features-20261026
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+211
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
fastjson1-compatible/src/test/java/com/alibaba/fastjson/issue_7900/Issue7932.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| package com.alibaba.fastjson.issue_7900; | ||
|
|
||
| import com.alibaba.fastjson.JSON; | ||
| import com.alibaba.fastjson.JSONArray; | ||
| import com.alibaba.fastjson.JSONObject; | ||
| import com.alibaba.fastjson.serializer.SerializerFeature; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| public class Issue7932 { | ||
| static final String OBJECT_NO_REF = "{\"listA\":[\"a\",\"b\"],\"listB\":[\"a\",\"b\"]}"; | ||
| static final String OBJECT_REF = "{\"listA\":[\"a\",\"b\"],\"listB\":{\"$ref\":\"$.listA\"}}"; | ||
| static final String ARRAY_NO_REF = "[[\"a\",\"b\"],[\"a\",\"b\"]]"; | ||
| static final String ARRAY_REF = "[[\"a\",\"b\"],{\"$ref\":\"$[0]\"}]"; | ||
| static final String OBJECT_UNQUOTED_REF = "{listA:[\"a\",\"b\"],listB:{\"$ref\":\"$.listA\"}}"; | ||
| static final String OVERRIDE_DOC = "{\"overridden\":true}"; | ||
|
|
||
| static class CustomObject extends JSONObject { | ||
| CustomObject() { | ||
| super(true); | ||
| } | ||
| } | ||
|
|
||
| static class CustomArray extends JSONArray { | ||
| } | ||
|
|
||
| static class OverrideObject extends JSONObject { | ||
| OverrideObject() { | ||
| super(true); | ||
| } | ||
|
|
||
| @Override | ||
| public String toJSONString() { | ||
| return OVERRIDE_DOC; | ||
| } | ||
| } | ||
|
|
||
| static List<String> sharedList() { | ||
| return new ArrayList<String>(Arrays.asList("a", "b")); | ||
| } | ||
|
|
||
| static void putSharedList(JSONObject object) { | ||
| List<String> shared = sharedList(); | ||
| object.put("listA", shared); | ||
| object.put("listB", shared); | ||
| assertSame(shared, object.get("listA")); | ||
| assertSame(shared, object.get("listB")); | ||
| } | ||
|
|
||
| static void addSharedList(JSONArray array) { | ||
| List<String> shared = sharedList(); | ||
| array.add(shared); | ||
| array.add(shared); | ||
| assertSame(shared, array.get(0)); | ||
| assertSame(shared, array.get(1)); | ||
| } | ||
|
|
||
| static JSONObject objectWithSharedList() { | ||
| JSONObject object = new JSONObject(true); | ||
| putSharedList(object); | ||
| return object; | ||
| } | ||
|
|
||
| static JSONArray arrayWithSharedList() { | ||
| JSONArray array = new JSONArray(); | ||
| addSharedList(array); | ||
| return array; | ||
| } | ||
|
|
||
| @Test | ||
| public void disabledJSONObjectDefaults() { | ||
| int features = JSON.DEFAULT_GENERATE_FEATURE; | ||
| try { | ||
| JSON.DEFAULT_GENERATE_FEATURE = features | SerializerFeature.DisableCircularReferenceDetect.getMask(); | ||
| JSONObject object = objectWithSharedList(); | ||
|
|
||
| assertEquals(OBJECT_NO_REF, JSON.toJSONString(object), "static entry point"); | ||
| assertEquals(OBJECT_NO_REF, object.toJSONString(), "JSON#toJSONString()"); | ||
| assertEquals(OBJECT_NO_REF, object.toString(), "JSONObject#toString()"); | ||
| } finally { | ||
| JSON.DEFAULT_GENERATE_FEATURE = features; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void disabledJSONArrayDefaults() { | ||
| int features = JSON.DEFAULT_GENERATE_FEATURE; | ||
| try { | ||
| JSON.DEFAULT_GENERATE_FEATURE = features | SerializerFeature.DisableCircularReferenceDetect.getMask(); | ||
| JSONArray array = arrayWithSharedList(); | ||
|
|
||
| assertEquals(ARRAY_NO_REF, JSON.toJSONString(array), "static entry point"); | ||
| assertEquals(ARRAY_NO_REF, array.toString(), "JSONArray#toString()"); | ||
| assertEquals(ARRAY_NO_REF, array.toJSONString(), "inherited JSON#toJSONString()"); | ||
| } finally { | ||
| JSON.DEFAULT_GENERATE_FEATURE = features; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void defaultReferenceBehavior() { | ||
| int features = JSON.DEFAULT_GENERATE_FEATURE; | ||
| try { | ||
| JSON.DEFAULT_GENERATE_FEATURE = features & ~SerializerFeature.DisableCircularReferenceDetect.getMask(); | ||
|
|
||
| JSONObject object = objectWithSharedList(); | ||
| String objectStatic = JSON.toJSONString(object); | ||
| assertEquals(OBJECT_REF, objectStatic, "static entry point"); | ||
| assertTrue(objectStatic.contains("$ref")); | ||
| assertEquals(objectStatic, object.toJSONString(), "JSON#toJSONString()"); | ||
| assertEquals(objectStatic, object.toString(), "JSONObject#toString()"); | ||
|
|
||
| JSONArray array = arrayWithSharedList(); | ||
| String arrayStatic = JSON.toJSONString(array); | ||
| assertEquals(ARRAY_REF, arrayStatic, "static entry point"); | ||
| assertEquals(arrayStatic, array.toString(), "JSONArray#toString()"); | ||
| assertEquals(arrayStatic, array.toJSONString(), "inherited JSON#toJSONString()"); | ||
| } finally { | ||
| JSON.DEFAULT_GENERATE_FEATURE = features; | ||
| } | ||
| } | ||
|
|
||
| // unrelated-feature control: only DisableCircularReferenceDetect is honored by the instance paths | ||
| @Test | ||
| public void unrelatedQuoteFieldNamesUnchanged() { | ||
| int features = JSON.DEFAULT_GENERATE_FEATURE; | ||
| try { | ||
| JSON.DEFAULT_GENERATE_FEATURE = features & ~SerializerFeature.QuoteFieldNames.getMask(); | ||
| JSONObject object = objectWithSharedList(); | ||
|
|
||
| assertEquals(OBJECT_UNQUOTED_REF, JSON.toJSONString(object), "static entry point honors QuoteFieldNames"); | ||
| assertEquals(OBJECT_REF, object.toJSONString(), "JSON#toJSONString() keeps core quoting"); | ||
| assertEquals(OBJECT_REF, object.toString(), "JSONObject#toString() keeps core quoting"); | ||
| } finally { | ||
| JSON.DEFAULT_GENERATE_FEATURE = features; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void customJSONObjectSubclass() { | ||
| int features = JSON.DEFAULT_GENERATE_FEATURE; | ||
| try { | ||
| JSON.DEFAULT_GENERATE_FEATURE = features | SerializerFeature.DisableCircularReferenceDetect.getMask(); | ||
| CustomObject object = new CustomObject(); | ||
| putSharedList(object); | ||
| assertEquals(OBJECT_NO_REF, JSON.toJSONString(object), "static entry point"); | ||
| assertEquals(OBJECT_NO_REF, object.toJSONString(), "JSON#toJSONString()"); | ||
| assertEquals(OBJECT_NO_REF, object.toString(), "JSONObject#toString()"); | ||
|
|
||
| JSON.DEFAULT_GENERATE_FEATURE = features & ~SerializerFeature.DisableCircularReferenceDetect.getMask(); | ||
| CustomObject detected = new CustomObject(); | ||
| putSharedList(detected); | ||
| assertEquals(OBJECT_REF, JSON.toJSONString(detected), "static entry point"); | ||
| assertEquals(OBJECT_REF, detected.toJSONString(), "JSON#toJSONString()"); | ||
| assertEquals(OBJECT_REF, detected.toString(), "JSONObject#toString()"); | ||
| } finally { | ||
| JSON.DEFAULT_GENERATE_FEATURE = features; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void customJSONArraySubclass() { | ||
| int features = JSON.DEFAULT_GENERATE_FEATURE; | ||
| try { | ||
| JSON.DEFAULT_GENERATE_FEATURE = features | SerializerFeature.DisableCircularReferenceDetect.getMask(); | ||
| CustomArray array = new CustomArray(); | ||
| addSharedList(array); | ||
| assertEquals(ARRAY_NO_REF, JSON.toJSONString(array), "static entry point"); | ||
| assertEquals(ARRAY_NO_REF, array.toString(), "JSONArray#toString()"); | ||
| assertEquals(ARRAY_NO_REF, array.toJSONString(), "inherited JSON#toJSONString()"); | ||
|
|
||
| JSON.DEFAULT_GENERATE_FEATURE = features & ~SerializerFeature.DisableCircularReferenceDetect.getMask(); | ||
| CustomArray detected = new CustomArray(); | ||
| addSharedList(detected); | ||
| assertEquals(ARRAY_REF, JSON.toJSONString(detected), "static entry point"); | ||
| assertEquals(ARRAY_REF, detected.toString(), "JSONArray#toString()"); | ||
| assertEquals(ARRAY_REF, detected.toJSONString(), "inherited JSON#toJSONString()"); | ||
| } finally { | ||
| JSON.DEFAULT_GENERATE_FEATURE = features; | ||
| } | ||
| } | ||
|
|
||
| // recursion guard: JSONAware static route keeps the override, toString() goes through super | ||
| @Test | ||
| public void customOverrideToJSONString() { | ||
| int features = JSON.DEFAULT_GENERATE_FEATURE; | ||
| try { | ||
| JSON.DEFAULT_GENERATE_FEATURE = features | SerializerFeature.DisableCircularReferenceDetect.getMask(); | ||
| OverrideObject object = new OverrideObject(); | ||
| putSharedList(object); | ||
|
|
||
| assertEquals(OVERRIDE_DOC, JSON.toJSONString(object), "static JSONAware route"); | ||
| assertEquals(OVERRIDE_DOC, object.toJSONString(), "override kept"); | ||
| assertEquals(OBJECT_NO_REF, object.toString(), "JSONObject#toString() uses super"); | ||
| } finally { | ||
| JSON.DEFAULT_GENERATE_FEATURE = features; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是不是直接调用jSON.tojosnstring(this)更好
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you mean
com.alibaba.fastjson.JSON.toJSONString(this), I checked it against the current implementation: the calls are not equivalent.The compatibility provider uses
Fastjson1xWriterModule. For aJSONObjectorJSONArraysubclass, that writer callsJSONAware.toJSONString()again. In a standalone JDK 21 probe, giving either subclass the suggested delegating method producesStackOverflowErrorin both standard and reflect modes. The exact base classes work, which is why the simple object/array examples do not expose it.The static compatibility call also changes the output for an overridden
toJSONString(),QuoteFieldNames,WriteMapNullValueand a custom compatibility writer. The current implementation already delegates tocom.alibaba.fastjson2.JSON.toJSONString(...); it selects the overload forDisableCircularReferenceDetectwhile preserving the existing provider and other behavior. I have kept that narrower change here.中文
如果指的是兼容层
com.alibaba.fastjson.JSON.toJSONString(this),实测并不等价。它的 writer 会对JSONObject/JSONArray子类回调JSONAware.toJSONString();给子类换成这个委托实现后,独立 JDK 21 探针在普通和 reflect 模式都出现栈溢出。直接使用两个基类则正常,因此简单示例看不出这个差异。静态兼容入口还会改变自定义
toJSONString()、QuoteFieldNames、WriteMapNullValue和兼容层自定义 writer 的输出。当前实现已经调用 core 的com.alibaba.fastjson2.JSON.toJSONString(...),只是按DisableCircularReferenceDetect选择重载,保留原 provider 和其他行为,所以这里先保留这个较小的修复。