media-io / yaserde

Yet Another Serializer/Deserializer
MIT License
175 stars 57 forks source link

Naming collisions with std types #127

Open marcelbuesing opened 2 years ago

marcelbuesing commented 2 years ago

Having elements in an XML where the naming collides with std type names lead to errors. In the example below I have a custom String type. This seems like a contrived example and I would actually just go with just renaming the type, but I ran into this issue with an existing XSD and xml-schema and then noticed the issue is actually one layer deeper.

I think the actual issue is in Field.rs the following line

"String" => Some(Field::FieldString),

Problem being that basically it's not checked if this is actually a std::string::String or from a different module.

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

  mod non_std {
    #[derive(Default, PartialEq, Debug, YaDeserialize, YaSerialize)]
    #[yaserde(rename = "STRING")]
    pub struct String {
      pub content: std::string::String,
    }

    #[derive(Default, PartialEq, Debug, YaDeserialize, YaSerialize)]
    #[yaserde(rename = "foo")]
    pub struct FooOuter {
      #[yaserde(rename = "STRING")]
      pub non_std_string: self::String,
    }
  }
}

deseriaizer.rs

error[E0308]: mismatched types
    --> yaserde/tests/deserializer.rs:1093:11
     |
1093 |       pub non_std_string: self::String,
     |           ^^^^^^^^^^^^^^ expected struct `non_std::String`, found struct `std::string::String`

error[E0277]: `non_std::String` doesn't implement `std::fmt::Display`
    --> yaserde/tests/deserializer.rs:1089:56
     |
1089 |     #[derive(Default, PartialEq, Debug, YaDeserialize, YaSerialize)]
     |                                                        ^^^^^^^^^^^ `non_std::String` cannot be formatted with the default formatter
     |
     = help: the trait `std::fmt::Display` is not implemented for `non_std::String`
     = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
     = note: this error originates in the macro `$crate::__export::format_args` (in Nightly builds, run with -Z macro-backtrace for more info)
mlevkov commented 2 years ago

@marcelbuesing You can probably follow my example here -> https://github.com/media-io/yaserde/issues/87 to create your own serializer/deserializer that implements your type.

MarcAntoine-Arnaud commented 7 months ago

@marcelbuesing Can you check with newest version of YaSerDe ? If I am right, it's addressed.