schmittjoh / serializer

Library for (de-)serializing data of any complexity (supports JSON, and XML)
http://jmsyst.com/libs/serializer
MIT License
2.32k stars 588 forks source link

Problem to follow schema: xsd:sequence with maxOccurs="unbounded" #1424

Open Hanmac opened 2 years ago

Hanmac commented 2 years ago
Q A
Bug report? no
Feature request? yes
BC Break report? maybe
RFC? no

Steps required to reproduce the problem

i got this xsd part:

<xsd:element name="EMAILS">
    <xsd:complexType>
        <xsd:sequence maxOccurs="unbounded">
            <xsd:element ref="EMAIL"/>
            <xsd:element ref="PUBLIC_KEY" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

my Problem is now that the sequence has maxOccurs="unbounded", so my first try was to put the sequence into its own class, and try to make it inline

/**
 * @Serializer\XmlRoot("EMAILS")
 */
class Emails
{
    /**
     * @Serializer\Expose
     * @Serializer\XmlList(inline = "true")
     * @Serializer\Inline
     *
     * @var EmailsInner[]
     */
    protected array $inners = [];
}

Expected Result

<?xml version="1.0" encoding="UTF-8"?>
<EMAILS>
  <EMAIL><![CDATA[A]]></EMAIL>
  <EMAIL><![CDATA[B]]></EMAIL>
</EMAILS>

Actual Result

<?xml version="1.0" encoding="UTF-8"?>
<EMAILS>
  <entry>
    <EMAIL><![CDATA[A]]></EMAIL>
  </entry>
  <entry>
    <EMAIL><![CDATA[B]]></EMAIL>
  </entry>
</EMAILS>

i assume it isn't just serializing but de-serializing too which should be affected by this?

and no i can't change the schema.

With PreSerialize

With PreSerialize i could add it to a list attribute to serialize it, but that causes it to be this:

Actual Result

<?xml version="1.0" encoding="UTF-8"?>
<EMAILS>
  <entry><![CDATA[A]]></entry>
  <entry><![CDATA[KeyA]]></entry>
  <entry><![CDATA[B]]></entry>
  <entry><![CDATA[KeyB]]></entry>
</EMAILS>

instead of this:

Expected Result

<?xml version="1.0" encoding="UTF-8"?>
<EMAILS>
  <EMAIL><![CDATA[A]]></EMAIL>
  <PUBLIC_KEY><![CDATA[KeyA]]></PUBLIC_KEY>
  <EMAIL><![CDATA[B]]></EMAIL>
  <PUBLIC_KEY><![CDATA[KeyB]]></PUBLIC_KEY>
</EMAILS>