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

Array is generated althought minOccurs = 1 and maxOccurs = 1 #23

Closed syvanpera closed 3 years ago

syvanpera commented 3 years ago

Description

When the xsd file has an element with minOccurs="1" and maxOccurs="1", an array of elements if generated although in this case just a single element would be appropriate.

Steps to reproduce the issue:

  1. Use for example the following xsd file
    <?xml version="1.0" encoding="utf-8"?>
    <xsd:schema version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="SomeXmlFile">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="SomeElement" type="string" minOccurs="1" maxOccurs="1"  />
        <xsd:element name="SomeOtherElement"  type="string" minOccurs="1" maxOccurs="1" />
      </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    </xsd:schema>
  2. Run xgen -i test.xsd -o test -l Go

Describe the results you received: The generated Go file contains:

 // Code generated by xgen. DO NOT EDIT.
package schema

// SomeXmlFile ...
type SomeXmlFile struct {
  SomeElement      []string `xml:"SomeElement"`
  SomeOtherElement []string `xml:"SomeOtherElement"`
}

Describe the results you expected:

 // Code generated by xgen. DO NOT EDIT.
package schema

// SomeXmlFile ...
type SomeXmlFile struct {
  SomeElement      string `xml:"SomeElement"`
  SomeOtherElement string `xml:"SomeOtherElement"`
}
syvanpera commented 3 years ago

Haven't really gone through all the possible places, but shouldn't this check in xmlElement.go:

        if attr.Name.Local == "maxOccurs" {
            if attr.Value != "0" {
                e.Plural = true
            }
        }

rather be:

        if attr.Name.Local == "maxOccurs" {
            if attr.Value > "1" {
                e.Plural = true
            }
        }
xuri commented 3 years ago

Thanks for your issue, I have fixed it, please try to use the master branch code.