xgen icon indicating copy to clipboard operation
xgen copied to clipboard

Incorrectly generated code for `mixed="true"` complex types

Open halostatue opened this issue 8 months ago • 1 comments

Description

If a complexType is defined with mixed="true", it should collect the innerXml or CDATA in addition to the tags. The examples in this ticket are generated using the SFCC catalog.xsd which also requires the xml.xsd file from https://salesforcecommercecloud.github.io/b2c-dev-doc/docs/current/xsd/.

Inside of it, the following types are declared:

    <xsd:complexType name="sharedType.CustomAttributes" mixed="false">
        <xsd:sequence>
            <xsd:element name="custom-attribute" type="sharedType.CustomAttribute" minOccurs="0" maxOccurs="unbounded" />
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="sharedType.CustomAttribute" mixed="true">
        <xsd:sequence>
            <xsd:element name="value" type="simpleType.Generic.String" minOccurs="0" maxOccurs="unbounded" />
        </xsd:sequence>
        <xsd:attribute name="attribute-id" type="simpleType.Generic.NonEmptyString.256" use="required" />
        <xsd:attribute ref="xml:lang" />
    </xsd:complexType>

This generates the following Go code:

// SharedTypeCustomAttributes ...
type SharedTypeCustomAttributes struct {
	XMLName         xml.Name                     `xml:"sharedType.CustomAttributes"`
	Customattribute []*SharedTypeCustomAttribute `xml:"custom-attribute"`
}

// SharedTypeCustomAttribute ...
type SharedTypeCustomAttribute struct {
	XMLName         xml.Name `xml:"sharedType.CustomAttribute"`
	AttributeidAttr string   `xml:"attribute-id,attr"`
	XmlLangAttr     string   `xml:"xml:lang,attr,omitempty"`
	Value           []string `xml:"value"`
}

In most cases from SFCC files, this is presented as <custom-attribute attribute-id="foo">bar</custom-attribute>, and using xgen there's no way to get access to the bare contents of the optional <value/> tag.

Steps to reproduce the issue:

  1. Use the XSD files mentioned above.
  2. Run xgen -i catalog.xsd -l Go -o tmp/catalog

Describe the results you received:

Note that there's no way of getting the inner XML, only Value[].

Describe the results you expected:

A way to get the inner XML in addition to Value[].

Output of go version:

go version go1.22.3 darwin/arm64

xgen version or commit ID:

0.1.0

Environment details (OS, physical, etc.):

macOS 14.5

halostatue avatar Jun 21 '24 17:06 halostatue