File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1212
1313public 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 });
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments