media-io / yaserde

Yet Another Serializer/Deserializer
MIT License
174 stars 58 forks source link

skip/skip_if are not working for Enum, Struct and Vec #152

Closed lbenini closed 1 year ago

lbenini commented 1 year ago

Description

skip_serializing and skip_serializing_if are not working for Enum, Struct (children of the root) and Vec.

How to procedure

The following snippets of code can be inserted in the skip.rs test suite to reproduce the bug for skip_serializing

#[test]
fn skip_serializing_for_nested_struct() {
  init();

  #[derive(YaSerialize, PartialEq, Debug)]
  #[yaserde(rename = "base")]
  pub struct XmlStruct {
    #[yaserde(skip_serializing)]
    skipped_serializing: XmlStructChild,
  }

  #[derive(YaSerialize, PartialEq, Debug)]
  #[yaserde(rename = "child")]
  pub struct XmlStructChild {
  }

  let model = XmlStruct {
    skipped_serializing: XmlStructChild{},
  };

  let content = "<base />";
  serialize_and_validate!(model, content);
}

#[test]
fn skip_serializing_for_enum() {
  init();

  #[derive(YaSerialize, PartialEq, Debug)]
  #[yaserde(rename = "base")]
  pub struct XmlStruct {
    #[yaserde(skip_serializing)]
    skipped_serializing: XmlEnum,
  }

  #[derive(YaSerialize, PartialEq, Debug)]
  #[yaserde(rename = "child")]
  pub enum XmlEnum {
    Ok
  }

  let model = XmlStruct {
    skipped_serializing: XmlEnum::Ok,
  };

  let content = "<base />";
  serialize_and_validate!(model, content);
}

#[test]
fn skip_serializing_for_vec() {
  init();

  #[derive(YaSerialize, PartialEq, Debug)]
  #[yaserde(rename = "base")]
  pub struct XmlStruct {
    #[yaserde(skip_serializing)]
    skipped_serializing: Vec<i8>,
  }

  let model = XmlStruct {
    skipped_serializing: vec![1,2,3],
  };

  let content = "<base />";
  serialize_and_validate!(model, content);
}

the following snippets can be used in serializing_if.rs file to reproduce the conditional serialization issue

#[test]
fn skip_serializing_if_for_nested_struct() {
  init();

  #[derive(YaSerialize, PartialEq, Debug)]
  #[yaserde(rename = "base")]
  pub struct XmlStruct {
    #[yaserde(skip_serializing_if="check_child")]
    skipped_serializing: XmlStructChild,
  }
  impl XmlStruct
  {
    fn check_child(&self, _child:&XmlStructChild)->bool
    {
      true
    }
  }
  #[derive(YaSerialize, PartialEq, Debug)]
  #[yaserde(rename = "child")]
  pub struct XmlStructChild {
  }

  let model = XmlStruct {
    skipped_serializing: XmlStructChild{},
  };

  let content = "<base />";
  serialize_and_validate!(model, content);
}

#[test]
fn skip_serializing_if_for_enum() {
  init();

  #[derive(YaSerialize, PartialEq, Debug)]
  #[yaserde(rename = "base")]
  pub struct XmlStruct {
    #[yaserde(skip_serializing_if="check_enum")]
    skipped_serializing: XmlEnum,
  }
  impl XmlStruct
  {
    fn check_enum(&self, _child:&XmlEnum)->bool
    {
      true
    }
  }
  #[derive(YaSerialize, PartialEq, Debug)]
  #[yaserde(rename = "child")]
  pub enum XmlEnum {
    Ok
  }

  let model = XmlStruct {
    skipped_serializing: XmlEnum::Ok,
  };

  let content = "<base />";
  serialize_and_validate!(model, content);
}

#[test]
fn skip_serializing_if_for_vec() {
  init();

  #[derive(YaSerialize, PartialEq, Debug)]
  #[yaserde(rename = "base")]
  pub struct XmlStruct {
    #[yaserde(skip_serializing_if="check_vec")]
    skipped_serializing: Vec<i8>,
  }
  impl XmlStruct
  {
    fn check_vec(&self, _child:&Vec<i8>)->bool
    {
      true
    }
  }
  let model = XmlStruct {
    skipped_serializing: vec![1,2,3],
  };

  let content = "<base />";
  serialize_and_validate!(model, content);
}

Analisys

The issue is located expand_struct.rs where the conditions are generated, but never kept in consideration for Enum, Struct and Vec

Solutions

I'm working on pull request that fix the issue

Notes

Related, but different or partial issues are: https://github.com/media-io/yaserde/issues/131 https://github.com/media-io/yaserde/issues/139

lbenini commented 1 year ago

With the merge of #153 this is now solved