Skip to content

Commit

Permalink
fix(Mutation): Deeply-nested uid facets (#7455)
Browse files Browse the repository at this point in the history
* only run `parseMapFacets` for JSON scalar arrays

(cherry picked from commit 5049092)
  • Loading branch information
nelsonpecora authored and danielmai committed Feb 18, 2021
1 parent 0ad6375 commit e69540c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
20 changes: 14 additions & 6 deletions chunker/json_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,14 +500,22 @@ func (buf *NQuadBuffer) mapToNquads(m map[string]interface{}, op int, parentPred
buf.PushPredHint(pred, pb.Metadata_SINGLE)
case []interface{}:
buf.PushPredHint(pred, pb.Metadata_LIST)
// TODO(Ashish): We need to call this only in case of scalarlist, for other lists
// this can be avoided.
facetsMapSlice, err := parseMapFacets(m, prefix)
if err != nil {
return mr, err
}

// NOTE: facetsMapSlice should be empty unless this is a scalar list
var facetsMapSlice []map[int]*api.Facet
for idx, item := range v {
if idx == 0 {
switch item.(type) {
case string, float64, json.Number, int64:
var err error
facetsMapSlice, err = parseMapFacets(m, prefix)
if err != nil {
return mr, err
}
default:
}
}

nq := api.NQuad{
Subject: mr.uid,
Predicate: pred,
Expand Down
32 changes: 32 additions & 0 deletions chunker/json_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,38 @@ func TestNquadsFromJsonFacets4(t *testing.T) {
}
}

func TestNquadsFromJsonFacets5(t *testing.T) {
// Dave has uid facets which should go on the edge between Alice and Dave,
// AND Emily has uid facets which should go on the edge between Dave and Emily
json := `[
{
"name": "Alice",
"friend": [
{
"name": "Dave",
"friend|close": true,
"friend": [
{
"name": "Emily",
"friend|close": true
}
]
}
]
}
]`

nq, err := Parse([]byte(json), SetNquads)
require.NoError(t, err)
require.Equal(t, 5, len(nq))
checkCount(t, nq, "friend", 1)

fastNQ, err := FastParse([]byte(json), SetNquads)
require.NoError(t, err)
require.Equal(t, 5, len(fastNQ))
checkCount(t, fastNQ, "friend", 1)
}

func TestNquadsFromJsonError1(t *testing.T) {
p := Person{
Name: "Alice",
Expand Down

0 comments on commit e69540c

Please sign in to comment.