Closed skysbird closed 1 year ago
required Type type = 1;
required
has been removed from proto3.
To simulate it on the wire, mark the field optional
, then a value will always be written into the protobuf, even if it's the default value, and proto2 parsers will be able to decode the message.
required Type type = 1;
required
has been removed from proto3.To simulate it on the wire, mark the field
optional
, then a value will always be written into the protobuf, even if it's the default value, and proto2 parsers will be able to decode the message.
Wouldn't this mean the value would not be written if not explicitly set? This would cause a well formatted proto3 message to fail when being decoded in proto2. Consider:
syntax = "proto3"
message Hop {
enum Type {
RESERVE = 0;
...
}
optional Type type;
}
As far as typechecks are considered HopMessage {}
is a valid message and would be serialised as an empty slice, but this would fail when being parsed by proto2 since required Type type
is not set.
Agreed, but there's no other way to ensure the value is written onto the wire in proto3.
Consider this:
syntax = "proto3"
message Hop {
enum Type {
RESERVE = 0;
...
}
Type type;
}
With this definition { type: 0 }
would also be serialized to an empty slice and a proto2 parser would fail to read it.
It's better to upgrade everyone to proto3 and not use required
- even Google express some pretty strong caveats over using that keyword.
Where that isn't possible the only thing you can do is mark it optional
and enforce the field presence at the application level.
related with these pr: https://github.com/libp2p/specs/pull/506 https://github.com/ipfs/protons/pull/80
https://github.com/ipfs/protons/blob/51746ec22fcc4337e3975a2a5eff871b336ab5e9/packages/protons/src/index.ts#L405
When I protons this proto, it will gnenerate the code like this:
This will cause the wrong binary data(MALFORM) when I encode the HopMessage with type value is RESERVE when the relay node is kubo