Skip to content

Commit e042b8e

Browse files
committed
Align Float parseValue with JS implementation
1 parent 03e10fb commit e042b8e

4 files changed

Lines changed: 58 additions & 3 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,20 @@ private Double serialiseImpl(Object input, @NotNull Locale locale) {
6464
}
6565

6666
@NotNull
67-
private Double parseValueImpl(Object input, @NotNull Locale locale) {
67+
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(
7177
i18nMsg(locale, "Float.notFloat", typeName(input))
7278
);
7379
}
80+
7481
return result;
7582
}
7683

src/main/resources/i18n/Scalars.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ ID.unexpectedAstType=Expected an AST type of ''IntValue'' or ''StringValue'' but
2424
#
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}''
27+
Float.unexpectedRawValueType=Expected a Number input, but it was a ''{0}''
2728
#
2829
Boolean.notBoolean=Expected a value that can be converted to type ''Boolean'' but it was a ''{0}''
2930
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
@@ -27,6 +27,7 @@ ID.unexpectedAstType=Erwartet wurde ein AST type von ''IntValue'' oder ''StringV
2727
#
2828
Float.notFloat=Erwartet wurde ein Wert, der in den Typ ''Float'' konvertiert werden kann, aber es war ein ''{0}''
2929
Float.unexpectedAstType=Erwartet wurde ein AST type von ''IntValue'' oder ''FloatValue'', aber es war ein ''{0}''
30+
Float.unexpectedRawValueType=Erwartet wurde eine Number-Eingabe, aber es war ein ''{0}''
3031
#
3132
Boolean.notBoolean=Erwartet wurde ein Wert, der in den Typ ''Boolean'' konvertiert werden kann, aber es war ein ''{0}''
3233
Boolean.unexpectedAstType=Erwartet wurde ein AST type ''BooleanValue'', aber es war ein ''{0}''

src/test/groovy/graphql/ScalarsFloatTest.groovy

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ class ScalarsFloatTest extends Specification {
5858
def "Float serialize #value into #result (#result.class)"() {
5959
expect:
6060
Scalars.GraphQLFloat.getCoercing().serialize(value, GraphQLContext.default, Locale.default) == result
61-
Scalars.GraphQLFloat.getCoercing().parseValue(value, GraphQLContext.default, Locale.default) == result
6261

6362
where:
6463
value | result
@@ -84,7 +83,6 @@ class ScalarsFloatTest extends Specification {
8483
def "Float serialize #value into #result (#result.class) with deprecated methods"() {
8584
expect:
8685
Scalars.GraphQLFloat.getCoercing().serialize(value) == result // Retain deprecated method for coverage
87-
Scalars.GraphQLFloat.getCoercing().parseValue(value) == result // Retain deprecated method for coverage
8886

8987
where:
9088
value | result
@@ -131,6 +129,51 @@ class ScalarsFloatTest extends Specification {
131129
Float.NEGATIVE_INFINITY.toString() | _
132130
}
133131

132+
@Unroll
133+
def "Float parseValue #value into #result (#result.class)"() {
134+
expect:
135+
Scalars.GraphQLFloat.getCoercing().parseValue(value, GraphQLContext.default, Locale.default) == result
136+
137+
where:
138+
value | result
139+
42.0000d | 42
140+
new Integer(42) | 42
141+
new BigInteger("42") | 42
142+
new BigDecimal("42") | 42
143+
new BigDecimal("4.2") | 4.2d
144+
42.3f | 42.3d
145+
42.0d | 42d
146+
new Byte("42") | 42
147+
new Short("42") | 42
148+
1234567l | 1234567d
149+
new AtomicInteger(42) | 42
150+
Double.MAX_VALUE | Double.MAX_VALUE
151+
Double.MIN_VALUE | Double.MIN_VALUE
152+
}
153+
154+
@Unroll
155+
def "Float parseValue #value into #result (#result.class) with deprecated methods"() {
156+
expect:
157+
Scalars.GraphQLFloat.getCoercing().parseValue(value) == result // Retain deprecated method for coverage
158+
159+
where:
160+
value | result
161+
42.0000d | 42
162+
new Integer(42) | 42
163+
new BigInteger("42") | 42
164+
new BigDecimal("42") | 42
165+
new BigDecimal("4.2") | 4.2d
166+
42.3f | 42.3d
167+
42.0d | 42d
168+
new Byte("42") | 42
169+
new Short("42") | 42
170+
1234567l | 1234567d
171+
new AtomicInteger(42) | 42
172+
Double.MAX_VALUE | Double.MAX_VALUE
173+
Double.MIN_VALUE | Double.MIN_VALUE
174+
}
175+
176+
134177
@Unroll
135178
def "parseValue throws exception for invalid input #value"() {
136179
when:
@@ -154,6 +197,9 @@ class ScalarsFloatTest extends Specification {
154197
Float.POSITIVE_INFINITY.toString() | _
155198
Float.NEGATIVE_INFINITY | _
156199
Float.NEGATIVE_INFINITY.toString() | _
200+
"42" | _
201+
"42.123" | _
202+
"-1" | _
157203
}
158204

159205
}

0 commit comments

Comments
 (0)