stepancheg/rust-protobuf

`.merge_from()` does not correctly merge `MessageField`s

mkghaas opened this issue · 0 comments

When merging messages containing fields of type MessageField, these fields are not merged as expected. Instead, the message field is simply overwritten.

In my own fork I fixed this by replacing the relevant code in protobuf/src/rt/message.rs:

/// Read singular `message` field.
pub fn read_singular_message_into_field<M>(
    is: &mut CodedInputStream,
    target: &mut MessageField<M>,
) -> crate::Result<()>
where
    M: Message,
{
    let mut m = M::new();
    is.merge_message(&mut m)?;
    *target = MessageField::some(m);
    Ok(())
}

with

/// Read singular `message` field.
pub fn read_singular_message_into_field<M>(
    is: &mut CodedInputStream,
    target: &mut MessageField<M>,
) -> crate::Result<()>
where
    M: Message,
{
    if let Some(target) = target.as_mut() {
        is.merge_message(target)?;
    } else {
        let mut m = M::new();
        is.merge_message(&mut m)?;
        *target = MessageField::some(m);
    }
    Ok(())
}

Before opening a PR I wanted to ask if the current behavior is by design, or if this is indeed a deviation from the protobuf specification.