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

Fixes #632: Detect cycles when constructing a JSONObject #633

Closed
wants to merge 8 commits into from

Conversation

guptnis
Copy link

@guptnis guptnis commented Oct 14, 2021

  1. Added a method layer to check before converting an object to a JsonObject if there exists a cyclical dependency somewhere.
  2. If yes, throws Exception
  3. Changes to testGenericBean and testGenericIntBean. The "get" method is now expected to be called twice.

@guptnis guptnis changed the title Fixes #632 Fixes Issue #632 Oct 14, 2021
@guptnis guptnis changed the title Fixes Issue #632 fixes Issue #632 Oct 14, 2021
Copy link
Contributor

@johnjaylward johnjaylward left a comment

Choose a reason for hiding this comment

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

Please undo the formatting changes and have your code changes match the current project formatting.

Copy link
Contributor

@johnjaylward johnjaylward left a comment

Choose a reason for hiding this comment

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

There are still a lot of unrelated changes due to white space differences

@guptnis
Copy link
Author

guptnis commented Oct 14, 2021

@johnjaylward I did not add any spaces/indents. I think intellij is doing it by default. Any pointers on how to prevent intellij from changing the formatting?

@johnjaylward
Copy link
Contributor

I don't use intellij, but perhaps the suggestions here may help you: https://stackoverflow.com/questions/30045874/how-to-disable-intellij-auto-change-my-code-format

@stleary stleary changed the title fixes Issue #632 Fixes #632: Detect cycles when constructing a JSONObject Oct 14, 2021
@guptnis
Copy link
Author

guptnis commented Oct 14, 2021

@johnjaylward @stleary corrected the file indentations

Copy link
Contributor

@johnjaylward johnjaylward left a comment

Choose a reason for hiding this comment

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

Overall I think your approach is in the right direction, however I think it can use some optimization.

  1. For bean processing, your check makes us cycle through the entire bean twice, which seems unnecessary. If you instead just keep the setOfInstanceVariables locally for the bean check, you can keep everything in the 1 loop and just throw the exception immediately from the building of the JSON object instead or pre-processing.
  2. For the map processing, I feel like there is a gap in the logic there. I think more use-cases need to be thought of for a complete view here. I don't see any test cases to confirm that the changes in place do what's expected. i.e Map_A -> Map_B -> Map_C -> Map_A would throw an exception or Map_A -> Bean_B -> Bean_C -> Map_A throws an exception
  3. Overall on the testing, I'd like to see a few more thorough tests. The 2 test cases provided are very simple and don't cover some more complex mapping that may be cyclical. For example Bean_A -> Bean_B -> Bean_C -> Bean_A

Another test that should not throw an exception may be something with a structure like this:

       Bean_A
      /      \
Bean_B       Bean_C
                \
              Bean_B

Even though Bean_B shows up twice in the graph, it's not cyclical

// we don't use the result anywhere outside of wrap
// if it's a resource we should be sure to close it
// after calling toString
if (result instanceof Closeable) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This processing the close of the value may not be safe here since we are not using the value at this time. If for some reason the value does need to be used after this method is called, it may not be available since it's already been closed by this method.

@@ -296,6 +297,7 @@ public JSONObject(Map<?, ?> m) {
}
final Object value = e.getValue();
if (value != null) {
checkForCyclicDependency(value);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not confident this is doing what you expect and I fear this is adding a lot of overhead for deep maps. This appears to check every child every time a new depth is processed instead of checking the entire map 1 time.

Also, the "cyclic" check doesn't seem to take into account the use of sub-maps.

@stleary
Copy link
Owner

stleary commented Nov 18, 2021

Closing this PR since an alternate solution has been accepted.

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

Successfully merging this pull request may close these issues.

3 participants