Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Nested non-nullable elements in choice not supported #339

Closed
jmatss opened this issue Jun 15, 2022 · 0 comments · Fixed by #340
Closed

Nested non-nullable elements in choice not supported #339

jmatss opened this issue Jun 15, 2022 · 0 comments · Fixed by #340

Comments

@jmatss
Copy link
Contributor

jmatss commented Jun 15, 2022

Because of how XmlSchemaClassGenerator generates C# properties for choices (see choice), all properties generated from items in a choice must be nullable.

The current implementation of XmlSchemaClassGenerator only forces the direct children of choices to nullable. It does not force any descendants nested deeper than its children to nullable and might therefore generate invalid code in those cases.

For example the following schema file:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Root">
    <xs:complexType>
      <xs:choice>
        <xs:sequence>
          <xs:element name="Element" type="xs:int"/>
        </xs:sequence>
        <xs:group ref="GroupRef"/>
      </xs:choice>
    </xs:complexType>
  </xs:element>

  <xs:group name="GroupRef">
    <xs:sequence>
      <xs:element name="GroupRefChild" type="xs:int"/>
    </xs:sequence>
  </xs:group>
</xs:schema>

generates a class that looks something like this:

public partial class Root
{
    [System.ComponentModel.DataAnnotations.RequiredAttribute()]
    public int Element { get; set; }

    [System.ComponentModel.DataAnnotations.RequiredAttribute()]
    public int GroupRefChild { get; set; }
}

which isn't valid since both the properties are required when they must be nullable according to how XmlSchemaClassGenerator handles choice elements.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant