xsdata icon indicating copy to clipboard operation
xsdata copied to clipboard

Issue with sequence with choice

Open Risto97 opened this issue 8 months ago • 3 comments

Hello,

I noticed an issue with the generated classes. From this sequence

<xs:sequence>
	<xs:group ref="ipxact:nameGroup"/>
    <xs:element ref="ipxact:activeInterface"/>
	<xs:choice>
		<xs:sequence>
			<xs:element ref="ipxact:activeInterface" minOccurs="0" maxOccurs="unbounded"/>
			<xs:element name="hierInterface" type="ipxact:hierInterfaceType" minOccurs="0" maxOccurs="unbounded"/>
		</xs:sequence>
		<xs:element name="hierInterface" type="ipxact:hierInterfaceType" maxOccurs="unbounded"/>
	</xs:choice>
	<xs:element ref="ipxact:vendorExtensions" minOccurs="0"/>
</xs:sequence>

It generates this:

    active_interface: list[ActiveInterface] = field(
        default_factory=list,
        metadata={
            "name": "activeInterface",
            "type": "Element",
            "min_occurs": 2,
            "sequence": 1,
        },
    )
    hier_interface: list[HierInterfaceType] = field(
        default_factory=list,
        metadata={
            "name": "hierInterface",
            "type": "Element",
            "min_occurs": 1,
            "sequence": 1,
        },
    )

Problem is the sequence value is 1 for both ActiveInterface and HierInterfaceType. This makes it so that serialized data becomes interleaved if the active_interface is a list of multiple elements. Basically I get this:

        <ipxact:interconnection>
            <ipxact:name>intc_3805385578664854469</ipxact:name>
            <ipxact:activeInterface componentInstanceRef="i_gpio" busRef="IPReset"/>
            <ipxact:hierInterface busRef="rst"/>
            <ipxact:activeInterface componentInstanceRef="i_timer" busRef="APBReset"/>
        </ipxact:interconnection>

And this does not validate against the schema as it is expected to be like this:

        <ipxact:interconnection>
            <ipxact:name>intc_3805385578664854469</ipxact:name>
            <ipxact:activeInterface componentInstanceRef="i_gpio" busRef="IPReset"/>
            <ipxact:activeInterface componentInstanceRef="i_timer" busRef="APBReset"/>
            <ipxact:hierInterface busRef="rst"/>
        </ipxact:interconnection>

Risto97 avatar Apr 25 '25 14:04 Risto97

For repeating choices or complex sequence elements give the compound fields a try.

tefra avatar May 02 '25 12:05 tefra

It seems to me with --compound-fields or --no-compound-fields the generation of this class remain the same.

Thomasb81 avatar May 03 '25 09:05 Thomasb81

Yes, I confirm that this issue does not get fixed by using --compound-fields. Currently I manually fix the issue as luckily this construct does not appear often. Could the tool at least issue a warning in such cases, or is it difficult to even detect this?

Risto97 avatar May 06 '25 07:05 Risto97