xsdata icon indicating copy to clipboard operation
xsdata copied to clipboard

`CompoundFields` doesn't work well when a `<xs:sequence>` is nested in a `<xs:choice>`

Open sashkent3 opened this issue 10 months ago • 1 comments

Consider the following schema
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Root">
    <xs:complexType>
      <xs:choice>
        <xs:sequence>
          <xs:element name="TheFirstInnerChoice" type="xs:string"></xs:element>
          <xs:element name="TheSecondInnerChoice" type="xs:string"></xs:element>
        </xs:sequence>
        <xs:element name="TheFirstChoice" type="xs:string"></xs:element>
        <xs:element name="TheSecondChoice" type="xs:string"></xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

When the CompoundFields option is enabled, the type of the choice field is:

tuple[
  Union[
    TheFirstInnerChoice,
    TheSecondInnerChoice,
    TheFirstChoice,
    TheSecondChoice,
  ],
  ...,
]

The above is rather incorrect because the strict Python equivalent of the schema would be:

Union[
  tuple[
    TheFirstInnerChoice,
    TheSecondInnerChoice
  ],
  TheFirstChoice,
  TheSecondChoice,
]

sashkent3 avatar Feb 28 '25 16:02 sashkent3

@tefra just to clarify, the problem is not only with the type-hinting. The following XML is (incorrectly) parsed without error by the code generated from the above schema:

<Root>
    <TheFirstChoice>test</TheFirstChoice>
    <TheSecondChoice>test</TheSecondChoice>
</Root>

I believe this should be considered a bug.

sashkent3 avatar Mar 01 '25 13:03 sashkent3