-
Notifications
You must be signed in to change notification settings - Fork 50
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
fix(codecs): coerce cid.Undef to null in dag{json,cbor} output #433
Conversation
I'm not sure I've grasp the full impact of this. But serializing a |
I'm thinking through what I might do in JS if we made it OK to have these zero-CIDs in the codecs, and I don't think I have the ability to round-trip a null CID, we don't have (and haven't needed) an equivalent of @vmx |
Here's a case for just erroring—the old go-ipld-cbor will if it gets an |
@rvagg I'm not sure I'm missing something here. I don't understand the relation to codecs. It seems to be a Go specific issue. I wouldn't expect codecs to support/round-trip invalid CIDs and I also think we should try as hard as possible to not leak language specific things into codecs. |
@rvagg what's the motivation to not error? IIUC the older libraries error here and it's not clear why anyone would intentionally want to encode a null CID rather than that being an application bug. Is there a bunch of this malformed data running around in the wild that we'd want to decode without erroring? |
I also have mixed feelings on this one. Having errors when nil/undef cids is a good indication that you should be checking and converting that case into the null case of a nullable field. That said, it is boilerplate that gets added in at all those places and does seem right that we can just automatically interpret that to the null case when possible. |
OK, no appetite for changing codec specs to allow some form of zero-CID, makes this a bit easier. Error vs coerce: I coming down on the side of erroring - it may lead to boilerplate, but if you end up with an undefined CID maybe it should have been a pointer and therefore be Erroring does give us some good symmetry as well, there's no magic or surprises and ensures round-trip stability. Make your input the same as what you should get on the output, and we're not making undefined CIDs anywhere on the way out, it'll just be a null Node. So latest commit makes it an error; reviews or further discussion? |
Otherwise we end up with invalid output. dag-cbor: 0xd82a4100 d8 2a # tag(42) 41 # bytes(1) 00 # "\x00" dag-json: {"/":"b"} Fixes: #246
99c91d9
to
eb767ed
Compare
Otherwise we end up with invalid output like this:
dag-cbor:
0xd82a4100
dag-json:
{"/":"b"}
Fixes: #246
@aschmahmann @willscott I'd appreciate your 👍 on this as it's a coercion rather than an error or other funky behaviour. But this probably isn't that simple:
null
. The alternative may be to define a null CID for both of these codecs - we could just use the values above and make it work across our current codec implementations (currently in JS, both of these result in errors because it can't decode a varint where it expects, IIRC round-tripping in Go does essentially the same thing).nullable
oroptional
, then a round-trip with acid.Undef
is going to result in anull
in that position which might be a problem. But we could push that problem up the stack and make it a concern for bindnode and friends to figure out. They could even usecid.Undef
for non-nullable non-optional fields that come out asnull
.cid.Undef
, it's not allowed.(Related discussion in #191 and the currently rejected PR that would error on
Absent
fields: #196).@Stebalien @vmx you might want to weigh in too because we're talking possible codec behaviour changes.