Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String literal input value is trimming whitespace #937

Open
mattjohnsonpint opened this issue Oct 25, 2024 · 3 comments
Open

String literal input value is trimming whitespace #937

mattjohnsonpint opened this issue Oct 25, 2024 · 3 comments
Assignees
Labels
internally-reviewed Internally reviewed

Comments

@mattjohnsonpint
Copy link
Contributor

If I have an operation with a string literal input value that has leading or trailing whitespace, such as:

query {
    test(s: "  abcd  ")
}

... the string gets trimmed to just "abcd" when the operation is parsed.

If instead I pass as a variable, then the string is not trimmed.

According to the GraphQL Spec:

... White space and other otherwise-ignored characters are significant within a string value. ...

Thus I would expect to never have the string input trimmed of whitespace.

I believe this is happening because lexer.readSingleLineString is designed to seek out whitespace and exclude it from the string. IMHO it should not.

func (l *Lexer) readSingleLineString(tok *token.Token) {
tok.Keyword = keyword.STRING
tok.SetStart(l.input.InputPosition, l.input.TextPosition)
tok.TextPosition.CharStart -= 1
escaped := false
whitespaceCount := 0
reachedFirstNonWhitespace := false
leadingWhitespaceToken := 0
for {
next := l.readRune()
switch next {
case runes.SPACE, runes.TAB:
escaped = false
whitespaceCount++
case runes.EOF:
tok.SetEnd(l.input.InputPosition, l.input.TextPosition)
tok.Literal.Start += uint32(leadingWhitespaceToken)
tok.Literal.End -= uint32(whitespaceCount)
return
case runes.QUOTE, runes.CARRIAGERETURN, runes.LINETERMINATOR:
if escaped {
escaped = !escaped
continue
}
tok.SetEnd(l.input.InputPosition-1, l.input.TextPosition)
tok.Literal.Start += uint32(leadingWhitespaceToken)
tok.Literal.End -= uint32(whitespaceCount)
return
case runes.BACKSLASH:
escaped = !escaped
whitespaceCount = 0
default:
if !reachedFirstNonWhitespace {
reachedFirstNonWhitespace = true
leadingWhitespaceToken = whitespaceCount
}
escaped = false
whitespaceCount = 0
}
}

Thanks.

@jensneuse
Copy link
Member

From a first glance it looks like you're right.

@StarpTech
Copy link
Collaborator

@devsergiy please take a look.

@StarpTech StarpTech added the internally-reviewed Internally reviewed label Nov 5, 2024
@mmaxim2710
Copy link

mmaxim2710 commented Nov 8, 2024

Faced the same issue in my project. An interesting observation: if you substitute values not directly, but through query variables, then spaces in strings are not cut off

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internally-reviewed Internally reviewed
Projects
None yet
Development

No branches or pull requests

5 participants