Skip to content

Commit aabed3a

Browse files
committed
throw error when int literal is too big or too small
1 parent 644dcce commit aabed3a

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

src/main/java/graphql/Scalars.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212

1313
public class Scalars {
1414

15+
private static final BigInteger INT_MAX = BigInteger.valueOf(Integer.MAX_VALUE);
16+
private static final BigInteger INT_MIN = BigInteger.valueOf(Integer.MIN_VALUE);
1517

1618
public static GraphQLScalarType GraphQLInt = new GraphQLScalarType("Int", "Built-in Int", new Coercing() {
17-
@Override
19+
1820
public Object serialize(Object input) {
1921
if (input instanceof String) {
2022
return Integer.parseInt((String) input);
@@ -34,6 +36,9 @@ public Object parseValue(Object input) {
3436
public Object parseLiteral(Object input) {
3537
if (!(input instanceof IntValue)) return null;
3638
BigInteger value = ((IntValue) input).getValue();
39+
if (value.compareTo(INT_MIN) == -1 || value.compareTo(INT_MAX) == 1) {
40+
throw new GraphQLException("Int literal is too big or too small");
41+
}
3742
return value.intValue();
3843
}
3944
});

src/test/groovy/graphql/ScalarsTest.groovy

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ class ScalarsTest extends Specification {
5656
Scalars.GraphQLLong.getCoercing().parseLiteral(literal) == result
5757

5858
where:
59-
literal | result
60-
new StringValue("42") | 42
59+
literal | result
60+
new StringValue("42") | 42
6161
new IntValue(new BigInteger("42")) | 42
6262
new IntValue(new BigInteger("42345784398534785")) | 42345784398534785l
6363
}
@@ -87,6 +87,17 @@ class ScalarsTest extends Specification {
8787

8888
}
8989

90+
def "int parse error for too big/small literal"() {
91+
when:
92+
Scalars.GraphQLInt.getCoercing().parseLiteral(new IntValue(BigInteger.valueOf(intValue)))
93+
94+
then:
95+
thrown(GraphQLException)
96+
97+
where:
98+
intValue << [Integer.MIN_VALUE - 1l, Integer.MAX_VALUE + 1l]
99+
}
100+
90101
def "Int serialize/parseValue object"() {
91102
expect:
92103
Scalars.GraphQLInt.getCoercing().serialize(value) == result

0 commit comments

Comments
 (0)