diff --git a/soft_delete.go b/soft_delete.go index b3280ff7..f3272246 100644 --- a/soft_delete.go +++ b/soft_delete.go @@ -31,7 +31,7 @@ func (n DeletedAt) MarshalJSON() ([]byte, error) { func (n *DeletedAt) UnmarshalJSON(b []byte) error { err := json.Unmarshal(b, &n.Time) - if err == nil { + if err == nil && !n.Time.IsZero() { n.Valid = true } return err diff --git a/tests/soft_delete_test.go b/tests/soft_delete_test.go index 40d46fd8..c77675f7 100644 --- a/tests/soft_delete_test.go +++ b/tests/soft_delete_test.go @@ -1,6 +1,7 @@ package tests_test import ( + "encoding/json" "errors" "testing" @@ -42,3 +43,14 @@ func TestSoftDelete(t *testing.T) { t.Errorf("Can't find permanently deleted record") } } + +func TestDeletedAtUnMarshal(t *testing.T) { + expected := &gorm.Model{} + b, _ := json.Marshal(expected) + + result := &gorm.Model{} + _ = json.Unmarshal(b, result) + if result.DeletedAt != expected.DeletedAt { + t.Errorf("Failed, result.DeletedAt: %v is not same as expected.DeletedAt: %v", result.DeletedAt, expected.DeletedAt) + } +}