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

fix: Added Non-Finite check #777

Closed
wants to merge 3 commits into from

Conversation

dubeyaditya29
Copy link

This PR fixes the missing check for non-finite values being added to the map object in JSONObject

Added a new method isFiniteNumber(Object value) returns a boolean value false in case the value is non-finite.
The wrap throws a runtime exception in case the value is non-finite.

@dubeyaditya29
Copy link
Author

#713

@stleary
Copy link
Owner

stleary commented Oct 3, 2023

@dubeyaditya29 Good start, but more work is needed.

Please revert the change in pom.xml:
Error: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project json: Fatal error compiling: invalid target release: 9 -> [Help 1]

Are there any other numeric types that should be checked?
Does JSONArray need this check too?
Each new line of code should receive coverage from one or more of the new unit tests.

@@ -2629,8 +2632,34 @@ public static Object wrap(Object object) {
return wrap(object, null);
}

// Custom isFiniteNumber method
public static boolean isFiniteNumber(Object value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This method is sub-optimal, and redundant of testValidity and numberIsFinite methods that are already available. Please use the existing function(s)

pom.xml Outdated
@@ -95,8 +95,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>1.8</source>
Copy link
Contributor

Choose a reason for hiding this comment

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

As stated already, this needs to be reverted. Java 1.8 is our current target.

Copy link
Author

Choose a reason for hiding this comment

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

I'll made the necessary changes here thanks.

private static Object wrap(Object object, Set<Object> objectsRecord) {
try {
// Calling the isFiniteNumber(Object object) -> to check if the object is Non-finite.
if(!isFiniteNumber(object)){
Copy link
Contributor

@johnjaylward johnjaylward Oct 3, 2023

Choose a reason for hiding this comment

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

this check should happen after the NULL check. Also, you can change this to just use testValidity

Comment on lines 2639 to 2640
if (value instanceof Integer) {
doubleValue = ((Integer) value).doubleValue();
Copy link
Contributor

Choose a reason for hiding this comment

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

Integers are always finite.

Copy link
Contributor

Choose a reason for hiding this comment

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

agreed. Using the existing testValidity method would be the correct course and already covers what this PR is attempting.

Copy link
Author

Choose a reason for hiding this comment

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

agree @johnjaylward the testValidity can alone verify this, am somehow new to this code base, can you help me understand how did you found this method.

@dubeyaditya29
Copy link
Author

hey @stleary Thankyou for reviewing this.

  1. Have added Integer, Float, Double, I will add Long as well, let me know if missing any specific type.
  2. Let me add test cases for that as well will get back on this.
  3. For the last point I have created a different test class file for checking the JSONObject, let me know if I have missed something will add that too.

@dubeyaditya29
Copy link
Author

thanks to @johnjaylward suggestions I think there is no need for defining a new method here we already have testValidaity which can fulfil the requirements.

src/test/java/org/json/junit/data/JsonObjectTest.java Outdated Show resolved Hide resolved
private static Object wrap(Object object, Set<Object> objectsRecord) {
try {
// Calling the isFiniteNumber(Object object) -> to check if the object is Non-finite.
if(!isFiniteNumber(object)){
throw new RuntimeException("Non-Finite Number encountered");
Copy link
Contributor

Choose a reason for hiding this comment

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

Throw JSONException, not a generic RuntimeException.

@johnjaylward
Copy link
Contributor

#779 is a better implementation. This should be rejected

@stleary
Copy link
Owner

stleary commented Oct 5, 2023

Thanks, but not accepting this PR.

@stleary stleary closed this Oct 5, 2023
@dubeyaditya29
Copy link
Author

No problem @stleary thanks for reviewing.

@stleary
Copy link
Owner

stleary commented Oct 5, 2023

@dubeyaditya29 if you want to work on a different issue, I recommend #748, which I just reopened. Let me know if you have any questions.

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.

4 participants