Skip to content

Commit 03e10fb

Browse files
committed
Align Boolean parseValue with JS implementation
1 parent 476632e commit 03e10fb

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,12 @@ private Boolean serializeImpl(@NotNull Object input, @NotNull Locale locale) {
6868

6969
@NotNull
7070
private Boolean parseValueImpl(@NotNull Object input, @NotNull Locale locale) {
71-
Boolean result = convertImpl(input);
72-
if (result == null) {
71+
if (!(input instanceof Boolean)) {
7372
throw new CoercingParseValueException(
74-
i18nMsg(locale, "Boolean.notBoolean", typeName(input))
73+
i18nMsg(locale, "Boolean.unexpectedRawValueType", typeName(input))
7574
);
7675
}
77-
return result;
76+
return (Boolean) input;
7877
}
7978

8079
private static boolean parseLiteralImpl(@NotNull Object input, @NotNull Locale locale) {

src/main/resources/i18n/Scalars.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ Float.unexpectedAstType=Expected an AST type of ''IntValue'' or ''FloatValue'' b
2727
#
2828
Boolean.notBoolean=Expected a value that can be converted to type ''Boolean'' but it was a ''{0}''
2929
Boolean.unexpectedAstType=Expected an AST type of ''BooleanValue'' but it was a ''{0}''
30+
Boolean.unexpectedRawValueType=Expected a Boolean input, but it was a ''{0}''
3031
#
3132
String.unexpectedRawValueType=Expected a String input, 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
@@ -30,5 +30,6 @@ Float.unexpectedAstType=Erwartet wurde ein AST type von ''IntValue'' oder ''Floa
3030
#
3131
Boolean.notBoolean=Erwartet wurde ein Wert, der in den Typ ''Boolean'' konvertiert werden kann, aber es war ein ''{0}''
3232
Boolean.unexpectedAstType=Erwartet wurde ein AST type ''BooleanValue'', aber es war ein ''{0}''
33+
Boolean.unexpectedRawValueType=Erwartet wurde eine Boolean-Eingabe, aber es war ein ''{0}''
3334
#
3435
String.unexpectedRawValueType=Erwartet wurde eine String-Eingabe, aber es war ein ''{0}''

src/test/groovy/graphql/ScalarsBooleanTest.groovy

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ class ScalarsBooleanTest extends Specification {
5353
def "Boolean serialize #value into #result (#result.class)"() {
5454
expect:
5555
Scalars.GraphQLBoolean.getCoercing().serialize(value, GraphQLContext.default, Locale.default) == result
56-
Scalars.GraphQLBoolean.getCoercing().parseValue(value, GraphQLContext.default, Locale.default) == result
5756

5857
where:
5958
value | result
@@ -75,7 +74,6 @@ class ScalarsBooleanTest extends Specification {
7574
def "Boolean serialize #value into #result (#result.class) with deprecated methods"() {
7675
expect:
7776
Scalars.GraphQLBoolean.getCoercing().serialize(value) == result // Retain deprecated method for test coverage
78-
Scalars.GraphQLBoolean.getCoercing().parseValue(value) == result // Retain deprecated method for test coverage
7977

8078
where:
8179
value | result
@@ -111,6 +109,28 @@ class ScalarsBooleanTest extends Specification {
111109
"f" | _
112110
}
113111

112+
@Unroll
113+
def "Boolean parseValue #value into #result (#result.class)"() {
114+
expect:
115+
Scalars.GraphQLBoolean.getCoercing().parseValue(value, GraphQLContext.default, Locale.default) == result
116+
117+
where:
118+
value | result
119+
true | true
120+
false | false
121+
}
122+
123+
@Unroll
124+
def "Boolean parseValue #value into #result (#result.class) with deprecated methods"() {
125+
expect:
126+
Scalars.GraphQLBoolean.getCoercing().parseValue(value) == result // Retain deprecated method for test coverage
127+
128+
where:
129+
value | result
130+
true | true
131+
false | false
132+
}
133+
114134
@Unroll
115135
def "parseValue throws exception for invalid input #value"() {
116136
when:
@@ -119,8 +139,19 @@ class ScalarsBooleanTest extends Specification {
119139
thrown(CoercingParseValueException)
120140

121141
where:
122-
value | _
123-
new Object() | _
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 | _
124155
}
125156

126157
}

0 commit comments

Comments
 (0)