Skip to content

Commit 648032e

Browse files
jimkyndemeyerbbakerman
authored andcommitted
Ensured that Antlr's '<unknown>' source name isn't sent to clients as part of operation responses by setting it to null
1 parent 58bf17e commit 648032e

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import graphql.parser.antlr.GraphqlBaseVisitor;
5454
import graphql.parser.antlr.GraphqlParser;
5555
import org.antlr.v4.runtime.CommonTokenStream;
56+
import org.antlr.v4.runtime.IntStream;
5657
import org.antlr.v4.runtime.ParserRuleContext;
5758
import org.antlr.v4.runtime.Token;
5859

@@ -831,6 +832,12 @@ private Description newDescription(GraphqlParser.DescriptionContext descriptionC
831832
private SourceLocation getSourceLocation(ParserRuleContext parserRuleContext) {
832833
Token startToken = parserRuleContext.getStart();
833834
String sourceName = startToken.getTokenSource().getSourceName();
835+
if (IntStream.UNKNOWN_SOURCE_NAME.equals(sourceName)) {
836+
// UNKNOWN_SOURCE_NAME is Antrl's way of indicating that no source name was given during parsing --
837+
// which is the case when queries and other operations are parsed. We don't want this hardcoded
838+
// '<unknown>' sourceName to leak to clients when the response is serialized as JSON, so we null it.
839+
sourceName = null;
840+
}
834841
return new SourceLocation(startToken.getLine(), startToken.getCharPositionInLine() + 1, sourceName);
835842
}
836843

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
import graphql.parser.antlr.GraphqlLexer;
66
import graphql.parser.antlr.GraphqlParser;
77
import org.antlr.v4.runtime.BailErrorStrategy;
8+
import org.antlr.v4.runtime.CharStream;
89
import org.antlr.v4.runtime.CharStreams;
910
import org.antlr.v4.runtime.CommonTokenStream;
10-
import org.antlr.v4.runtime.IntStream;
1111
import org.antlr.v4.runtime.Token;
1212
import org.antlr.v4.runtime.atn.PredictionMode;
1313
import org.antlr.v4.runtime.misc.ParseCancellationException;
1414

1515
import java.util.List;
16-
import java.util.Optional;
1716

1817
@Internal
1918
public class Parser {
@@ -24,7 +23,14 @@ public Document parseDocument(String input) {
2423

2524
public Document parseDocument(String input, String sourceName) {
2625

27-
GraphqlLexer lexer = new GraphqlLexer(CharStreams.fromString(input, Optional.ofNullable(sourceName).orElse(IntStream.UNKNOWN_SOURCE_NAME)));
26+
CharStream charStream;
27+
if(sourceName == null) {
28+
charStream = CharStreams.fromString(input);
29+
} else{
30+
charStream = CharStreams.fromString(input, sourceName);
31+
}
32+
33+
GraphqlLexer lexer = new GraphqlLexer(charStream);
2834

2935
CommonTokenStream tokens = new CommonTokenStream(lexer);
3036

src/test/groovy/graphql/parser/IDLParserTest.groovy

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import graphql.language.TypeName
3434
import graphql.language.UnionTypeDefinition
3535
import graphql.language.UnionTypeExtensionDefinition
3636
import graphql.language.VariableReference
37-
import org.antlr.v4.runtime.IntStream
3837
import spock.lang.Specification
3938

4039
import java.util.stream.Collectors
@@ -809,8 +808,8 @@ input Gun {
809808

810809
then:
811810

812-
defaultDoc.definitions[0].sourceLocation.sourceName == IntStream.UNKNOWN_SOURCE_NAME
813-
namedDocNull.definitions[0].sourceLocation.sourceName == IntStream.UNKNOWN_SOURCE_NAME
811+
defaultDoc.definitions[0].sourceLocation.sourceName == null
812+
namedDocNull.definitions[0].sourceLocation.sourceName == null
814813
namedDoc.definitions[0].sourceLocation.sourceName == sourceName
815814

816815
}

0 commit comments

Comments
 (0)