Skip to content
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

StackOverflowError Due to Circular Reference in JSON Serialization #129

Closed
testmigrator opened this issue Mar 6, 2025 · 2 comments
Closed

Comments

@testmigrator
Copy link

While testing the handling of circular references during JSON serialization, I encountered a StackOverflowError when attempting to serialize a HashMap containing a circular reference via an ArrayList.

    @Test
    public void testCircularReferenceHandling() {
        // Create a dictionary with a circular reference by appending the dictionary to a list it contains.
        java.util.HashMap<String, Object> dict = new java.util.HashMap<>();
        java.util.ArrayList<Object> list = new java.util.ArrayList<>();
        dict.put("list", list);
        list.add(dict);

        // Attempt to serialize the dictionary using a JSON serialization method.
        JsonWriter writer = new JsonWriter();
        StringWriter stringWriter = new StringWriter();

        try {
            writer.on(stringWriter).value(dict);
            fail("Expected a ValueError to be raised during serialization of dictionary with circular references.");
        } catch (IllegalArgumentException e) {
        }
    }

Proposed Solution:

To address this issue, the JsonWriter should implement a mechanism to track visited objects during serialization. If a circular reference is detected, an exception should be thrown to prevent infinite recursion and provide a clear error message.

@mmastrac mmastrac closed this as not planned Won't fix, can't repro, duplicate, stale Mar 6, 2025
@testmigrator
Copy link
Author

testmigrator commented Mar 8, 2025

Hi!~

Thank you for taking the time to review my issue. I understand it was marked as "not planned," but I’d like to clarify if this is indeed an issue or if it’s due to my misunderstanding of the library’s implementation.

I’m working on a test migration experiment, and the test case is based on a real test from the Java Gson library. I’d greatly appreciate your simple feedback on this.

Thank you for your work on this project, and I look forward to your response!

@testmigrator
Copy link
Author

I’m currently working on an experiment involving test code migration and have spent significant time verifying whether these tests can provide some positive impact for the target library. Your feedback would be very valuable and helpful for my work.

I sincerely hope to hear from you. Thank you so much for your time and consideration!

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

No branches or pull requests

2 participants