spreeuwers/xsd2ts

Support for `element ref`

Closed this issue · 2 comments

Hello,
Let me first say that this library is really good and something I was looking for for a long time.
But there is one bug in processing referenced elements (<xs:element ref>).

Let's take this example (adapted from XEP-0004):

<?xml version='1.0' encoding='UTF-8'?>

<xs:schema
    xmlns:xs='http://www.w3.org/2001/XMLSchema'
    targetNamespace='jabber:x:data'
    xmlns='jabber:x:data'
    elementFormDefault='qualified'>

  <xs:annotation>
    <xs:documentation>
      The protocol documented by this schema is defined in
      XEP-0004: http://www.xmpp.org/extensions/xep-0004.html
    </xs:documentation>
  </xs:annotation>

    <xs:complexType name="forms">
      <xs:sequence>
  <xs:element name='x'>
    <xs:complexType>
      <xs:sequence>
        <xs:element name='instructions'
                    minOccurs='0'
                    maxOccurs='unbounded'
                    type='xs:string'/>
        <xs:element name='title' minOccurs='0' type='xs:string'/>
        <xs:element ref='field' minOccurs='0' maxOccurs='unbounded'/>
        <xs:element ref='reported' minOccurs='0' maxOccurs='1'/>
        <xs:element ref='item' minOccurs='0' maxOccurs='unbounded'/>
      </xs:sequence>
      <xs:attribute name='type' use='required'>
        <xs:simpleType>
          <xs:restriction base='xs:NCName'>
            <xs:enumeration value='cancel'/>
            <xs:enumeration value='form'/>
            <xs:enumeration value='result'/>
            <xs:enumeration value='submit'/>
          </xs:restriction>
        </xs:simpleType>
      </xs:attribute>
    </xs:complexType>
  </xs:element>

  <xs:element name='field'>
    <xs:complexType>
      <xs:sequence>
        <xs:element name='desc' minOccurs='0' type='xs:string'/>
        <xs:element name='required' minOccurs='0' type='empty'/>
        <xs:element ref='value' minOccurs='0' maxOccurs='unbounded'/>
        <xs:element ref='option' minOccurs='0' maxOccurs='unbounded'/>
      </xs:sequence>
      <xs:attribute name='label' type='xs:string' use='optional'/>
      <xs:attribute name='type' use='optional'>
        <xs:simpleType>
          <xs:restriction base='xs:NCName'>
            <xs:enumeration value='boolean'/>
            <xs:enumeration value='fixed'/>
            <xs:enumeration value='hidden'/>
            <xs:enumeration value='jid-multi'/>
            <xs:enumeration value='jid-single'/>
            <xs:enumeration value='list-multi'/>
            <xs:enumeration value='list-single'/>
            <xs:enumeration value='text-multi'/>
            <xs:enumeration value='text-private'/>
            <xs:enumeration value='text-single'/>
          </xs:restriction>
        </xs:simpleType>
      </xs:attribute>
      <xs:attribute name='var' type='xs:string' use='optional'/>
    </xs:complexType>
  </xs:element>

  <xs:element name='option'>
    <xs:complexType>
      <xs:sequence>
        <xs:element ref='value'/>
      </xs:sequence>
      <xs:attribute name='label' type='xs:string' use='optional'/>
    </xs:complexType>
  </xs:element>

  <xs:element name='value' type='xs:string'/>

  <xs:element name='reported'>
    <xs:annotation>
      <xs:documentation>
        When contained in a "reported" element, the "field" element
        SHOULD NOT contain a "value" child.
      </xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element ref='field' maxOccurs='unbounded'/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name='item'>
    <xs:complexType>
      <xs:sequence>
        <xs:element ref='field' maxOccurs='unbounded'/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:simpleType name='empty'>
    <xs:restriction base='xs:string'>
      <xs:enumeration value=''/>
    </xs:restriction>
  </xs:simpleType>
      </xs:sequence>
    </xs:complexType>

</xs:schema>

It generates the following code:

export class forms {
    public x: string[];
    public field: string;
    public option: any;
    public value: string;
    public reported: any;
    public item: any[];

    public constructor(props?: forms) {
        this["@class"] = ".forms";

        (<any>Object).assign(this, <any> props);
    }
}

Note that field is incorrectly inferred to be string but it should be the referenced element. Inlining the element helps.

Thanks for your comment. I only implemented refs for groups.
I' ll look into it..

Fixed