dxos / codec-protobuf

GNU Affero General Public License v3.0
2 stars 2 forks source link

decode strips top-level __type_url. #4

Closed telackey closed 4 years ago

telackey commented 4 years ago

It should be possible to "loop" a valid object through the codec, and the output be identical to the input. That is:

before = {
  __type_url: ‘my.Type’,
  payload: { _typeurl: ‘my.OtherType’, ..stuff.. }
}
after = codec.decode(codec.encode(before))
assert(after.equals(before))

But in practice, the top __type_url is stripped and after looks like:

{
  payload: { _typeurl: ‘my.OtherType’, ..stuff.. }
}
tinchoz49 commented 4 years ago

This new codec-protobuf force you to have a root type for the encode/decode and type_url is only used for inner objects of type Any.

tinchoz49 commented 4 years ago

Maybe if I know better what is the context to doing this we can find a workaround.

telackey commented 4 years ago

The first problem is just that it is unexpected, since an object should decode to the same thing it was before encoding.

In practical usage, it can show up a lot of ways, but the simplest is if I want to use a message I received in another message.

message A {
   a = 1;
   ...
}
message B {
   b = 1;
   Any payload = 2;
  ...
}
const a = { __type_url: 'my.A', a: 'a' }
send(codec.encode(a));
const incoming = codec.decode(a);
const b = {
  b: 'b',
  payload: incoming
};

send(codec.encode(b));  //ERROR missing __type_url.

In practice, this is problem for any of the 'Envelope' stuff. If you just want to see it break quickly, go here: https://github.com/wirelineio/incubator/blob/master/packages/credentials/src/party/party.test.js#L104

And add a codecLoop around pseudo.

tinchoz49 commented 4 years ago

I understand. Yes, I think the problem here is related with having a rootTypeUrl is limiting us to use the codec-protobuf as normal encoder/decoder. Encode/decode any type of our schema.

I will prefer to always encode/decode using the __type_url as in your example and also returning it with the result.

And another thing, since we are in JavaScript I recomend to change __type_url to something like __typeurl like in apollo with the __typename

@richburdon