Skip to content

Coverage not detected when deserializing with custom derserializers (serde-json) #427

@mickem

Description

@mickem

Not really sure if this is an issue with this tool or with serde.

But I have noticed that frequently when I look at my coverage deserializers are not covered.
And at least when I have a custom deserializer this seems reproducible:

The following code seemingly test deserialization but coverage will report it as uncovered:

use serde::{Deserialize, Serialize};

pub mod base64 {
    use base64::{engine::general_purpose::STANDARD, Engine as _};
    use serde::{Deserialize, Serialize};
    use serde::{Deserializer, Serializer};
    pub fn serialize<S: Serializer>(v: &Vec<u8>, s: S) -> Result<S::Ok, S::Error> {
        let base64 = STANDARD.encode(v);
        String::serialize(&base64, s)
    }

    pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<u8>, D::Error> {
        let base64 = String::deserialize(d)?;
        STANDARD
            .decode(base64.as_bytes())
            .map_err(|e| serde::de::Error::custom(format!("Base64 decode error: {}", e)))
    }
}
#[derive(Deserialize, Debug, PartialEq)]
struct TLocJson {
    tag: u8,
    #[serde(with = "base64")]
    data: Vec<u8>,
}

fn main() {
}

#[cfg(test)]
mod tests {
    use crate::TLocJson;

    #[test]
    fn should_deserialize() {
        let json = r#"{"tag":1,"data":"AQID"}"#;
        let tloc: TLocJson = serde_json::from_str(json).unwrap();
        assert_eq!(
            tloc,
            TLocJson {
                tag: 1,
                data: vec![1, 2, 3]
            }
        );
    }

}

Image

If I remove the custom deserializer coverage works as expected.

Image

I don't really understand why it behaves like this and if this is expected behaviour or if this is an issue with serde or serde-json or my serializer please feel free to close this issue.

Cargo file for reference:

[package]
name = "rust-issue"
version = "0.1.0"
edition = "2021"

[dependencies]
serde_json = "1.0.140"
serde = { version = "1.0.219", features = ["derive"] }
base64 = "0.22.1"

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-upstream-bugCategory: This is a bug of compiler or dependencies (the fix may require action in the upstream)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions