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

Error in datasource when querying using triple quotes syntax containing quotes or newlines #839

Closed
mattjohnsonpint opened this issue Jun 28, 2024 · 4 comments · Fixed by #843
Labels
internally-reviewed Internally reviewed

Comments

@mattjohnsonpint
Copy link
Contributor

mattjohnsonpint commented Jun 28, 2024

Given a schema such as:

type Query {
    foo(bar: String):String
}

I can accept queries like this:

query {
  foo(bar: "abc")
}

or:

query {
  foo(bar: "abc\ndef")
}

or with variables (containing any newlines or not):

query MyQuery($bar: String){
  foo(bar: $bar)
}

But I believe I should be able to also do this, and it fails:

query {
  foo(bar: 
"""
abc
def
""")
}

I'm using a custom data source with a JsonVariableRenderer.

With some trace debugging, I believe the problem is here:

case ValueKindString:
buf.Write(quotes.WrapBytes(d.StringValueContentBytes(value.Ref)))

Since the string value returned by d.StringValueContentBytes(value.Ref) contains a raw newline character, and it's only wrapping it in quotes, the resulting value doesn't have the newline escaped as "\n" to be valid JSON.

The failure occurs in my custom datasource at the start of the Load function when I try to unmarshal the input string. It should be valid JSON, but in this case it's malformed.

A minimal repro will take some time if you need one, but I thought I'd report it in the meantime. Thanks.

@mattjohnsonpint
Copy link
Contributor Author

For now I've worked around it with:

input = bytes.ReplaceAll(input, []byte("\n"), []byte("\\n"))

... in my datasource before I parse. But this should probably be done earlier. Thanks.

@mattjohnsonpint
Copy link
Contributor Author

mattjohnsonpint commented Jul 1, 2024

An update, it's not just newlines. In a """ block I can also have raw quotes, and those don't get escaped properly either.

query {
  foo(bar: 
"""
a"bc
""")
}

I can't workaround this case easily either, because the outer JSON quotes would need to remain unescaped.

@mattjohnsonpint mattjohnsonpint changed the title Error in datasource when querying with multiline string input using triple quotes syntax Error in datasource when querying using triple quotes syntax containing quotes or newlines Jul 1, 2024
@StarpTech
Copy link
Collaborator

Hi @mattjohnsonpint, great catch! Would you be open to create a PR?

@mattjohnsonpint
Copy link
Contributor Author

@StarpTech - PR created: #843

devsergiy added a commit that referenced this issue Jul 15, 2024
Fixes #839 

Note, I considered using `gjson.AppendJSONString` for better
performance, but it currently doesn't have an option to disable escaping
of HTML characters - which I think would be undesirable here. See
tidwall/gjson#362

---------

Co-authored-by: Sergiy 🇺🇦 <[email protected]>
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

Successfully merging a pull request may close this issue.

2 participants