Skip to content

Commit 6335cf0

Browse files
authored
Float coercion tidy up (graphql-java#2982)
* Add all Float and Double coercion exception cases * Clean up tests, remove no longer allowed NaN value * Add early return for Doubles * Reorder NaN/Infinity check and add string representation to test * Remove unnecessary NaN check * Tidy package name * Tidy up
1 parent 4024420 commit 6335cf0

5 files changed

Lines changed: 52 additions & 38 deletions

File tree

‎src/main/java/graphql/execution/ValuesResolverLegacy.java‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ static Value<?> valueToLiteralLegacy(Object value, GraphQLType type, GraphQLCont
7777
// Since value is an internally represented value, it must be serialized
7878
// to an externally represented value before converting into an AST.
7979
final Object serialized = serializeLegacy(type, value, graphqlContext, locale);
80-
if (isNullishLegacy(serialized)) {
81-
return null;
82-
}
8380

8481
// Others serialize based on their corresponding JavaScript scalar types.
8582
if (serialized instanceof Boolean) {
@@ -157,11 +154,4 @@ private static Object serializeLegacy(GraphQLType type, Object value, GraphQLCon
157154
return ((GraphQLEnumType) type).serialize(value, graphqlContext, locale);
158155
}
159156
}
160-
161-
private static boolean isNullishLegacy(Object serialized) {
162-
if (serialized instanceof Number) {
163-
return Double.isNaN(((Number) serialized).doubleValue());
164-
}
165-
return serialized == null;
166-
}
167157
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,27 @@
2929
public class GraphqlFloatCoercing implements Coercing<Double, Double> {
3030

3131
private Double convertImpl(Object input) {
32-
if (isNumberIsh(input)) {
32+
// From the GraphQL Float spec, non-finite floating-point internal values (NaN and Infinity)
33+
// must raise a field error on both result and input coercion
34+
Double doubleInput;
35+
if (input instanceof Double) {
36+
doubleInput = (Double) input;
37+
} else if (isNumberIsh(input)) {
3338
BigDecimal value;
3439
try {
3540
value = new BigDecimal(input.toString());
3641
} catch (NumberFormatException e) {
3742
return null;
3843
}
39-
return value.doubleValue();
44+
doubleInput = value.doubleValue();
4045
} else {
4146
return null;
4247
}
4348

49+
if (Double.isNaN(doubleInput) || Double.isInfinite(doubleInput)) {
50+
return null;
51+
}
52+
return doubleInput;
4453
}
4554

4655
@NotNull

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,46 @@ class ScalarsFloatTest extends Specification {
114114
thrown(CoercingSerializeException)
115115

116116
where:
117-
value | _
118-
"" | _
119-
"not a number " | _
120-
Double.NaN | _
117+
value | _
118+
"" | _
119+
"not a number " | _
120+
Double.NaN | _
121+
Double.NaN.toString() | _
122+
Double.POSITIVE_INFINITY | _
123+
Double.POSITIVE_INFINITY.toString() | _
124+
Double.NEGATIVE_INFINITY | _
125+
Double.NEGATIVE_INFINITY.toString() | _
126+
Float.NaN | _
127+
Float.NaN.toString() | _
128+
Float.POSITIVE_INFINITY | _
129+
Float.POSITIVE_INFINITY.toString() | _
130+
Float.NEGATIVE_INFINITY | _
131+
Float.NEGATIVE_INFINITY.toString() | _
121132
}
122133

123134
@Unroll
124-
def "serialize/parseValue throws exception for invalid input #value"() {
135+
def "parseValue throws exception for invalid input #value"() {
125136
when:
126137
Scalars.GraphQLFloat.getCoercing().parseValue(value, GraphQLContext.default, Locale.default)
127138
then:
128139
thrown(CoercingParseValueException)
129140

130141
where:
131-
value | _
132-
"" | _
133-
"not a number " | _
134-
Double.NaN | _
142+
value | _
143+
"" | _
144+
"not a number " | _
145+
Double.NaN | _
146+
Double.NaN.toString() | _
147+
Double.POSITIVE_INFINITY | _
148+
Double.POSITIVE_INFINITY.toString() | _
149+
Double.NEGATIVE_INFINITY | _
150+
Double.NEGATIVE_INFINITY.toString() | _
151+
Float.NaN | _
152+
Float.NaN.toString() | _
153+
Float.POSITIVE_INFINITY | _
154+
Float.POSITIVE_INFINITY.toString() | _
155+
Float.NEGATIVE_INFINITY | _
156+
Float.NEGATIVE_INFINITY.toString() | _
135157
}
136158

137159
}

‎src/test/groovy/graphql/ScalarsQuerySchema.java‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,6 @@ public class ScalarsQuerySchema {
1818

1919
public static final GraphQLObjectType queryType = newObject()
2020
.name("QueryType")
21-
// Static scalars
22-
.field(newFieldDefinition()
23-
.name("floatNaN")
24-
.type(Scalars.GraphQLFloat)
25-
.staticValue(Double.NaN)) // Retain for test coverage
26-
// Scalars with input of same type, value echoed back
27-
.field(newFieldDefinition()
28-
.name("floatNaNInput")
29-
.type(Scalars.GraphQLFloat)
30-
.argument(newArgument()
31-
.name("input")
32-
.type(GraphQLNonNull.nonNull(Scalars.GraphQLFloat))))
3321
.field(newFieldDefinition()
3422
.name("stringInput")
3523
.type(Scalars.GraphQLString)
@@ -51,12 +39,10 @@ public class ScalarsQuerySchema {
5139
.type(Scalars.GraphQLString)))
5240
.build();
5341

54-
static FieldCoordinates floatNaNCoordinates = FieldCoordinates.coordinates("QueryType", "floatNaNInput");
5542
static FieldCoordinates stringInputCoordinates = FieldCoordinates.coordinates("QueryType", "stringInput");
5643
static FieldCoordinates floatStringCoordinates = FieldCoordinates.coordinates("QueryType", "floatString");
5744
static FieldCoordinates intStringCoordinates = FieldCoordinates.coordinates("QueryType", "intString");
5845
static GraphQLCodeRegistry codeRegistry = GraphQLCodeRegistry.newCodeRegistry()
59-
.dataFetcher(floatNaNCoordinates, inputDF)
6046
.dataFetcher(stringInputCoordinates, inputDF)
6147
.dataFetcher(floatStringCoordinates, inputDF)
6248
.dataFetcher(intStringCoordinates, inputDF)

‎src/test/groovy/graphql/execution/ValuesResolverTestLegacy.groovy‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
package graphql.language
1+
package graphql.execution
22

33
import graphql.GraphQLContext
4+
import graphql.language.ArrayValue
5+
import graphql.language.EnumValue
6+
import graphql.language.FloatValue
7+
import graphql.language.IntValue
8+
import graphql.language.ObjectField
9+
import graphql.language.ObjectValue
10+
import graphql.language.StringValue
411
import graphql.schema.GraphQLEnumType
512
import graphql.schema.GraphQLInputObjectType
613
import spock.lang.Ignore
@@ -137,15 +144,15 @@ class ValuesResolverTestLegacy extends Specification {
137144
def 'converts list to lists'() {
138145
expect:
139146
valueToLiteralLegacy(['hello', 'world'], list(GraphQLString), graphQLContext, locale).isEqualTo(
140-
new ArrayValue(['hello', 'world'])
147+
new ArrayValue([new StringValue('hello'), new StringValue('world')])
141148
)
142149
}
143150

144151
def 'converts arrays to lists'() {
145152
String[] sArr = ['hello', 'world'] as String[]
146153
expect:
147154
valueToLiteralLegacy(sArr, list(GraphQLString), graphQLContext, locale).isEqualTo(
148-
new ArrayValue(['hello', 'world'])
155+
new ArrayValue([new StringValue('hello'), new StringValue('world')])
149156
)
150157
}
151158

0 commit comments

Comments
 (0)