Skip to content

Commit fe9e13f

Browse files
committed
reject non-finite float values in parseLiteral
1 parent 26186af commit fe9e13f

5 files changed

Lines changed: 22 additions & 5 deletions

File tree

‎src/main/java/graphql/scalar/GraphqlFloatCoercing.java‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,25 @@ private Double parseValueImpl(@NonNull Object input, @NonNull Locale locale) {
8282
}
8383

8484
private static double parseLiteralImpl(@NonNull Object input, @NonNull Locale locale) {
85+
double result;
8586
if (input instanceof IntValue) {
86-
return ((IntValue) input).getValue().doubleValue();
87+
result = ((IntValue) input).getValue().doubleValue();
8788
} else if (input instanceof FloatValue) {
88-
return ((FloatValue) input).getValue().doubleValue();
89+
result = ((FloatValue) input).getValue().doubleValue();
8990
} else {
9091
throw new CoercingParseLiteralException(
9192
i18nMsg(locale, "Float.unexpectedAstType", typeName(input))
9293
);
9394
}
95+
// the GraphQL Float spec requires non-finite values (NaN and Infinity) to raise an error on
96+
// input coercion, same as convertImpl above. a literal like 1e400 overflows BigDecimal.doubleValue()
97+
// to Infinity, so it has to be rejected here rather than flowing through to a data fetcher.
98+
if (Double.isNaN(result) || Double.isInfinite(result)) {
99+
throw new CoercingParseLiteralException(
100+
i18nMsg(locale, "Float.notFinite")
101+
);
102+
}
103+
return result;
94104
}
95105

96106
@NonNull

‎src/main/resources/i18n/Scalars.properties‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ ID.unexpectedAstType=Expected an AST type of ''IntValue'' or ''StringValue'' but
2525
Float.notFloat=Expected a value that can be converted to type ''Float'' but it was a ''{0}''
2626
Float.unexpectedAstType=Expected an AST type of ''IntValue'' or ''FloatValue'' but it was a ''{0}''
2727
Float.unexpectedRawValueType=Expected a Number input, but it was a ''{0}''
28+
Float.notFinite=Float value must be a finite number, but it was infinite or NaN
2829
#
2930
Boolean.notBoolean=Expected a value that can be converted to type ''Boolean'' but it was a ''{0}''
3031
Boolean.unexpectedAstType=Expected an AST type of ''BooleanValue'' but it was a ''{0}''

‎src/main/resources/i18n/Scalars_de.properties‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ ID.unexpectedAstType=Erwartet wurde ein AST type von ''IntValue'' oder ''StringV
2525
Float.notFloat=Erwartet wurde ein Wert, der in den Typ ''Float'' konvertiert werden kann, aber es war ein ''{0}''
2626
Float.unexpectedAstType=Erwartet wurde ein AST type von ''IntValue'' oder ''FloatValue'', aber es war ein ''{0}''
2727
Float.unexpectedRawValueType=Erwartet wurde eine Number-Eingabe, aber es war ein ''{0}''
28+
Float.notFinite=Der Float-Wert muss eine endliche Zahl sein, aber er war unendlich oder NaN
2829
#
2930
Boolean.notBoolean=Erwartet wurde ein Wert, der in den Typ ''Boolean'' konvertiert werden kann, aber es war ein ''{0}''
3031
Boolean.unexpectedAstType=Erwartet wurde ein AST type ''BooleanValue'', aber es war ein ''{0}''

‎src/main/resources/i18n/Scalars_nl.properties‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Float.notFloat=Verwacht werd een waarde die in ''Float'' veranderd kon worden, m
2626
Float.unexpectedAstType=Verwacht werd een AST-type ''IntValue'' of ''FloatValue'', maar het was een ''{0}''
2727
# TODO: To be translated into Dutch
2828
Float.unexpectedRawValueType=Expected a Number input, but it was a ''{0}''
29+
# TODO: To be translated into Dutch
30+
Float.notFinite=Float value must be a finite number, but it was infinite or NaN
2931
#
3032
Boolean.notBoolean=Verwacht werd een waarde die in ''Boolean'' veranderd kon worden, maar het was een ''{0}''
3133
Boolean.unexpectedAstType=Verwacht werd een AST-type ''BooleanValue'', maar het was een ''{0}''

‎src/test/groovy/graphql/ScalarsFloatTest.groovy‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ class ScalarsFloatTest extends Specification {
4949
thrown(CoercingParseLiteralException)
5050

5151
where:
52-
literal | _
53-
new BooleanValue(true) | _
54-
new StringValue("") | _
52+
literal | _
53+
new BooleanValue(true) | _
54+
new StringValue("") | _
55+
new FloatValue(new BigDecimal("1e400")) | _
56+
new FloatValue(new BigDecimal("-1e400")) | _
57+
new IntValue(BigInteger.TEN.pow(400)) | _
5558
}
5659

5760
@Unroll

0 commit comments

Comments
 (0)