Skip to content

Commit

Permalink
Fix messages with optionals and oneofs (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed May 4, 2021
1 parent 32ad5a2 commit 28e4522
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
12 changes: 6 additions & 6 deletions prost-build/src/code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,12 @@ impl<'a> CodeGenerator<'a> {

for (idx, oneof) in message.oneof_decl.into_iter().enumerate() {
let idx = idx as i32;
self.append_oneof(
&fq_message_name,
oneof,
idx,
oneof_fields.remove(&idx).unwrap(),
);
// optional fields create a synthetic oneof that we want to skip
let fields = match oneof_fields.remove(&idx) {
Some(fields) => fields,
None => continue,
};
self.append_oneof(&fq_message_name, oneof, idx, fields);
}

self.pop_mod();
Expand Down
5 changes: 4 additions & 1 deletion tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,10 @@ mod tests {

#[test]
fn test_proto3_presence() {
let msg = proto3::presence::A { b: Some(42) };
let msg = proto3::presence::A {
b: Some(42),
foo: Some(proto3::presence::a::Foo::C(13)),
};

check_message(&msg);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/src/proto3_presence.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ package proto3.presence;

message A {
optional int32 b = 1;

oneof foo {
int32 c = 2;
}
}

0 comments on commit 28e4522

Please sign in to comment.