-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[java] fix JSON parsing of numbers with exponent #16961
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
Conversation
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
||||||||||||||||||||||||
PR Code Suggestions ✨Explore these optional code suggestions:
|
||||||||||||
|
This is fine to me, can you also check the code suggestion |
asolntsev
left a comment
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.
I found a bug:
assertThat((Double) new Json().toType("42e+1", Integer.class)).isEqualTo(420);throws java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Double :)
The line above does try to cast an |
|
@joerg1985 My bad, sorry! :) Anyway, I have one small suggestion how to simplify the code. |
asolntsev
left a comment
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.
A general question is: why Selenium re-invented the wheel instead of using some read-made JSON parser, e.g. Jackson?
This bug shows why it was a bad idea...
asolntsev
left a comment
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.
@joerg1985 Wait, this time I found a bug. I think. Maybe. Probably. :)
@Test
void shouldParseNonDecimalNumbersAsLongs_e() {
try (JsonInput input = newInput("42e+1")) {
assertThat(input.peek()).isEqualTo(NUMBER);
assertThat(input.nextNumber()).isEqualTo(420L);
}
}
expected: 420L
but was: 420.0
I don't know, but in the past GSON has been used and i guess they had good reasons for this. Into the exponent without fraction i have to look closer the next days. |
|
Gson is not maintained anymore. So yes, it was a good idea to stop using Gson. But it doesn't mean we had to re-invent the wheel. ;) This bug is not critical at all, indeed. I think we can merge this PR. |
|
@joerg1985 I found another problem. At least inconsistency. The case is when JsonInput is parsing a number followed by text. For two different texts, it shows a different behaviour: @Test
void bug() {
try (JsonInput input = newInput("12345altruistic")) {
assertThat(input.peek()).isEqualTo(NUMBER);
assertThat(input.nextNumber()).isEqualTo(12345L);
}
try (JsonInput input = newInput("12345egoistic")) { // throws JsonException: Unable to parse to a number: 12345e
assertThat(input.peek()).isEqualTo(NUMBER);
assertThat(input.nextNumber()).isEqualTo(12345L);
}
} |
|
@joerg1985 I tried a bit different fix using |
df08de0 to
673c6e5
Compare
The |
LGTM, we could still change it before the next release.
User description
🔗 Related Issues
I noticed this when reading the z-index of an element via Javascript.
All numbers with exponent and no fraction part are broken.
e.g.
42e-1should be parsed asdouble 4.2but instead was parsed aslong 4💥 What does this PR do?
Fix the JSON parsing of numbers with exponent and no fraction part.
🔧 Implementation Notes
💡 Additional Considerations
🔄 Types of changes
PR Type
Bug fix
Description
Fix JSON parsing of numbers with exponent notation
Correctly parse exponents without fractional parts as doubles
Refactor number parsing logic using switch statement
Add test cases for exponent notation variants
Diagram Walkthrough
flowchart LR A["JSON Number Input"] --> B["Check for decimal point or exponent"] B --> C{Contains decimal or exponent?} C -->|Yes| D["Parse as Double via BigDecimal"] C -->|No| E["Parse as Long"] D --> F["Return Number"] E --> FFile Walkthrough
JsonInput.java
Refactor number parsing with exponent supportjava/src/org/openqa/selenium/json/JsonInput.java
switch statement
fractionalPartboolean todecimalboolean todetect both decimal points and exponents
incorrectly parsed as Long instead of Double
numbers with decimal or exponent notation
JsonTest.java
Add exponent notation test casesjava/test/org/openqa/selenium/json/JsonTest.java
4.2e+1shouldparse as
42.042e+1shouldparse as
420.042e-1shouldparse as
4.24.2e-1shouldparse as
0.42