Skip to content

protobufjs6, oneof and default values #542

@jonathon-love

Description

@jonathon-love

hi,

with the following .proto file:

message Message {
    oneof c {
        int32 a = 1;
        int32 b = 2;
    }
}

compiled into a static-module (that's just what i'm using, but i expect other code paths will have the same issue), and invoked thus:

const Root = require('./awesome_pb.js');

let message = new Root.Message();

message.a = 0;
let bytes = Root.Message.encode(message).finish();
console.log(bytes)

bytes ends up an empty buffer.

this is understandable, because 0 is the default value for a, but it also means that whenever a oneof is set to it's default, it is no longer possible to determine which value was set.

i don't think the google protobuf documentation is clear on what the correct behaviour here is (but i haven't had a very good look), but the behaviour of the google protobuf is to encode the zero value (which i think makes sense).

what do you think? i'm happy to create a PR for it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions