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

Fix json string encoding #122

Merged
merged 2 commits into from
Jun 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions example/todo/todo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"net/http/httptest"
"testing"

"github.com/vektah/gqlgen/client"
"github.com/vektah/gqlgen/neelance/introspection"

"github.com/stretchr/testify/require"
"github.com/vektah/gqlgen/client"
"github.com/vektah/gqlgen/handler"
"github.com/vektah/gqlgen/neelance/introspection"
)

func TestTodo(t *testing.T) {
Expand Down
32 changes: 31 additions & 1 deletion graphql/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,39 @@ import (
"strconv"
)

const encodeHex = "0123456789ABCDEF"

func MarshalString(s string) Marshaler {
return WriterFunc(func(w io.Writer) {
io.WriteString(w, strconv.Quote(s))
start := 0
io.WriteString(w, `"`)

for i, c := range s {
if c < 0x20 || c == '\\' || c == '"' {
io.WriteString(w, s[start:i])

switch c {
case '\t':
io.WriteString(w, `\t`)
case '\r':
io.WriteString(w, `\r`)
case '\n':
io.WriteString(w, `\n`)
case '\\':
io.WriteString(w, `\\`)
case '"':
io.WriteString(w, `\"`)
default:
io.WriteString(w, `\u00`)
w.Write([]byte{encodeHex[c>>4], encodeHex[c&0xf]})
}

start = i + 1
}
}

io.WriteString(w, s[start:])
io.WriteString(w, `"`)
})
}
func UnmarshalString(v interface{}) (string, error) {
Expand Down
27 changes: 27 additions & 0 deletions graphql/string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package graphql

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
)

func TestString(t *testing.T) {
assert.Equal(t, `"hello"`, doStrMarshal("hello"))
assert.Equal(t, `"he\tllo"`, doStrMarshal("he\tllo"))
assert.Equal(t, `"he\tllo"`, doStrMarshal("he llo"))
assert.Equal(t, `"he\nllo"`, doStrMarshal("he\nllo"))
assert.Equal(t, `"he\r\nllo"`, doStrMarshal("he\r\nllo"))
assert.Equal(t, `"he\\llo"`, doStrMarshal(`he\llo`))
assert.Equal(t, `"quotes\"nested\"in\"quotes\""`, doStrMarshal(`quotes"nested"in"quotes"`))
assert.Equal(t, `"\u0000"`, doStrMarshal("\u0000"))
assert.Equal(t, `"\u0000"`, doStrMarshal("\u0000"))
assert.Equal(t, "\"\U000fe4ed\"", doStrMarshal("\U000fe4ed"))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit/curios: why double quotes around expected val here?

Copy link
Collaborator Author

@vektah vektah May 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because raw strings can't have escaped literals, and I didn't want the raw unicode fe4ed in the source document.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just pointing out that you are using double quotes around expected string in the last test case vs. back ticks for all other test cases. It seems semantically it is all the same.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

}

func doStrMarshal(s string) string {
var buf bytes.Buffer
MarshalString(s).MarshalGQL(&buf)
return buf.String()
}
5 changes: 3 additions & 2 deletions handler/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,12 @@ func GraphQL(exec graphql.ExecutableSchema, options ...Option) http.HandlerFunc
return
}

ctx := graphql.WithRequestContext(r.Context(), cfg.newRequestContext(doc, reqParams.Query, reqParams.Variables))
reqCtx := cfg.newRequestContext(doc, reqParams.Query, reqParams.Variables)
ctx := graphql.WithRequestContext(r.Context(), reqCtx)

defer func() {
if err := recover(); err != nil {
userErr := cfg.recover(ctx, err)
userErr := reqCtx.Recover(ctx, err)
sendErrorf(w, http.StatusUnprocessableEntity, userErr.Error())
}
}()
Expand Down
5 changes: 3 additions & 2 deletions handler/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ func (c *wsConnection) subscribe(message *operationMessage) bool {
return true
}

ctx := graphql.WithRequestContext(c.ctx, c.cfg.newRequestContext(doc, reqParams.Query, reqParams.Variables))
reqCtx := c.cfg.newRequestContext(doc, reqParams.Query, reqParams.Variables)
ctx := graphql.WithRequestContext(c.ctx, reqCtx)

if op.Type != query.Subscription {
var result *graphql.Response
Expand All @@ -176,7 +177,7 @@ func (c *wsConnection) subscribe(message *operationMessage) bool {
go func() {
defer func() {
if r := recover(); r != nil {
userErr := c.cfg.recover(ctx, r)
userErr := reqCtx.Recover(ctx, r)
c.sendError(message.ID, &errors.QueryError{Message: userErr.Error()})
}
}()
Expand Down
34 changes: 34 additions & 0 deletions test/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions test/resolvers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,28 @@ func TestInputDefaults(t *testing.T) {
require.True(t, called)
}

func TestJsonEncoding(t *testing.T) {
srv := httptest.NewServer(handler.GraphQL(MakeExecutableSchema(&testResolvers{})))
c := client.New(srv.URL)

var resp struct {
JsonEncoding string
}

err := c.Post(`{ jsonEncoding }`, &resp)
require.NoError(t, err)
require.Equal(t, "\U000fe4ed", resp.JsonEncoding)
}

type testResolvers struct {
err error
queryDate func(ctx context.Context, filter models.DateFilter) (bool, error)
}

func (r *testResolvers) Query_jsonEncoding(ctx context.Context) (string, error) {
return "\U000fe4ed", nil
}

func (r *testResolvers) Query_viewer(ctx context.Context) (*Viewer, error) {
return &Viewer{
User: &remote_api.User{"Bob"},
Expand Down
1 change: 1 addition & 0 deletions test/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Query {
path: [Element]
date(filter: DateFilter!): Boolean!
viewer: Viewer
jsonEncoding: String!
}

// this is a comment with a `backtick`