Skip to content
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

dagcbor: coerce undef to null. #308

Merged
merged 2 commits into from
Dec 7, 2021
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
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)
})
}