-
Notifications
You must be signed in to change notification settings - Fork 125
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
Better handling of string lexing #140
base: master
Are you sure you want to change the base?
Conversation
… the cases easier to read
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also review the notes in the PR message above and give me an idea what the plans are wrt handling bad inputs. |
Some clarifications needed. 2.I also have doubt in your implementation.
|
@chewxy Sorry for the long delay. I'm looking this over, but I'm happy to merge this if you address the concerns of @JatinDev543 |
Better handling of string lexing vektah#140
The Problem
Given a schema like this:
Here's a way to break a mutation:
giving rise to the following error:
The issue is that
\xaa
is not parsed correctly.The Solution
The solution is quite simple: add a case to the
readString()
method of the lexer to handlex
as a escape character.Side Note: How Parsing of Strings Should Work
Please note that the handling of bad inputs for
\xHH
(whereH
is a meta-variable standing in for a hexadecimal number) is not quite the same as elsewhere in the package. I've got a good reason for this, and I am willing to make changes to the other escape sequences as well.With this PR, the bad inputs will lead to the literals being returned - so that
"\xZZ"
will return"\xZZ"
, while good inputs such as"\xaa"
will be parsed correctly, returning"ª"
.I believe this is more user friendly.
In the example I had listed, the scenario is one where the server is receiving a mutation. The input string, could be an end user input. And end users do often type the wrong things.
For example, consider a dyslexic person trying to write the sentence "they will give it to me/u". Said person would often type something along the lines of "they will give it to me\u". In this case, the extra parsing for UTF-8 characters in the string will cause this input to fail. What the user meant to type, in a string representation, is
"they will give it to me\\u"
.An argument could be made that it is the onus of the user of this library to escape the string
"they will give it to me\u"
to"they will give it to me\\u"
. My counter argument is that the role of a lexer is to simply find tokens. A string token that contains the literal bytes in"they will give it to me\u"
would qualify as a string token. That gqlparser goes above and beyond in order to parse out the UTF8 characters in the contents of a string is most commendable.But it should not return an error. In the example I have given so far, it would be very unfriendly to the end user, as well as the user of the library.
There can be a further argument to be made - that having the user of this library parse the string and escape any invalid sequences would be extra computation wasted. Now as a total, the program has to go through the string twice - once to escape bad sequences (done by the user of this library), and the second, to parse the correct escape sequences into UTF8 chars (done by this library). If we could save computation by doing all at once, we would make the world a much nicer place.
As I mentioned, I am willing to put the changes in for handling the rest of the bad escape sequences. I am also OK if you want to just keep returning errors for string parsing, and will modify this PR. Your call @vektah .
End Notes
This was originally filed as an issue in Dgraph's community forum: https://discuss.dgraph.io/t/graphql-string-semantics-unclear-and-inconsistent-in-the-presence-of-escape-sequences/12019