Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.merge_from() does not correctly merge MessageFields #674

Open
mkghaas opened this issue Jun 1, 2023 · 0 comments
Open

.merge_from() does not correctly merge MessageFields #674

mkghaas opened this issue Jun 1, 2023 · 0 comments

Comments

@mkghaas
Copy link

mkghaas commented Jun 1, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant