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
Issue 652 cycle detection doesnt catch cycles through collections #699
Closed
hendrixjoseph
wants to merge
5
commits into
stleary:master
from
hendrixjoseph:issue-652-cycle-detection-doesnt-catch-cycles-through-collections
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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
75 changes: 75 additions & 0 deletions
75
src/test/java/org/json/junit/JSONObjectRecursiveExceptionTest.java
This file contains 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,75 @@ | ||
package org.json.junit; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.hamcrest.core.StringContains; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
import org.junit.Before; | ||
import org.junit.Ignore; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
public class JSONObjectRecursiveExceptionTest { | ||
public static class RecursiveList { | ||
public List<RecursiveList> getRecursiveList() { | ||
return Collections.singletonList(this); | ||
} | ||
} | ||
|
||
public static class RecursiveArray { | ||
public RecursiveArray[] getRecursiveArray() { | ||
return new RecursiveArray[] {this}; | ||
} | ||
} | ||
|
||
public static class RecursiveMap { | ||
public Map<String, RecursiveMap> getRecursiveMap() { | ||
Map<String, RecursiveMap> map = new HashMap<String, RecursiveMap>(); | ||
map.put("test", this); | ||
return map; | ||
} | ||
} | ||
|
||
public static class Recursive { | ||
public Recursive getSelf() { | ||
return this; | ||
} | ||
} | ||
|
||
@Rule | ||
public ExpectedException exceptionRule = ExpectedException.none(); | ||
|
||
@Before | ||
public void before() { | ||
exceptionRule.expect(JSONException.class); | ||
exceptionRule.expectMessage(StringContains.containsString("JavaBean object contains recursively defined member variable of key")); | ||
} | ||
|
||
|
||
@Test | ||
public void testRecursiveList() { | ||
new JSONObject(new RecursiveList()); | ||
} | ||
|
||
@Ignore | ||
@Test | ||
public void testRecursiveArray() { | ||
new JSONObject(new RecursiveArray()); | ||
} | ||
|
||
@Ignore | ||
@Test | ||
public void testRecursiveMap() { | ||
new JSONObject(new RecursiveMap()); | ||
} | ||
|
||
@Test | ||
public void testRecursive() { | ||
new JSONObject(new Recursive()); | ||
} | ||
} |
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.
Why?
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.
Why the
new JSONObject(new RecursiveList());
or why the@Ignore
?The
new JSONObject(new RecursiveList());
is testing if it throws the right exception if the list contains the object which has the list.The
@Ignore
(and there's two of them) are for two additional locations where I identified stack overflows when having the object-within-something - one case is within a Map, one case is within an array. Since I didn't fix them as part of this PR (I felt it was out of scope), I added the@Ignore
annotation so I could show where the stack overflows were happening, but not have the test fail.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.
Why do we need code that will be ignored? It may be an exception check or just remove it.