Skip to content

Commit 64934d6

Browse files
committed
Add separate exception message for values outside integer range
1 parent 2c514ca commit 64934d6

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,45 @@ 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 Integer) {
68+
return (Integer) input;
69+
}
70+
6771
if (!(input instanceof Number)) {
6872
throw new CoercingParseValueException(
6973
i18nMsg(locale, "Int.notInt", typeName(input))
7074
);
7175
}
7276

73-
Integer result = convertImpl(input);
77+
BigInteger result = convertParseValueImpl(input);
7478
if (result == null) {
7579
throw new CoercingParseValueException(
7680
i18nMsg(locale, "Int.notInt", typeName(input))
7781
);
7882
}
79-
return result;
83+
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+
}
80106
}
81107

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

0 commit comments

Comments
 (0)