Skip to content

Commit

Permalink
[FAB-9816] JSON Unmarshal special case
Browse files Browse the repository at this point in the history
Unmarshaling a the string "null" results in a nil which is
unexpected by statecouchdb.  This causes a downstream failure.

This change adds handling for the special case of string "null".

Change-Id: I37cd5c07a6da42a37b441225e809de8c9f0c1d93
Signed-off-by: Chris Elder <[email protected]>
  • Loading branch information
Chris Elder committed Jun 12, 2018
1 parent ff47486 commit b73d94c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ func TestBasicRW(t *testing.T, dbProvider statedb.VersionedDBProvider) {
vv2 := statedb.VersionedValue{Value: []byte("value2"), Version: version.NewHeight(1, 2)}
vv3 := statedb.VersionedValue{Value: []byte("value3"), Version: version.NewHeight(1, 3)}
vv4 := statedb.VersionedValue{Value: []byte{}, Version: version.NewHeight(1, 4)}
vv5 := statedb.VersionedValue{Value: []byte("null"), Version: version.NewHeight(1, 5)}
batch.Put("ns1", "key1", vv1.Value, vv1.Version)
batch.Put("ns1", "key2", vv2.Value, vv2.Version)
batch.Put("ns2", "key3", vv3.Value, vv3.Version)
batch.Put("ns2", "key4", vv4.Value, vv4.Version)
batch.Put("ns2", "key5", vv5.Value, vv5.Version)
savePoint := version.NewHeight(2, 5)
db.ApplyUpdates(batch, savePoint)

Expand All @@ -88,6 +90,9 @@ func TestBasicRW(t *testing.T, dbProvider statedb.VersionedDBProvider) {
vv, _ = db.GetState("ns2", "key4")
testutil.AssertEquals(t, vv, &vv4)

vv, _ = db.GetState("ns2", "key5")
testutil.AssertEquals(t, vv, &vv5)

sp, err = db.GetLatestSavePoint()
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, sp, savePoint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,18 @@ func keyValToCouchDoc(kv *keyValue, revision string) (*couchdb.CouchDoc, error)
switch {
case value == nil:
kvtype = kvTypeDelete
case json.Unmarshal(value, &jsonMap) == nil:
// check for the case where the jsonMap is nil, this will indicate
// a special case for the Unmarshal that results in a valid JSON returning nil
case json.Unmarshal(value, &jsonMap) == nil && jsonMap != nil:
kvtype = kvTypeJSON
if err := jsonMap.checkReservedFieldsNotPresent(); err != nil {
return nil, err
}
default:
// create an empty map, if the map is nil
if jsonMap == nil {
jsonMap = make(jsonValue)
}
kvtype = kvTypeAttachment
}

Expand Down

0 comments on commit b73d94c

Please sign in to comment.