Skip to content

Commit

Permalink
Merge pull request #587 from pkosiec/fix-json-string-arg
Browse files Browse the repository at this point in the history
Fix quotation during printing StringValue node
  • Loading branch information
chris-ramon authored Apr 11, 2021
2 parents 1a9db88 + 18f6ed0 commit 8a92e97
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion language/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package printer

import (
"fmt"
"strconv"
"strings"

"reflect"
Expand Down Expand Up @@ -372,7 +373,7 @@ var printDocASTReducer = map[string]visitor.VisitFunc{
"StringValue": func(p visitor.VisitFuncParams) (string, interface{}) {
switch node := p.Node.(type) {
case *ast.StringValue:
return visitor.ActionUpdate, `"` + fmt.Sprintf("%v", node.Value) + `"`
return visitor.ActionUpdate, strconv.Quote(node.Value)
case map[string]interface{}:
return visitor.ActionUpdate, `"` + getMapValueString(node, "Value") + `"`
}
Expand Down
14 changes: 14 additions & 0 deletions language/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,17 @@ fragment frag on Follower {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, results))
}
}

func TestPrinter_CorrectlyPrintsStringArgumentsWithProperQuoting(t *testing.T) {
queryAst := `query { foo(jsonStr: "{\"foo\": \"bar\"}") }`
expected := `{
foo(jsonStr: "{\"foo\": \"bar\"}")
}
`
astDoc := parse(t, queryAst)
results := printer.Print(astDoc)

if !reflect.DeepEqual(expected, results) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, results))
}
}

0 comments on commit 8a92e97

Please sign in to comment.