xuri / xgen

XSD (XML Schema Definition) parser and Go/C/Java/Rust/TypeScript code generator
BSD 3-Clause "New" or "Revised" License
313 stars 74 forks source link

Enums not generated properly in certain circumstances #59

Open brianpursley opened 2 years ago

brianpursley commented 2 years ago

Description Sometimes, depending on the schema, enums are not generated correctly, or are not generated at all.

Steps to reproduce the issue: Schema:

<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:here="http://example.org/" targetNamespace="http://example.org/">

    <element name="TopLevel">
        <complexType>
            <choice minOccurs="1" maxOccurs="unbounded">
                <element name="TShirt" type="here:SizeType" minOccurs="0" />
                <element name="Style" minOccurs="0">
                    <simpleType>
                        <restriction base="string">
                            <enumeration value="Value1" />
                            <enumeration value="Value2" />
                        </restriction>
                    </simpleType>
                </element>
            </choice>
        </complexType>
    </element>

    <simpleType name="SizeType">
        <restriction base="string">
            <enumeration value="Small" />
            <enumeration value="Medium" />
            <enumeration value="Large" />
        </restriction>
    </simpleType>

    <simpleType name="HasNoElements">
        <restriction base="string">
        </restriction>
    </simpleType>

</schema>

Command:

xgen -l TypeScript -i example.xsd

Describe the results you received:

// Code generated by xgen. DO NOT EDIT.

// TopLevel ...
export class TopLevel {
    TShirt: Array<SizeType>;
    Style: string;
}

// Style ...
export type Style = string;

// HasNoElements ...
export enum HasNoElements {
    Value1 = 'Value1',
    Value2 = 'Value2',
    Small = 'Small',
    Medium = 'Medium',
    Large = 'Large',
}

Describe the results you expected:

// Code generated by xgen. DO NOT EDIT.

// TopLevel ...
export class TopLevel {
    TShirt: Array<SizeType>;
    Style: string;
}

// Style ...
export type Style = string;

// SizeType ...
export enum SizeType {
    Small = 'Small',
    Medium = 'Medium',
    Large = 'Large',
}

// HasNoElements ...
export type HasNoElements = string;

Output of go version:

go version go1.18.1 linux/amd64

xgen version or commit ID:

98e2a901798bddb93c5e532c98afc1f5f139d47e

Environment details (OS, physical, etc.):

$ uname -a
Linux cinlogic-xps13 5.15.0-41-generic #44~20.04.1-Ubuntu SMP Fri Jun 24 13:27:29 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Other Information: If I omit the HasNoElements enum from the schema, the generated code also omits the SizeType enum for some reason:

<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:here="http://example.org/" targetNamespace="http://example.org/">

    <element name="TopLevel">
        <complexType>
            <choice minOccurs="1" maxOccurs="unbounded">
                <element name="TShirt" type="here:SizeType" minOccurs="0" />
                <element name="Style" minOccurs="0">
                    <simpleType>
                        <restriction base="string">
                            <enumeration value="Value1" />
                            <enumeration value="Value2" />
                        </restriction>
                    </simpleType>
                </element>
            </choice>
        </complexType>
    </element>

    <simpleType name="SizeType">
        <restriction base="string">
            <enumeration value="Small" />
            <enumeration value="Medium" />
            <enumeration value="Large" />
        </restriction>
    </simpleType>

</schema>
// Code generated by xgen. DO NOT EDIT.

// TopLevel ...
export class TopLevel {
    TShirt: Array<SizeType>;
    Style: string;
}

// Style ...
export type Style = string;

Also...

If I omit the Style element from the top level element, then it works fine:

<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:here="http://example.org/" targetNamespace="http://example.org/">

    <element name="TopLevel">
        <complexType>
            <choice minOccurs="1" maxOccurs="unbounded">
                <element name="TShirt" type="here:SizeType" minOccurs="0" />
            </choice>
        </complexType>
    </element>

    <simpleType name="SizeType">
        <restriction base="string">
            <enumeration value="Small" />
            <enumeration value="Medium" />
            <enumeration value="Large" />
        </restriction>
    </simpleType>

</schema>

Output:

// Code generated by xgen. DO NOT EDIT.

// TopLevel ...
export class TopLevel {
    TShirt: string;
}

// SizeType ...
export enum SizeType {
    Small = 'Small',
    Medium = 'Medium',
    Large = 'Large',
}

So there is some interaction involving the top level element I think. Also, I think the order of the elements in the schema seems to have some effect too. There seem to be a lot of factors involved in reproducing this.