Skip to content

Commit 5a7d7db

Browse files
committed
Handle quotes while building upsert query
1 parent 237f6b6 commit 5a7d7db

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

dgraph/cmd/alpha/upsert_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2998,3 +2998,23 @@ func TestLargeStringIndex(t *testing.T) {
29982998
require.Contains(t, dqlSchema,
29992999
`{"predicate":"name_term","type":"string","index":true,"tokenizer":["term"]}`)
30003000
}
3001+
3002+
func TestStringWithQuote(t *testing.T) {
3003+
require.NoError(t, dropAll())
3004+
require.NoError(t, alterSchemaWithRetry(`name: string @unique @index(exact) .`))
3005+
mu := `{ set { <0x01> <name> "\"problem\" is the quotes (json)" . } }`
3006+
require.NoError(t, runMutation(mu))
3007+
3008+
var data struct {
3009+
Data struct {
3010+
Q []struct {
3011+
Name string `json:"name"`
3012+
} `json:"q"`
3013+
} `json:"data"`
3014+
}
3015+
q := `{ q(func: has(name)) { name } }`
3016+
res, _, err := queryWithTs(queryInp{body: q, typ: "application/dql"})
3017+
require.NoError(t, err)
3018+
require.NoError(t, json.Unmarshal([]byte(res), &data))
3019+
require.Equal(t, `"problem" is the quotes (json)`, data.Data.Q[0].Name)
3020+
}

edgraph/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,8 +1746,8 @@ func addQueryIfUnique(qctx context.Context, qc *queryContext) error {
17461746
// in the mutation, then we reject the mutation.
17471747

17481748
if !strings.HasPrefix(pred.ObjectId, "val(") {
1749-
query := fmt.Sprintf(`%v as var(func: eq(%v,"%v"))`, queryVar, predicateName,
1750-
dql.TypeValFrom(pred.ObjectValue).Value)
1749+
val := strconv.Quote(fmt.Sprintf("%v", dql.TypeValFrom(pred.ObjectValue).Value))
1750+
query := fmt.Sprintf(`%v as var(func: eq(%v,"%v"))`, queryVar, predicateName, val[1:len(val)-1])
17511751
if _, err := buildQuery.WriteString(query); err != nil {
17521752
return errors.Wrapf(err, "error while writing string")
17531753
}

0 commit comments

Comments
 (0)