Skip to content

Commit e4283f4

Browse files
committed
Add backport of scalar coercion reversion PR #3186
1 parent bc1770b commit e4283f4

11 files changed

+56
-102
lines changed

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

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

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

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

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@ 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-
7468
Double result = convertImpl(input);
7569
if (result == null) {
7670
throw new CoercingParseValueException(

src/main/java/graphql/scalar/GraphqlIntCoercing.java

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -64,45 +64,15 @@ private Integer serialiseImpl(Object input, @NotNull Locale locale) {
6464

6565
@NotNull
6666
private Integer parseValueImpl(@NotNull Object input, @NotNull Locale locale) {
67-
if (!(input instanceof Number)) {
68-
throw new CoercingParseValueException(
69-
i18nMsg(locale, "Int.notInt", typeName(input))
70-
);
71-
}
72-
73-
if (input instanceof Integer) {
74-
return (Integer) input;
75-
}
67+
Integer result = convertImpl(input);
7668

77-
BigInteger result = convertParseValueImpl(input);
7869
if (result == null) {
7970
throw new CoercingParseValueException(
8071
i18nMsg(locale, "Int.notInt", typeName(input))
8172
);
8273
}
8374

84-
if (result.compareTo(INT_MIN) < 0 || result.compareTo(INT_MAX) > 0) {
85-
throw new CoercingParseValueException(
86-
i18nMsg(locale, "Int.outsideRange", result.toString())
87-
);
88-
}
89-
return result.intValueExact();
90-
}
91-
92-
private BigInteger convertParseValueImpl(Object input) {
93-
BigDecimal value;
94-
try {
95-
value = new BigDecimal(input.toString());
96-
} catch (NumberFormatException e) {
97-
return null;
98-
}
99-
100-
try {
101-
return value.toBigIntegerExact();
102-
} catch (ArithmeticException e) {
103-
// Exception if number has non-zero fractional part
104-
return null;
105-
}
75+
return result;
10676
}
10777

10878
private static int parseLiteralImpl(Object input, @NotNull Locale locale) {

src/main/java/graphql/scalar/GraphqlStringCoercing.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@ private String toStringImpl(Object input) {
2828
return String.valueOf(input);
2929
}
3030

31-
private String parseValueImpl(@NotNull Object input, @NotNull Locale locale) {
32-
if (!(input instanceof String)) {
33-
throw new CoercingParseValueException(
34-
i18nMsg(locale, "String.unexpectedRawValueType", typeName(input))
35-
);
36-
}
37-
return (String) input;
38-
}
39-
4031
private String parseLiteralImpl(@NotNull Object input, Locale locale) {
4132
if (!(input instanceof StringValue)) {
4233
throw new CoercingParseLiteralException(
@@ -64,12 +55,12 @@ public String serialize(@NotNull Object dataFetcherResult) {
6455
@Override
6556
@Deprecated
6657
public String parseValue(@NotNull Object input) {
67-
return parseValueImpl(input, Locale.getDefault());
58+
return toStringImpl(input);
6859
}
6960

7061
@Override
7162
public String parseValue(@NotNull Object input, @NotNull GraphQLContext graphQLContext, @NotNull Locale locale) throws CoercingParseValueException {
72-
return parseValueImpl(input, locale);
63+
return toStringImpl(input);
7364
}
7465

7566
@Override

src/main/resources/i18n/Scalars.properties

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ 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}''
2827
#
2928
Boolean.notBoolean=Expected a value that can be converted to type ''Boolean'' but it was a ''{0}''
3029
Boolean.unexpectedAstType=Expected an AST type of ''BooleanValue'' but it was a ''{0}''
31-
Boolean.unexpectedRawValueType=Expected a Boolean input, but it was a ''{0}''
32-
#
33-
String.unexpectedRawValueType=Expected a String input, but it was a ''{0}''

src/main/resources/i18n/Scalars_de.properties

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ 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}''
3130
#
3231
Boolean.notBoolean=Erwartet wurde ein Wert, der in den Typ ''Boolean'' konvertiert werden kann, aber es war ein ''{0}''
3332
Boolean.unexpectedAstType=Erwartet wurde ein AST type ''BooleanValue'', aber es war ein ''{0}''
34-
Boolean.unexpectedRawValueType=Erwartet wurde eine Boolean-Eingabe, aber es war ein ''{0}''
35-
#
36-
String.unexpectedRawValueType=Erwartet wurde eine String-Eingabe, aber es war ein ''{0}''

src/test/groovy/graphql/ScalarsBooleanTest.groovy

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,27 @@ class ScalarsBooleanTest extends Specification {
131131
false | false
132132
}
133133

134+
@Unroll
135+
def "parseValue parses non-Boolean input #value"() {
136+
expect:
137+
Scalars.GraphQLBoolean.getCoercing().parseValue(value, GraphQLContext.default, Locale.default) == result
138+
139+
where:
140+
value | result
141+
true | true
142+
"false" | false
143+
"true" | true
144+
"True" | true
145+
0 | false
146+
1 | true
147+
-1 | true
148+
new Long(42345784398534785l) | true
149+
new Double(42.3) | true
150+
new Float(42.3) | true
151+
Integer.MAX_VALUE + 1l | true
152+
Integer.MIN_VALUE - 1l | true
153+
}
154+
134155
@Unroll
135156
def "parseValue throws exception for invalid input #value"() {
136157
when:
@@ -141,17 +162,6 @@ class ScalarsBooleanTest extends Specification {
141162
where:
142163
value | _
143164
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 | _
155165
}
156166

157167
}

src/test/groovy/graphql/ScalarsFloatTest.groovy

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ class ScalarsFloatTest extends Specification {
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
152155
}
153156

154157
@Unroll
@@ -171,6 +174,9 @@ class ScalarsFloatTest extends Specification {
171174
new AtomicInteger(42) | 42
172175
Double.MAX_VALUE | Double.MAX_VALUE
173176
Double.MIN_VALUE | Double.MIN_VALUE
177+
"42" | 42d
178+
"42.123" | 42.123d
179+
"-1" | -1
174180
}
175181

176182

@@ -197,9 +203,6 @@ class ScalarsFloatTest extends Specification {
197203
Float.POSITIVE_INFINITY.toString() | _
198204
Float.NEGATIVE_INFINITY | _
199205
Float.NEGATIVE_INFINITY.toString() | _
200-
"42" | _
201-
"42.123" | _
202-
"-1" | _
203206
}
204207

205208
}

src/test/groovy/graphql/ScalarsIntTest.groovy

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,15 @@ class ScalarsIntTest extends Specification {
137137
new Short("42") | 42
138138
1234567l | 1234567
139139
new AtomicInteger(42) | 42
140-
Integer.MAX_VALUE | Integer.MAX_VALUE
141-
Integer.MIN_VALUE | Integer.MIN_VALUE
142140
42.0000d | 42
143141
new BigDecimal("42") | 42
144142
42.0f | 42
145143
42.0d | 42
144+
Integer.MAX_VALUE | Integer.MAX_VALUE
145+
Integer.MIN_VALUE | Integer.MIN_VALUE
146+
"42" | 42
147+
"42.0000" | 42
148+
"-1" | -1
146149
}
147150

148151
@Unroll
@@ -152,18 +155,21 @@ class ScalarsIntTest extends Specification {
152155

153156
where:
154157
value | result
155-
42.0000d | 42
156158
new Integer(42) | 42
157159
new BigInteger("42") | 42
158-
new BigDecimal("42") | 42
159-
42.0f | 42
160-
42.0d | 42
161160
new Byte("42") | 42
162161
new Short("42") | 42
163162
1234567l | 1234567
164163
new AtomicInteger(42) | 42
164+
42.0000d | 42
165+
new BigDecimal("42") | 42
166+
42.0f | 42
167+
42.0d | 42
165168
Integer.MAX_VALUE | Integer.MAX_VALUE
166169
Integer.MIN_VALUE | Integer.MIN_VALUE
170+
"42" | 42
171+
"42.0000" | 42
172+
"-1" | -1
167173
}
168174

169175
@Unroll
@@ -184,9 +190,6 @@ class ScalarsIntTest extends Specification {
184190
Integer.MAX_VALUE + 1l | _
185191
Integer.MIN_VALUE - 1l | _
186192
new Object() | _
187-
"42" | _
188-
"42.0000" | _
189-
"-1" | _
190193
}
191194

192195
}

src/test/groovy/graphql/ScalarsStringTest.groovy

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import graphql.execution.CoercedVariables
44
import graphql.language.BooleanValue
55
import graphql.language.StringValue
66
import graphql.schema.CoercingParseLiteralException
7-
import graphql.schema.CoercingParseValueException
87
import spock.lang.Shared
98
import spock.lang.Specification
109
import spock.lang.Unroll
@@ -86,24 +85,15 @@ class ScalarsStringTest extends Specification {
8685
}
8786

8887
@Unroll
89-
def "String parseValue throws exception for non-String values"() {
90-
when:
91-
Scalars.GraphQLString.getCoercing().parseValue(literal, GraphQLContext.default, Locale.default)
92-
then:
93-
def ex = thrown(CoercingParseValueException)
88+
def "String parseValue can parse non-String values"() {
89+
expect:
90+
Scalars.GraphQLString.getCoercing().parseValue(value, GraphQLContext.default, Locale.default) == result
9491

9592
where:
96-
literal | _
97-
123 | _
98-
true | _
99-
customObject | _
93+
value | result
94+
123 | "123"
95+
true | "true"
96+
customObject | "foo"
10097
}
10198

102-
def "String parseValue English exception message"() {
103-
when:
104-
Scalars.GraphQLString.getCoercing().parseValue(9001, GraphQLContext.default, Locale.ENGLISH)
105-
then:
106-
def ex = thrown(CoercingParseValueException)
107-
ex.message == "Expected a String input, but it was a 'Integer'"
108-
}
10999
}

src/test/groovy/graphql/execution/ValuesResolverTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ class ValuesResolverTest extends Specification {
641641
executionResult.data == null
642642
executionResult.errors.size() == 1
643643
executionResult.errors[0].errorType == ErrorType.ValidationError
644-
executionResult.errors[0].message == "Variable 'input' has an invalid value: Expected a Boolean input, but it was a 'String'"
644+
executionResult.errors[0].message == "Variable 'input' has an invalid value: Expected a value that can be converted to type 'Boolean' but it was a 'String'"
645645
executionResult.errors[0].locations == [new SourceLocation(2, 35)]
646646
}
647647

@@ -679,7 +679,7 @@ class ValuesResolverTest extends Specification {
679679
executionResult.data == null
680680
executionResult.errors.size() == 1
681681
executionResult.errors[0].errorType == ErrorType.ValidationError
682-
executionResult.errors[0].message == "Variable 'input' has an invalid value: Expected a Number input, but it was a 'String'"
682+
executionResult.errors[0].message == "Variable 'input' has an invalid value: Expected a value that can be converted to type 'Float' but it was a 'String'"
683683
executionResult.errors[0].locations == [new SourceLocation(2, 35)]
684684
}
685685
}

0 commit comments

Comments
 (0)