Skip to content

fix(dgraph): Fix empty string response for zero datetime #6067

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

Merged
merged 4 commits into from
Jul 30, 2020
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
9 changes: 2 additions & 7 deletions query/outputnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,8 @@ func (enc *encoder) IsEmpty(fj fastJsonNode) bool {
}

var (
boolTrue = []byte("true")
boolFalse = []byte("false")
emptyString = []byte(`""`)
boolTrue = []byte("true")
boolFalse = []byte("false")

// Below variables are used in stringJsonMarshal function.
bufferPool = sync.Pool{
Expand Down Expand Up @@ -465,11 +464,7 @@ func valToBytes(v types.Val) ([]byte, error) {
}
return boolFalse, nil
case types.DateTimeID:
// Return empty string instead of zero-time value string - issue#3166
t := v.Value.(time.Time)
if t.IsZero() {
return emptyString, nil
}
return t.MarshalJSON()
case types.GeoID:
return geojson.Marshal(v.Value.(geom.T))
Expand Down
35 changes: 35 additions & 0 deletions systest/mutations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func TestSystem(t *testing.T) {
t.Run("overwrite uid predicates", wrap(OverwriteUidPredicates))
t.Run("overwrite uid predicates reverse index", wrap(OverwriteUidPredicatesReverse))
t.Run("delete and query same txn", wrap(DeleteAndQuerySameTxn))
t.Run("add and query zero datetime value", wrap(AddAndQueryZeroTimeValue))
}

func FacetJsonInputSupportsAnyOfTerms(t *testing.T, c *dgo.Dgraph) {
Expand Down Expand Up @@ -2395,3 +2396,37 @@ func DeleteAndQuerySameTxn(t *testing.T, c *dgo.Dgraph) {
testutil.CompareJSON(t, `{"me":[{"name":"Alice"}]}`,
string(resp.GetJson()))
}

func AddAndQueryZeroTimeValue(t *testing.T, c *dgo.Dgraph) {
ctx := context.Background()

op := &api.Operation{Schema: `val: datetime .`}
require.NoError(t, c.Alter(ctx, op))

txn := c.NewTxn()
_, err := txn.Mutate(ctx, &api.Mutation{
SetNquads: []byte(`
_:value <val> "0000-01-01T00:00:00Z" .
`),
})
require.NoError(t, err)
require.NoError(t, txn.Commit(ctx))

const datetimeQuery = `
{
q(func: has(val)) {
val
}
}`

txn = c.NewTxn()
resp, err := txn.Query(ctx, datetimeQuery)
require.NoError(t, err)
testutil.CompareJSON(t, `{
"q": [
{
"val": "0000-01-01T00:00:00Z"
}
]
}`, string(resp.Json))
}