Skip to content

Commit 34ae905

Browse files
fix(core): Fix regression in parsing json empty string (#9108)
Currently when we parse an empty string, a json empty array is returned. This is a regression. This diff fixes it. We disallow any vector of empty length.
1 parent 3486cf4 commit 34ae905

File tree

2 files changed

+133
-1
lines changed

2 files changed

+133
-1
lines changed

chunker/json_parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func handleBasicType(k string, v interface{}, op int, nq *api.NQuad) error {
230230
return nil
231231
}
232232

233-
if vf, err := types.ParseVFloat(v); err == nil {
233+
if vf, err := types.ParseVFloat(v); err == nil && len(vf) != 0 {
234234
nq.ObjectValue = &api.Value{Val: &api.Value_Vfloat32Val{Vfloat32Val: types.FloatArrayAsBytes(vf)}}
235235
return nil
236236
}

chunker/json_parser_test.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ type Person struct {
7474
School *School `json:"school,omitempty"`
7575
}
7676

77+
type Product struct {
78+
Uid string `json:"uid,omitempty"`
79+
Name string `json:"name"`
80+
Discription string `json:"discription"`
81+
Discription_v string `json:"discription_v"`
82+
}
83+
7784
func Parse(b []byte, op int) ([]*api.NQuad, error) {
7885
nqs := NewNQuadBuffer(1000)
7986
err := nqs.ParseJSON(b, op)
@@ -1380,3 +1387,128 @@ func BenchmarkNoFacetsFast(b *testing.B) {
13801387
_, _ = FastParse(json, SetNquads)
13811388
}
13821389
}
1390+
1391+
func TestNquadsEmptyStringFromJson(t *testing.T) {
1392+
json := `[{"name":""}]`
1393+
1394+
nq, err := Parse([]byte(json), SetNquads)
1395+
require.NoError(t, err)
1396+
1397+
fastNQ, err := FastParse([]byte(json), SetNquads)
1398+
require.NoError(t, err)
1399+
1400+
// The string value should be empty.
1401+
require.Equal(t, nq[0].ObjectValue.GetStrVal(), "")
1402+
require.Equal(t, fastNQ[0].ObjectValue.GetStrVal(), "")
1403+
}
1404+
1405+
func TestNquadsJsonEmptyStringVectorPred(t *testing.T) {
1406+
p := Product{
1407+
Uid: "1",
1408+
Name: "",
1409+
Discription_v: "",
1410+
}
1411+
1412+
b, err := json.Marshal([]Product{p})
1413+
require.NoError(t, err)
1414+
1415+
nq, err := Parse(b, SetNquads)
1416+
require.NoError(t, err)
1417+
require.Equal(t, 3, len(nq))
1418+
1419+
fastNQ, err := FastParse(b, SetNquads)
1420+
require.NoError(t, err)
1421+
require.Equal(t, 3, len(fastNQ))
1422+
1423+
// predicate Name should be empty and edge for Discription_v should not be there
1424+
// we do not create edge for "" in float32vector.
1425+
exp := &Experiment{
1426+
t: t,
1427+
nqs: nq,
1428+
schema: `name: string @index(exact) .
1429+
discription_v: float32vector .`,
1430+
query: `{product(func: uid(1)) {
1431+
name
1432+
discription_v
1433+
}}`,
1434+
expected: `{"product":[{
1435+
"name":""}]}`,
1436+
}
1437+
exp.verify()
1438+
1439+
exp.nqs = fastNQ
1440+
exp.verify()
1441+
}
1442+
1443+
func TestNquadsJsonEmptySquareBracketVectorPred(t *testing.T) {
1444+
p := Product{
1445+
Name: "ipad",
1446+
Discription_v: "[]",
1447+
}
1448+
1449+
b, err := json.Marshal(p)
1450+
require.NoError(t, err)
1451+
1452+
nq, err := Parse(b, SetNquads)
1453+
require.NoError(t, err)
1454+
require.Equal(t, 3, len(nq))
1455+
1456+
fastNQ, err := FastParse(b, SetNquads)
1457+
require.NoError(t, err)
1458+
require.Equal(t, 3, len(fastNQ))
1459+
1460+
// predicate Name should have value "ipad" and edge for Discription_v should not be there
1461+
// we do not create edge for [] in float32vector.
1462+
exp := &Experiment{
1463+
t: t,
1464+
nqs: nq,
1465+
schema: `name: string @index(exact) .
1466+
discription_v: float32vector .`,
1467+
query: `{product(func: eq(name, "ipad")) {
1468+
name
1469+
discription_v
1470+
}}`,
1471+
expected: `{"product":[{
1472+
"name":"ipad"}]}`,
1473+
}
1474+
exp.verify()
1475+
1476+
exp.nqs = fastNQ
1477+
exp.verify()
1478+
}
1479+
1480+
func TestNquadsJsonValidVector(t *testing.T) {
1481+
p := Product{
1482+
Name: "ipad",
1483+
Discription_v: "[1.1, 2.2, 3.3]",
1484+
}
1485+
1486+
b, err := json.Marshal(p)
1487+
require.NoError(t, err)
1488+
1489+
nq, err := Parse(b, SetNquads)
1490+
require.NoError(t, err)
1491+
require.Equal(t, 3, len(nq))
1492+
1493+
fastNQ, err := FastParse(b, SetNquads)
1494+
require.NoError(t, err)
1495+
require.Equal(t, 3, len(fastNQ))
1496+
1497+
exp := &Experiment{
1498+
t: t,
1499+
nqs: nq,
1500+
schema: `name: string @index(exact) .
1501+
discription_v: float32vector .`,
1502+
query: `{product(func: eq(name, "ipad")) {
1503+
name
1504+
discription_v
1505+
}}`,
1506+
expected: `{"product":[{
1507+
"name":"ipad",
1508+
"discription_v":[1.1, 2.2, 3.3]}]}`,
1509+
}
1510+
exp.verify()
1511+
1512+
exp.nqs = fastNQ
1513+
exp.verify()
1514+
}

0 commit comments

Comments
 (0)