Skip to content

Commit

Permalink
Merge pull request #308 from ipld/dagcbor-coerce-undef-to-null
Browse files Browse the repository at this point in the history
dagcbor: coerce undef to null.
  • Loading branch information
warpfork committed Dec 7, 2021
2 parents dc30274 + 0683bbe commit 0d1b9a3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion codec/dagcbor/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ func (cfg DecodeOptions) Decode(na datamodel.NodeAssembler, r io.Reader) error {
return na2.DecodeDagCbor(r)
}
// Okay, generic builder path.
return Unmarshal(na, cbor.NewDecoder(cbor.DecodeOptions{}, r), cfg)
return Unmarshal(na, cbor.NewDecoder(cbor.DecodeOptions{
CoerceUndefToNull: true,
}, r), cfg)
}

// Future work: we would like to remove the Unmarshal function,
Expand Down
11 changes: 11 additions & 0 deletions codec/dagcbor/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"strings"
"testing"

"github.com/ipld/go-ipld-prime/datamodel"

qt "github.com/frankban/quicktest"

"github.com/ipld/go-ipld-prime/node/basicnode"
Expand Down Expand Up @@ -38,4 +40,13 @@ func TestFunBlocks(t *testing.T) {
err := Decode(nb, buf)
qt.Assert(t, err, qt.Equals, ErrAllocationBudgetExceeded)
})
t.Run("undef", func(t *testing.T) {
// This fixture tests that we tolerate cbor's "undefined" token (even though it's noncanonical and you shouldn't use it),
// and that it becomes a null in the data model level.
buf := strings.NewReader("\xf7")
nb := basicnode.Prototype.Any.NewBuilder()
err := Decode(nb, buf)
qt.Assert(t, err, qt.IsNil)
qt.Assert(t, nb.Build().Kind(), qt.Equals, datamodel.Kind_Null)
})
}

0 comments on commit 0d1b9a3

Please sign in to comment.