goetas-webservices / xsd2php

Convert XSD into PHP classes and JMS serializer definitions
MIT License
238 stars 91 forks source link

xs:Choice of xs:Sequence not supported? #168

Open rtek opened 7 months ago

rtek commented 7 months ago

Attempting to parse this xsd results in a fatal. Is this an unsupported situation or a bug? Thoughts on a workaround?

https://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd

<element name="PGPData" type="ds:PGPDataType"/> 
<complexType name="PGPDataType"> 
  <choice>
    <sequence>
      <element name="PGPKeyID" type="base64Binary"/> 
      <element name="PGPKeyPacket" type="base64Binary" minOccurs="0"/> 
      <any namespace="##other" processContents="lax" minOccurs="0"
       maxOccurs="unbounded"/>
    </sequence>
    <sequence>
      <element name="PGPKeyPacket" type="base64Binary"/> 
      <any namespace="##other" processContents="lax" minOccurs="0"
       maxOccurs="unbounded"/>
    </sequence>
  </choice>
</complexType>
PHP Fatal error:  Uncaught TypeError: GoetasWebservices\Xsd\XsdToPhp\Php\PhpConverter::visitElement(): Argument #3 ($element) must be of type GoetasWebservices\XML\XSDReader\Schema\Element\ElementSingle, GoetasWebservices\XML\XSDReader\Schema\Element\Sequence given, called in ...\vendor\goetas-webservices\xsd2php\src\Php\PhpConverter.php on line 133 and defined in ...\vendor\goetas-webservices\xsd2php\src\Php\PhpConverter.php:429
nikbobbie commented 6 months ago

I ran into the same issue with the that file after upgrading my php runtime from 7.4 to 8.2. Before php8 it was running flawless for over a year.

I spend some time debugging and made it work by patching some functions in Php/PhpConverter. Looks like the implementation of xs:sequence is just incomplete. Someone already created a pull request with a proper implementation which you could use a base for a patch until this fix is included in a release. https://github.com/goetas-webservices/xsd2php/pull/171/commits/bba3d457a584210321c45e33d5429b8a39cc6827

rtek commented 6 months ago

As a workaround i just altered the XSD to drop the second sequence and remove the choice, since i didnt need it anyways - e.g.

<element name="PGPData" type="ds:PGPDataType"/> 
<complexType name="PGPDataType"> 
    <sequence>
      <element name="PGPKeyID" type="base64Binary"/> 
      <element name="PGPKeyPacket" type="base64Binary" minOccurs="0"/> 
      <any namespace="##other" processContents="lax" minOccurs="0"
       maxOccurs="unbounded"/>
    </sequence>
</complexType>

related https://github.com/goetas-webservices/xsd2php/issues/170