question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proto2 `required` fields that contain default values are not serialized correctly

See original GitHub issue

My proto is as follows

message Update {
    repeated Prop prop = 1;
    message Prop {
        oneof prop_update_type {
            StandardProp standard_prop = 2;
            RegularProp regular_prop = 5;
        }
    message StandardProp {
        optional string id = 1;
        optional string title = 2;
    }

    message RegularProp {
        required string name = 1;
        required bool is_array = 2;
        optional Value value = 3;
    }
}
message Value {
    oneof value {
        string string_value = 1;
        bool boolean_value = 2;
        int32 integer_value = 3;
        int64 long_value = 4;
        float float_value = 5;
        double double_value = 6;
    }
}

The Update object protoMarshal() is sent across Kafka and on the consumer side, I am getting exceptions which are on java platform. The exceptions are different in different projects.

Note: I am getting exception only when Prop contains RegularProp.

Below listed bullet points are the exception messages I am getting in java projects which deserialize using Update.parseFrom(data)

Project - 1 Value cannot cast to Update Project - 2 prop[0].regular_prop.is_array not present

Is this due to the presence of Value in RegularProp?

The same proto is used by other Kafka producers in different projects (Javascript, java, etc.) and working fine. So to repeat the exception is only for Kafka message sent from Kotlin to other platform and only when using RegularProp

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
garypcommented, Jul 15, 2020

@sujithkris This seems like a bug in pbandk. That’s why it works in Java 🙂 pbandk isn’t doing the correct thing for proto2 required fields that contain default values.

At least that’s my hypothesis. If you could share the binary data, that’d help me confirm the hypothesis. But I should be able to also write a quick unit test to test this scenario.

0reactions
garypcommented, Sep 15, 2020

@sujithkris I was able to reproduce the bug with a unit test. Unfortunately it doesn’t look like pbandk ever properly supported required proto2 fields (despite the documentation), so this is going to be a bit more involved to fix than I expected. It won’t make the 0.9.0 release (which should be this or next week), but we’ll try to get it in for the 0.10.0 release.

Also, to clarify, the bug only impacts proto2 required fields that contain default values (as you have in your example up above). proto2 optional fields with default values and proto3 fields with default values should be working correctly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why doesn't protobuf-net for proto2 .protos generate ...
Default values are entirely local. They are never sent over the wire. This means that setting a default value for a required field...
Read more >
Language Guide | Protocol Buffers - Google Developers
When a message is parsed, if it does not contain an optional element, accessing the corresponding field in the parsed object returns the...
Read more >
Protobuf Buffers v3.0.0-alpha-1
I have a question regarding the decision to drop support for default values. Fields which are set to their default values are not...
Read more >
Why Protobuf 3 made all fields on the messages optional?
only the implicit default is allowed; no other default values are permitted; if a field has that default value, it is not serialized;...
Read more >
Protocol Buffers - Google's data interchange format
For proto2, if not opted in, has state (field presence) is not available. Serialization outputs all fields not equal to their defaults (see...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found