Problems with mandatory collection with optional member

May 6, 2014 at 4:36 PM
Edited May 6, 2014 at 4:37 PM
Hallo,

I hope this project is still under development.

According to Issue 10718 I have found a probelm with mandatory collections with optional member.

My xsd is looking like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:dev="urn:test:dev:xsd:1"
            targetNamespace="urn:test:dev:xsd:1"
            xmlns:test="urn:test:dev:xsd:1" elementFormDefault="unqualified"
            attributeFormDefault="unqualified" version="1.0">
            
  <xsd:simpleType name="ItemType">
    <xsd:restriction base="xsd:string"/>
  </xsd:simpleType>
  
  <xsd:complexType name="CollectionType">
        <xsd:sequence>
          <xsd:element name="items">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="item" type="dev:ItemType" minOccurs="0"
                             maxOccurs="unbounded"/>
              </xsd:sequence>
            </xsd:complexType>
          </xsd:element>

        </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
The class CollectionType should have a mandatory list named items, but the list itself could be empty. After serealizing an instance of this class with an empty list the items element is missing therefore the xml is invalid. For better understandingh here the generated class:
/// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.18408")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:test:dev:xsd:1")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="urn:test:dev:xsd:1", IsNullable=true)]
    public partial class CollectionType
    {
        
        private System.Collections.Generic.List<string> itemsField;
        
        /// <remarks/>
        [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        [System.Xml.Serialization.XmlArrayItemAttribute("item", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
        public System.Collections.Generic.List<string> items
        {
            get
            {
                return this.itemsField;
            }
            set
            {
                this.itemsField = value;
            }
        }
        
        public virtual bool ShouldSerializeitems()
        {
            return ((this.items != null) 
                        && (this.items.Count > 0));
        }
    }
After i have done some researches I found out that during the serealizing process the method "ShouldSerializeitems" is called and that method suppressed the serialization of the empty list.

A little code review lead me to the class Thinktecture.Tools.Web.Services.CodeGeneration.CollectionTypeGenerator within the method HandleShouldSerialize the ShouldSerialize method will be generated based on the IsNullable argument of the XmlArrayItemAttribut. But for my understanding it doesnt matter if the list members are optional or not only if the list itself is optional the serialization should be suppressd.

Hoping for a quick response

Thanks for your help Kind Regards

Lars