Skip to content

Commit 4542c35

Browse files
committed
Allow GraphQLLong parsing from large numeric literals
IntValue now stores BigInteger instead of int. GraphQLInt and GraphQLLong parseLiteral will throw if the value overflows int or long respectively. StringValue parsing for GraphQLLong is still supported for backwards compatibility. Add a test for parsing GraphQLLong larger than INT_MAX.
1 parent a5e011d commit 4542c35

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

src/main/java/graphql/Scalars.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public Object parseValue(Object input) {
3131
@Override
3232
public Object parseLiteral(Object input) {
3333
if (!(input instanceof IntValue)) return null;
34-
return ((IntValue) input).getValue();
34+
return ((IntValue) input).getValue().intValueExact();
3535
}
3636
});
3737

@@ -60,7 +60,7 @@ public Object parseLiteral(Object input) {
6060
if (input instanceof StringValue) {
6161
return Long.parseLong(((StringValue) input).getValue());
6262
} else if (input instanceof IntValue) {
63-
return ((IntValue) input).getValue();
63+
return ((IntValue) input).getValue().longValueExact();
6464
}
6565
return null;
6666
}

src/main/java/graphql/language/IntValue.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
package graphql.language;
22

33

4+
import java.math.BigInteger;
45
import java.util.ArrayList;
56
import java.util.List;
67

78
public class IntValue extends AbstractNode implements Value {
89

9-
private int value;
10+
private BigInteger value;
1011

11-
public IntValue(int value) {
12+
public IntValue(BigInteger value) {
1213
this.value = value;
1314
}
1415

15-
public int getValue() {
16+
public BigInteger getValue() {
1617
return value;
1718
}
1819

19-
public void setValue(int value) {
20+
public void setValue(BigInteger value) {
2021
this.value = value;
2122
}
2223

@@ -30,9 +31,9 @@ public boolean isEqualTo(Node o) {
3031
if (this == o) return true;
3132
if (o == null || getClass() != o.getClass()) return false;
3233

33-
IntValue intValue = (IntValue) o;
34+
IntValue that = (IntValue) o;
3435

35-
return value == intValue.value;
36+
return !(value != null ? !value.equals(that.value) : that.value != null);
3637

3738
}
3839

src/main/java/graphql/parser/GraphqlAntlrToLanguage.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.antlr.v4.runtime.ParserRuleContext;
99

1010
import java.math.BigDecimal;
11+
import java.math.BigInteger;
1112
import java.util.ArrayDeque;
1213
import java.util.Deque;
1314

@@ -321,7 +322,7 @@ public Void visitDirective(GraphqlParser.DirectiveContext ctx) {
321322

322323
private Value getValue(GraphqlParser.ValueWithVariableContext ctx) {
323324
if (ctx.IntValue() != null) {
324-
IntValue intValue = new IntValue(Integer.parseInt(ctx.IntValue().getText()));
325+
IntValue intValue = new IntValue(new BigInteger(ctx.IntValue().getText()));
325326
newNode(intValue, ctx);
326327
return intValue;
327328
} else if (ctx.FloatValue() != null) {
@@ -366,7 +367,7 @@ private Value getValue(GraphqlParser.ValueWithVariableContext ctx) {
366367

367368
private Value getValue(GraphqlParser.ValueContext ctx) {
368369
if (ctx.IntValue() != null) {
369-
IntValue intValue = new IntValue(Integer.parseInt(ctx.IntValue().getText()));
370+
IntValue intValue = new IntValue(new BigInteger(ctx.IntValue().getText()));
370371
newNode(intValue, ctx);
371372
return intValue;
372373
} else if (ctx.FloatValue() != null) {

src/test/groovy/graphql/ScalarsTest.groovy

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

5858
where:
59-
literal | result
60-
new StringValue("42") | 42
61-
new IntValue(42) | 42
59+
literal | result
60+
new StringValue("42") | 42
61+
new IntValue(42) | 42
62+
new IntValue(42345784398534785l) | 42345784398534785l
6263
}
6364

6465

0 commit comments

Comments
 (0)