xuri / xgen

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

Child element omitted if attribute with enumeration is passed #28

Closed fwielstra closed 3 years ago

fwielstra commented 3 years ago

Description

I've tried to boil it down to the simplest example. I have an XSD as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="template">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="requests"/>
        <xs:element minOccurs="0" ref="responses"/>
      </xs:sequence>
      <xs:attribute name="type" use="required">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:enumeration value="avp"/>
            <xs:enumeration value="condition"/>
            <xs:enumeration value="routing"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:attribute>
    </xs:complexType>
  </xs:element>
  <xs:element name="requests" />
  <xs:element name="responses" />
</xs:schema>

After generating, the following is generated:

// Code generated by xgen. DO NOT EDIT.

package schema

import (
    "encoding/xml"
)

// Template ...
type Template struct {
    XMLName  xml.Name    `xml:"template"`
    TypeAttr interface{} `xml:"type,attr"`
    Requests *Requests   `xml:"requests"`
    Template string      `xml:"template"`
}

// Requests ...
type Requests *Requests

// Responses ...
type Responses *Responses

This is missing the Responses key in the Template struct, but adds a Template string property from.. somewhere.

Removing the <xs:simpleType> segment:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="template">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="requests" />
        <xs:element minOccurs="0" ref="responses" />
      </xs:sequence>
      <xs:attribute name="type" use="required" />
    </xs:complexType>
  </xs:element>
  <xs:element name="requests" />
  <xs:element name="responses" />
</xs:schema>

results in a struct that looks more correct:

// Template ...
type Template struct {
    XMLName   xml.Name    `xml:"template"`
    TypeAttr  interface{} `xml:"type,attr"`
    Requests  *Requests   `xml:"requests"`
    Responses *Responses  `xml:"responses"`
}

// Requests ...
type Requests *Requests

// Responses ...
type Responses *Responses

Output of go version:

go version go1.16.2 darwin/amd64

xgen version or commit ID:

v0.0.0-20210301142127-04f2cd700cdb

Environment details (OS, physical, etc.): MacOS, physical

alexandre-normand commented 3 years ago

@xuri : It looks like I didn't use the right message format for github to auto-close this issue with my fix in https://github.com/xuri/xgen/pull/34. Do you mind closing it since it's now fixed?

xuri commented 3 years ago

I closed this issue, thanks @alexandre-normand.