serde-xml-rs icon indicating copy to clipboard operation
serde-xml-rs copied to clipboard

Incorrect serialization of unit enums

Open rikercd opened this issue 5 months ago • 1 comments

The documentation shows the serialization of a unit enum as follows, which I believe is incorrect and inconsistent with typical usage of enumerations in XML: <Document> <message><quit /></message> </Document>

However, enumerations in XSD are typically defined as restrictions on String values, for example:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:simpleType name="Color">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Red"/>
      <xs:enumeration value="Green"/>
      <xs:enumeration value="Blue"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:element name="Paint">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Color" type="Color"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>

From this schema, a compliant XML document would thus be:

<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2020 (http://www.altova.com)-->
<Paint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Test.xsd">
	<Color>Blue</Color>
</Paint>

In my use-case, the Color type gets generated in my Rust bindings as an enum with unit enumerations, e.g.

pub enum Color {
    Red,
    Green,
    Blue
}

These are serialized appropriately in JSON and YAML as string values using serde similarly to the XML above , e.g.

{
    "Color" : "Red"
}

rikercd avatar Jun 20 '25 18:06 rikercd