Skip to content

Commit 2aecb85

Browse files
committed
Add stricter float parseValue coercion
1 parent 0336f9b commit 2aecb85

4 files changed

Lines changed: 61 additions & 58 deletions

File tree

src/main/java/graphql/scalar/GraphqlBooleanCoercing.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public Boolean parseLiteral(@NotNull Object input) {
128128

129129
@Override
130130
@Deprecated
131-
public Value valueToLiteral(@NotNull Object input) {
131+
public @NotNull Value<?> valueToLiteral(@NotNull Object input) {
132132
return valueToLiteralImpl(input, Locale.getDefault());
133133
}
134134

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ private Double serialiseImpl(Object input, @NotNull Locale locale) {
6565

6666
@NotNull
6767
private Double parseValueImpl(@NotNull Object input, @NotNull Locale locale) {
68+
if (!(input instanceof Number)) {
69+
throw new CoercingParseValueException(
70+
i18nMsg(locale, "Float.unexpectedRawValueType", typeName(input))
71+
);
72+
}
73+
6874
Double result = convertImpl(input);
6975
if (result == null) {
7076
throw new CoercingParseValueException(

src/test/groovy/graphql/ScalarsBooleanTest.groovy

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@ class ScalarsBooleanTest extends Specification {
5555
Scalars.GraphQLBoolean.getCoercing().serialize(value, GraphQLContext.default, Locale.default) == result
5656

5757
where:
58-
value | result
59-
true | true
60-
"false" | false
61-
"true" | true
62-
"True" | true
63-
0 | false
64-
1 | true
65-
-1 | true
66-
new Long(42345784398534785l) | true
67-
new Double(42.3) | true
68-
new Float(42.3) | true
69-
Integer.MAX_VALUE + 1l | true
70-
Integer.MIN_VALUE - 1l | true
58+
value | result
59+
true | true
60+
"false" | false
61+
"true" | true
62+
"True" | true
63+
0 | false
64+
1 | true
65+
-1 | true
66+
Long.valueOf(42345784398534785l) | true
67+
Double.valueOf(42.3) | true
68+
Float.valueOf(42.3) | true
69+
Integer.MAX_VALUE + 1l | true
70+
Integer.MIN_VALUE - 1l | true
7171
}
7272

7373
@Unroll
@@ -76,19 +76,19 @@ class ScalarsBooleanTest extends Specification {
7676
Scalars.GraphQLBoolean.getCoercing().serialize(value) == result // Retain deprecated method for test coverage
7777

7878
where:
79-
value | result
80-
true | true
81-
"false" | false
82-
"true" | true
83-
"True" | true
84-
0 | false
85-
1 | true
86-
-1 | true
87-
new Long(42345784398534785l) | true
88-
new Double(42.3) | true
89-
new Float(42.3) | true
90-
Integer.MAX_VALUE + 1l | true
91-
Integer.MIN_VALUE - 1l | true
79+
value | result
80+
true | true
81+
"false" | false
82+
"true" | true
83+
"True" | true
84+
0 | false
85+
1 | true
86+
-1 | true
87+
Long.valueOf(42345784398534785l) | true
88+
Double.valueOf(42.3) | true
89+
Float.valueOf(42.3) | true
90+
Integer.MAX_VALUE + 1l | true
91+
Integer.MIN_VALUE - 1l | true
9292
}
9393

9494
@Unroll
@@ -139,19 +139,19 @@ class ScalarsBooleanTest extends Specification {
139139
thrown(CoercingParseValueException)
140140

141141
where:
142-
value | _
143-
new Object() | _
144-
"false" | _
145-
"true" | _
146-
"True" | _
147-
0 | _
148-
1 | _
149-
-1 | _
150-
new Long(42345784398534785l) | _
151-
new Double(42.3) | _
152-
new Float(42.3) | _
153-
Integer.MAX_VALUE + 1l | _
154-
Integer.MIN_VALUE - 1l | _
142+
value | _
143+
new Object() | _
144+
"false" | _
145+
"true" | _
146+
"True" | _
147+
0 | _
148+
1 | _
149+
-1 | _
150+
Long.valueOf(42345784398534785l) | _
151+
Double.valueOf(42.3) | _
152+
Float.valueOf(42.3) | _
153+
Integer.MAX_VALUE + 1l | _
154+
Integer.MIN_VALUE - 1l | _
155155
}
156156

157157
}

src/test/groovy/graphql/ScalarsFloatTest.groovy

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ class ScalarsFloatTest extends Specification {
6464
"42" | 42d
6565
"42.123" | 42.123d
6666
42.0000d | 42
67-
new Integer(42) | 42
67+
Integer.valueOf(42) | 42
6868
"-1" | -1
6969
new BigInteger("42") | 42
7070
new BigDecimal("42") | 42
7171
new BigDecimal("4.2") | 4.2d
7272
42.3f | 42.3d
7373
42.0d | 42d
74-
new Byte("42") | 42
75-
new Short("42") | 42
74+
Byte.valueOf("42") | 42
75+
Short.valueOf("42") | 42
7676
1234567l | 1234567d
7777
new AtomicInteger(42) | 42
7878
Double.MAX_VALUE | Double.MAX_VALUE
@@ -89,15 +89,15 @@ class ScalarsFloatTest extends Specification {
8989
"42" | 42d
9090
"42.123" | 42.123d
9191
42.0000d | 42
92-
new Integer(42) | 42
92+
Integer.valueOf(42) | 42
9393
"-1" | -1
9494
new BigInteger("42") | 42
9595
new BigDecimal("42") | 42
9696
new BigDecimal("4.2") | 4.2d
9797
42.3f | 42.3d
9898
42.0d | 42d
99-
new Byte("42") | 42
100-
new Short("42") | 42
99+
Byte.valueOf("42") | 42
100+
Short.valueOf("42") | 42
101101
1234567l | 1234567d
102102
new AtomicInteger(42) | 42
103103
Double.MAX_VALUE | Double.MAX_VALUE
@@ -137,21 +137,18 @@ class ScalarsFloatTest extends Specification {
137137
where:
138138
value | result
139139
42.0000d | 42
140-
new Integer(42) | 42
140+
Integer.valueOf(42) | 42
141141
new BigInteger("42") | 42
142142
new BigDecimal("42") | 42
143143
new BigDecimal("4.2") | 4.2d
144144
42.3f | 42.3d
145145
42.0d | 42d
146-
new Byte("42") | 42
147-
new Short("42") | 42
146+
Byte.valueOf("42") | 42
147+
Short.valueOf("42") | 42
148148
1234567l | 1234567d
149149
new AtomicInteger(42) | 42
150150
Double.MAX_VALUE | Double.MAX_VALUE
151151
Double.MIN_VALUE | Double.MIN_VALUE
152-
"42" | 42d
153-
"42.123" | 42.123d
154-
"-1" | -1
155152
}
156153

157154
@Unroll
@@ -162,21 +159,18 @@ class ScalarsFloatTest extends Specification {
162159
where:
163160
value | result
164161
42.0000d | 42
165-
new Integer(42) | 42
162+
Integer.valueOf(42) | 42
166163
new BigInteger("42") | 42
167164
new BigDecimal("42") | 42
168165
new BigDecimal("4.2") | 4.2d
169166
42.3f | 42.3d
170167
42.0d | 42d
171-
new Byte("42") | 42
172-
new Short("42") | 42
168+
Byte.valueOf("42") | 42
169+
Short.valueOf("42") | 42
173170
1234567l | 1234567d
174171
new AtomicInteger(42) | 42
175172
Double.MAX_VALUE | Double.MAX_VALUE
176173
Double.MIN_VALUE | Double.MIN_VALUE
177-
"42" | 42d
178-
"42.123" | 42.123d
179-
"-1" | -1
180174
}
181175

182176

@@ -203,6 +197,9 @@ class ScalarsFloatTest extends Specification {
203197
Float.POSITIVE_INFINITY.toString() | _
204198
Float.NEGATIVE_INFINITY | _
205199
Float.NEGATIVE_INFINITY.toString() | _
200+
"42" | _
201+
"42.123" | _
202+
"-1" | _
206203
}
207204

208205
}

0 commit comments

Comments
 (0)