8000 Fixed bug where Special Values of Float and Double were not parsed from · robccan/google-http-java-client@926e04d · GitHub
[go: up one dir, main page]

Skip to content

Commit 926e04d

Browse files
cristianhejona86
authored andcommitted
Fixed bug where Special Values of Float and Double were not parsed from
String and threw an exception. The bug consists in the fact that NaN / Infinity / -Infinity are interpreted as a String (which it is). I added a check that if the expected Class is Float / Double (or float / double) and getText() returns NaN / Infinity / -Infinity (case-insensitive), the Parses omits checking that there is no JsonString Annotation. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=101010886
1 parent bec59ea commit 926e04d

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

google-http-client/src/main/java/com/google/api/client/json/JsonParser.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.Collection;
3636
import java.util.Collections;
3737
import java.util.HashSet;
38+
import java.util.Locale;
3839
import java.util.Map;
3940
import java.util.Set;
4041
import java.util.WeakHashMap;
@@ -842,11 +843,19 @@ private final Object parseValue(Field fieldContext,
842843
}
843844
throw new IllegalArgumentException("expected numeric type but got " + valueType);
844845
case VALUE_STRING:
845-
Preconditions.checkArgument(valueClass == null
846+
//TODO(user): Maybe refactor this method in multiple mini-methods for readability?
847+
String text = getText().trim().toLowerCase(Locale.US);
848+
// If we are expecting a Float / Double and the Text is NaN (case insensitive)
849+
// Then: Accept, even if the Annotation is JsonString.
850+
// Otherwise: Check that the Annotation is not JsonString.
851+
if (!(((valueClass == float.class || valueClass == Float.class)
852+
|| (valueClass == double.class || valueClass == Double.class))
853+
&& (text.equals("nan") || text.equals("infinity") || text.equals("-infinity")))) {
854+
64B2 Preconditions.checkArgument(valueClass == null
846855
|| !Number.class.isAssignableFrom(valueClass) || fieldContext != null
847856
&& fieldContext.getAnnotation(JsonString.class) != null,
848857
"number field formatted as a JSON string must use the @JsonString annotation");
849-
// TODO(yanivi): "special" values like Double.POSITIVE_INFINITY?
858+
}
850859
return Data.parsePrimitiveValue(valueType, getText());
851860
case VALUE_NULL:
852861
Preconditions.checkArgument(valueClass == null || !valueClass.isPrimitive(),

0 commit comments

Comments
 (0)
0