Skip to content

Commit 8aedb4d

Browse files
authored
Merge branch 'master' into fix/interface-argument-defaults
2 parents d6f7f07 + 0c5d3dd commit 8aedb4d

22 files changed

Lines changed: 665 additions & 416 deletions

‎build.gradle‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def getDevelopmentVersion() {
176176
def reactiveStreamsVersion = '1.0.3'
177177
def releaseVersion = System.env.RELEASE_VERSION
178178
def antlrVersion = '4.13.2' // https://mvnrepository.com/artifact/org.antlr/antlr4-runtime
179-
def guavaVersion = '32.1.2-jre'
179+
def guavaVersion = '33.7.2-jre'
180180
version = releaseVersion ? releaseVersion : getDevelopmentVersion()
181181
group = 'com.graphql-java'
182182

@@ -406,6 +406,7 @@ def annotationsToRemoveFromShadedJar = [
406406
'Lcom/google/errorprone/annotations/ForOverride;',
407407
'Lcom/google/errorprone/annotations/InlineMe;',
408408
'Lcom/google/errorprone/annotations/InlineMeValidationDisabled;',
409+
'Lcom/google/errorprone/annotations/RestrictedApi;',
409410
'Lcom/google/errorprone/annotations/concurrent/GuardedBy;',
410411
'Lcom/google/j2objc/annotations/RetainedWith;',
411412
'Lcom/google/j2objc/annotations/Weak;',

‎src/main/java/graphql/execution/values/legacycoercing/LegacyCoercingInputInterceptor.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static boolean isLegacyStringValue(Object input) {
120120

121121
static Object coerceLegacyBooleanValue(Object input) {
122122
if (input instanceof String) {
123-
String lStr = ((String) input).toLowerCase();
123+
String lStr = ((String) input).toLowerCase(Locale.ROOT);
124124
if (lStr.equals("true")) {
125125
return true;
126126
}

‎src/main/java/graphql/i18n/I18n.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ private String msgImpl(String msgKey, Object[] msgArgs) {
8989
} catch (MissingResourceException e) {
9090
assertShouldNeverHappen("There must be a resource bundle key called %s", msgKey);
9191
}
92-
return new MessageFormat(msgPattern).format(msgArgs);
92+
// format any arguments (eg numbers) with the same locale as the message rather than the JVM default locale
93+
return new MessageFormat(msgPattern, locale).format(msgArgs);
9394
}
9495
}

‎src/main/java/graphql/language/AstPrinter.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.Iterator;
1212
import java.util.LinkedHashMap;
1313
import java.util.List;
14+
import java.util.Locale;
1415
import java.util.Map;
1516

1617
import static graphql.Assert.assertShouldNeverHappen;
@@ -392,7 +393,7 @@ private NodePrinter<OperationDefinition> operationDefinition() {
392393
} else {
393394
description(out, node);
394395
OperationDefinition.Operation op = node.getOperation();
395-
out.append(op.toString().toLowerCase());
396+
out.append(op.toString().toLowerCase(Locale.ROOT));
396397
if (!isEmpty(name)) {
397398
out.append(' ');
398399
out.append(name);

‎src/main/java/graphql/parser/Parser.java‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
import graphql.parser.exceptions.ParseCancelledTooManyCharsException;
1818
import graphql.parser.exceptions.ParseCancelledTooManyNumericLiteralCharactersException;
1919
import org.antlr.v4.runtime.BaseErrorListener;
20-
import org.antlr.v4.runtime.CharStreams;
20+
import org.antlr.v4.runtime.CodePointBuffer;
2121
import org.antlr.v4.runtime.CodePointCharStream;
2222
import org.antlr.v4.runtime.CommonTokenStream;
23+
import org.antlr.v4.runtime.IntStream;
2324
import org.antlr.v4.runtime.ParserRuleContext;
2425
import org.antlr.v4.runtime.RecognitionException;
2526
import org.antlr.v4.runtime.Recognizer;
@@ -32,6 +33,7 @@
3233
import java.io.IOException;
3334
import java.io.Reader;
3435
import java.io.UncheckedIOException;
36+
import java.nio.CharBuffer;
3537
import java.util.List;
3638
import java.util.Optional;
3739
import java.util.function.BiConsumer;
@@ -309,15 +311,51 @@ private static SafeTokenReader setupSafeTokenReader(ParserEnvironment environmen
309311
return new SafeTokenReader(multiSourceReader, maxCharacters, onTooManyCharacters);
310312
}
311313

314+
// matches the chunk size ANTLR's CharStreams.fromReader() uses internally
315+
private static final int CHAR_STREAM_BUFFER_SIZE = 4096;
316+
312317
@NonNull
313318
private static CodePointCharStream setupCharStream(SafeTokenReader safeTokenReader) {
314-
CodePointCharStream charStream;
315319
try {
316-
charStream = CharStreams.fromReader(safeTokenReader);
320+
return readCodePointCharStream(safeTokenReader, CHAR_STREAM_BUFFER_SIZE);
317321
} catch (IOException e) {
318322
throw new UncheckedIOException(e);
319323
}
320-
return charStream;
324+
}
325+
326+
/**
327+
* This mirrors {@link CharStreams#fromReader(Reader)}, except that a surrogate pair split across
328+
* two chunk reads is held back and carried into the next chunk instead of being mis-decoded.
329+
* <p>
330+
* ANTLR's {@link CodePointBuffer.Builder#append(CharBuffer)} keeps a pending high surrogate across
331+
* calls, but its flush at the end of a chunk fails to clear that pending state, so a surrogate pair
332+
* split across two {@code append()} calls gets decoded into two separate code points instead of one.
333+
* Never handing it a chunk that ends on an unpaired high surrogate avoids the bug entirely.
334+
*
335+
* @see <a href="https://github.com/antlr/antlr4/pull/4943">antlr/antlr4#4943</a>
336+
*/
337+
private static CodePointCharStream readCodePointCharStream(Reader reader, int bufferSize) throws IOException {
338+
CodePointBuffer.Builder builder = CodePointBuffer.builder(bufferSize);
339+
CharBuffer charBuffer = CharBuffer.allocate(bufferSize);
340+
try {
341+
while (reader.read(charBuffer) != -1) {
342+
charBuffer.flip();
343+
int realLimit = charBuffer.limit();
344+
if (realLimit > charBuffer.position() && Character.isHighSurrogate(charBuffer.get(realLimit - 1))) {
345+
charBuffer.limit(realLimit - 1);
346+
}
347+
builder.append(charBuffer);
348+
charBuffer.limit(realLimit);
349+
charBuffer.compact();
350+
}
351+
charBuffer.flip();
352+
if (charBuffer.hasRemaining()) {
353+
builder.append(charBuffer);
354+
}
355+
return CodePointCharStream.fromBuffer(builder.build(), IntStream.UNKNOWN_SOURCE_NAME);
356+
} finally {
357+
reader.close();
358+
}
321359
}
322360

323361
@NonNull

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private Boolean convertImpl(Object input) {
3131
if (input instanceof Boolean) {
3232
return (Boolean) input;
3333
} else if (input instanceof String) {
34-
String lStr = ((String) input).toLowerCase();
34+
String lStr = ((String) input).toLowerCase(Locale.ROOT);
3535
if (lStr.equals("true")) {
3636
return true;
3737
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,25 @@ private Double parseValueImpl(@NonNull Object input, @NonNull Locale locale) {
8282
}
8383

8484
private static double parseLiteralImpl(@NonNull Object input, @NonNull Locale locale) {
85+
double result;
8586
if (input instanceof IntValue) {
86-
return ((IntValue) input).getValue().doubleValue();
87+
result = ((IntValue) input).getValue().doubleValue();
8788
} else if (input instanceof FloatValue) {
88-
return ((FloatValue) input).getValue().doubleValue();
89+
result = ((FloatValue) input).getValue().doubleValue();
8990
} else {
9091
throw new CoercingParseLiteralException(
9192
i18nMsg(locale, "Float.unexpectedAstType", typeName(input))
9293
);
9394
}
95+
// the GraphQL Float spec requires non-finite values (NaN and Infinity) to raise an error on
96+
// input coercion, same as convertImpl above. a literal like 1e400 overflows BigDecimal.doubleValue()
97+
// to Infinity, so it has to be rejected here rather than flowing through to a data fetcher.
98+
if (Double.isNaN(result) || Double.isInfinite(result)) {
99+
throw new CoercingParseLiteralException(
100+
i18nMsg(locale, "Float.notFinite")
101+
);
102+
}
103+
return result;
94104
}
95105

96106
@NonNull

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public class GraphqlIntCoercing implements Coercing<Integer, Integer> {
3131
private static final BigInteger INT_MAX = BigInteger.valueOf(Integer.MAX_VALUE);
3232
private static final BigInteger INT_MIN = BigInteger.valueOf(Integer.MIN_VALUE);
3333

34+
// Integer.MAX_VALUE / Integer.MIN_VALUE both have 10 significant digits
35+
private static final int INT_MAX_DIGITS = 10;
36+
3437
private Integer convertImpl(Object input) {
3538
if (input instanceof Integer) {
3639
return (Integer) input;
@@ -74,7 +77,7 @@ private Integer parseValueImpl(@NonNull Object input, @NonNull Locale locale) {
7477
return (Integer) input;
7578
}
7679

77-
BigInteger result = convertParseValueImpl(input);
80+
BigInteger result = convertParseValueImpl(input, locale);
7881
if (result == null) {
7982
throw new CoercingParseValueException(
8083
i18nMsg(locale, "Int.notInt", typeName(input))
@@ -88,14 +91,26 @@ private Integer parseValueImpl(@NonNull Object input, @NonNull Locale locale) {
8891
return result.intValueExact();
8992
}
9093

91-
private BigInteger convertParseValueImpl(Object input) {
94+
private BigInteger convertParseValueImpl(Object input, @NonNull Locale locale) {
9295
BigDecimal value;
9396
try {
9497
value = new BigDecimal(input.toString());
9598
} catch (NumberFormatException e) {
9699
return null;
97100
}
98101

102+
// A value whose integer part has more digits than Integer can hold is out of range by
103+
// definition. Decide that from the decimal's scale/precision instead of letting
104+
// toBigIntegerExact() expand it: a variable value like 1E1000000000 arrives here as a
105+
// BigDecimal and would otherwise materialise a BigInteger with ~10^9 digits (CPU/heap blow
106+
// up) before the range check in parseValueImpl ever runs. Use long math because scale can
107+
// be close to Integer.MIN_VALUE.
108+
if ((long) value.precision() - value.scale() > INT_MAX_DIGITS) {
109+
throw new CoercingParseValueException(
110+
i18nMsg(locale, "Int.outsideRange", value.toString())
111+
);
112+
}
113+
99114
try {
100115
return value.toBigIntegerExact();
101116
} catch (ArithmeticException e) {

‎src/main/java/graphql/schema/fetching/LambdaFetchingSupport.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.ArrayList;
1515
import java.util.Comparator;
1616
import java.util.List;
17+
import java.util.Locale;
1718
import java.util.Optional;
1819
import java.util.function.Function;
1920
import java.util.function.Predicate;
@@ -193,7 +194,7 @@ private static String decapitalize(String name) {
193194
if (name.length() == 0) {
194195
return name;
195196
}
196-
return name.substring(0, 1).toLowerCase() + name.substring(1);
197+
return name.substring(0, 1).toLowerCase(Locale.ROOT) + name.substring(1);
197198
}
198199

199200

‎src/main/java/graphql/schema/idl/SchemaGeneratorAppliedDirectiveHelper.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.util.ArrayList;
2424
import java.util.List;
25+
import java.util.Locale;
2526
import java.util.Map;
2627
import java.util.Optional;
2728
import java.util.Set;
@@ -272,7 +273,7 @@ private static List<Directive> combineDirectives(List<Directive> directives, Lis
272273

273274
private static List<Introspection.DirectiveLocation> buildLocations(DirectiveDefinition directiveDefinition) {
274275
return map(directiveDefinition.getDirectiveLocations(),
275-
dl -> Introspection.DirectiveLocation.valueOf(dl.getName().toUpperCase()));
276+
dl -> Introspection.DirectiveLocation.valueOf(dl.getName().toUpperCase(Locale.ROOT)));
276277
}
277278

278279
static GraphQLArgument buildDirectiveArgumentDefinitionFromAst(SchemaGeneratorHelper.BuildContext buildCtx, InputValueDefinition valueDefinition, Function<Type<?>, GraphQLInputType> inputTypeFactory) {

0 commit comments

Comments
 (0)