Skip to content

use serde_with::apply within macro #538

@ibotty

Description

@ibotty

serde_with::apply fails to apply attributes to data attributes added in macros. See below error.

use serde_with;

macro_rules! t {
($($param:ident : $ty:ty),*) => {
#[derive(Default)]
#[serde_with::apply(
    Option => #[serde(skip_serializing_if = "Option::is_none")],
)]
#[derive(serde::Serialize)]
struct Data {
    a: Option<String>,
    b: Option<u64>,
    c: Option<String>,
    $(
        pub $param: $ty),*
}
}}

t!(d: Option<bool>);

#[test]
fn t() {
    let golden = r#"{}"#;
    let data: Data = Default::default();
    assert_eq!(serde_json::to_string(&data).unwrap(), golden);
}

This fails with

---- t stdout ----
thread 't' panicked at 'assertion failed: `(left == right)`
  left: `"{\"d\":null}"`,
 right: `"{}"`', doveadm_types/src/lib.rs:34:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions