google / google/protobuf.dart

Presence checks (hazzers) for `optional bytes` fields don't work

Open
#690 6 comments 0 reactions 0 assignees View on GitHub
bug
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.