quick-protobuf icon indicating copy to clipboard operation
quick-protobuf copied to clipboard

Default values not respected

Open stjepangolemac opened this issue 2 years ago • 4 comments

For example:

syntax = "proto2";

...

required bool foo = 1 [default = true];

Generated rust code still defaults to false for field foo.

stjepangolemac avatar Apr 05 '22 14:04 stjepangolemac

I assume you mean when doing MyMessage::default(). It looks like default values should be respected when deserializing a proto.

nerdrew avatar Apr 09 '22 04:04 nerdrew

I don't know much about deserialization in this case but if the Default implementation doesn't respect the default value in the proto definition then the boolean will end up being false, hence it will be deserialized as false?

I guess what I'm asking is should the Default implementation follow the field definition in the proto file?

stjepangolemac avatar Apr 11 '22 07:04 stjepangolemac

Minimal Reproducible Example

schema.proto:

syntax = "proto2";

message Foo {
    required uint64 bar = 1;
    optional bool baz = 2 [default = true];
}

main.rs:

pub mod schema;

fn main() {
    let foo = schema::Foo{bar: 420, ..Default::default()};
    print!("foo.bar: {} foo.baz: {}\n", foo.bar, foo.baz);
}

expected result:

$ cargo run
foo.bar: 420 foo.baz: true

observed result:

$ cargo run
foo.bar: 420 foo.baz: false

viveshok avatar May 23 '22 21:05 viveshok

Oh, by the way I think #247 should solve this (as part of our implementation of field presence, we had to fix the impl Default system).

snproj avatar Mar 06 '23 08:03 snproj