File tree Expand file tree Collapse file tree
test/groovy/graphql/parser Expand file tree Collapse file tree Original file line number Diff line number Diff line change 5353import graphql .parser .antlr .GraphqlBaseVisitor ;
5454import graphql .parser .antlr .GraphqlParser ;
5555import org .antlr .v4 .runtime .CommonTokenStream ;
56+ import org .antlr .v4 .runtime .IntStream ;
5657import org .antlr .v4 .runtime .ParserRuleContext ;
5758import 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
Original file line number Diff line number Diff line change 55import graphql .parser .antlr .GraphqlLexer ;
66import graphql .parser .antlr .GraphqlParser ;
77import org .antlr .v4 .runtime .BailErrorStrategy ;
8+ import org .antlr .v4 .runtime .CharStream ;
89import org .antlr .v4 .runtime .CharStreams ;
910import org .antlr .v4 .runtime .CommonTokenStream ;
10- import org .antlr .v4 .runtime .IntStream ;
1111import org .antlr .v4 .runtime .Token ;
1212import org .antlr .v4 .runtime .atn .PredictionMode ;
1313import org .antlr .v4 .runtime .misc .ParseCancellationException ;
1414
1515import java .util .List ;
16- import java .util .Optional ;
1716
1817@ Internal
1918public 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
Original file line number Diff line number Diff line change @@ -34,7 +34,6 @@ import graphql.language.TypeName
3434import graphql.language.UnionTypeDefinition
3535import graphql.language.UnionTypeExtensionDefinition
3636import graphql.language.VariableReference
37- import org.antlr.v4.runtime.IntStream
3837import spock.lang.Specification
3938
4039import 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 }
You can’t perform that action at this time.
0 commit comments