Skip to content

Roundtrip issues involving transparent on tuple structs #113

Description

@valkum

Ran into some issues involving transparent. I assume this should work based on the docs.
It might be that transparent is not supposed to work with tuple structs (newtypes or unnamed fields); in that case, instant-xml should get some docs about this. Please don't hesitate to close this issue if this is the case. I will try to find time to add some docs afterward.

Issue: The derive macro for FromXml does not sequentially deserialize fields for unnamed fields that have a type that is transparent.
Current tests only cover transparent using named struct fields.
You also cannot serialize a transparent type directly, which I think is correct, because XML requires a single doc root, which is not there if you inline the struct basically.

Docs:

//! - **`transparent`** *(structs only)* - inlines fields without wrapper element

Here is a reproducer:

use instant_xml::{FromXml, ToXml, to_string};

fn main() {
    let reference = Wrapper(SomeXMLSchemaType {
        value: Some("example".to_string()),
        thing: SomeThing {
            field: "data".to_string(),
        },
    });

    let out = to_string(&reference).unwrap();
    println!("{}", out);
    assert_eq!(reference, instant_xml::from_str::<Wrapper>(&out).unwrap());
}

/// <Wrapper> element
/// ```
/// <element name="wrapper" type="SomeXMLSchemaType" />
/// ```
#[derive(FromXml, ToXml, PartialEq, Debug)]
#[xml(rename = "wrapper")]
struct Wrapper(SomeXMLSchemaType);

/// A type following XML Schema conventions for Wrapper
/// might be used in multiple elements.
/// Newtype used to reflect correct XML element.
///
/// E.g.
/// ```
/// <complexType name="SomeXMLSchemaType">
///    <sequence>
///      <element name="value" type="string" minOccurs="0" />
///      <element name="thing" type="SomeThing" />
///    </sequence>
///  </complexType>
/// ```
#[derive(FromXml, ToXml, PartialEq, Debug)]
#[xml(transparent)]
struct SomeXMLSchemaType {
    value: Option<String>,
    thing: SomeThing,
}

/// Inner type following XML Schema conventions
/// Used only in a single place, so no newtype and rename instead.
/// E.g.
/// ```
/// <complexType name="SomeThing">
///    <sequence>
///      <element name="field" type="text"/>
///    </sequence>
///  </complexType>
/// ```
#[derive(FromXml, ToXml, PartialEq, Debug)]
#[xml(rename = "thing")]
struct SomeThing {
    field: String,
}

This panics with:

thread 'main' (92315896) panicked at src/main.rs:17:66:
called `Result::unwrap()` on an `Err` value: MissingValue("SomeXMLSchemaType::thing")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions