Presence checks (hazzers) for `optional bytes` fields don't work
- Dominant language
- Dart
- Stars
- 572
- Forks
- 196
- Avg merge
- 1h 59m
- Merged PRs (30d)
- 2
Description
With `optional bytes optionalByts` and `optional string optionalStr` fields, if I run this:
```dart
{
Msg1 m1 = Msg1()..mergeFromProto3Json({'optionalByts': ''});
Msg1 m2 = Msg1()..mergeFromProto3Json({});
print('hash codes equal = ${m1.hashCode == m2.hashCode}');
print('values equal = ${m1 == m2}');
print('m1.optionalByts = ${m1.optionalByts}, m2.optionalByts = ${m2.optionalByts}, m1.hasOptionalByts = ${m1.hasOptionalByts()}, m2.hasOptionalByts = ${m2.hasOptionalByts()}');
}
print('---');
{
Msg1 m1 = Msg1()..mergeFromProto3Json({'optionalStr': ''});
Msg1 m2 = Msg1()..mergeFromProto3Json({});
print('hash codes equal = ${m1.hashCode == m2.hashCode}');
print('values equal = ${m1 == m2}');
print('m1.optionalStr = ${m1.optionalStr}, m2.optionalStr = ${m2.optionalStr}, m1.hasOptionalStr = ${m1.hasOptionalStr()}, m2.hasOptionalStr = ${m2.hasOptionalStr()}');
}
```
This is the output:
```
hash codes equal = true
values equal = true
m1.optionalByts = [], m2.optionalByts = [], m1.hasOptionalByts = false, m2.hasOptionalByts = false
---
hash codes equal = false
values equal = false
m1.optionalStr = , m2.optionalStr = , m1.hasOptionalStr = true, m2.hasOptionalStr = false
```
So for an `optional string` field, if I see a message with the default value, that's different than if I don't see the field at all. This is working as expected.
But for `optional bytes`, seeing the default value in the serialized message is the same as not seeing it.
There are probably more bugs in proto3 optional implementation. I will read the specs ([1], [2]) carefully and implement some tests to see.
[1]: https://github.com/protocolbuffers/protobuf/blob/main/docs/field_presence.md
[2]: https://github.com/protocolbuffers/protobuf/blob/main/docs/implementing_proto3_presence.md
Contributor guide
Assessment
This issue has not been assessed yet.