serde-rs / serde

Serialization framework for Rust
https://serde.rs/
Apache License 2.0
8.81k stars 747 forks source link

Skip serializing generic still requires the bound #2727

Open irevoire opened 2 months ago

irevoire commented 2 months ago

Hello,

I noticed that the serde derive macro was incorrectly adding impl Serialize bounds to the structures that derive Serialize:

use serde::Serialize;

#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct First<Http> {
    second: Second<Http>,
}

#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Second<Http> {
    nb: usize,
    #[serde(skip_serializing)]
    http_client: Http,
}

pub struct Truc;

fn main() {
    // This works, serde understands that `Http` doesn't need to implement `Serialize`
    let a = serde_json::to_string_pretty(&Second {
        nb: 1,
        http_client: Truc,
    })
    .unwrap();

    // This doesn't; serde derive added a bound requiring that `Http` implements `Serialize`
    let a = serde_json::to_string_pretty(&First {
        second: Second {
            nb: 1,
            http_client: Truc,
        },
    })
    .unwrap();
}