xgen icon indicating copy to clipboard operation
xgen copied to clipboard

xgen not generating omitempty for minOccurs = 0 & not creating arrays when maxOccurs = 'unbounded'

Open mikam-discovery opened this issue 3 years ago • 0 comments

Description

While generating Go XML objects with xgen, I have been noticing that omitempty is not being added to elements that have minOccurs='0' when those elements should be optional. I have also noticed that when an element contains <xsd:sequence maxOccurs='unbounded'> it is not producing an array of the element inside the sequence to allow for multiples of the element.

Steps to reproduce the issue:

  1. Run xgen on an xsd file similar to this example
<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>

 <xsd:element name='CoreContainer'>
  <xsd:complexType>
   <xsd:sequence>
    <xsd:element ref='VideoDocument' minOccurs='0' maxOccurs='unbounded'/>
   </xsd:sequence>
   <xsd:anyAttribute processContents="skip"/>
  </xsd:complexType>
 </xsd:element>

 <xsd:simpleType name='not_empty_string'>
  <xsd:restriction base="xsd:string">
   <xsd:pattern value="\s*\S+(.|\n|\r)*"/>
  </xsd:restriction>
 </xsd:simpleType>

 <xsd:element name='VideoDocument'>
  <xsd:complexType>
   <xsd:all>
    <xsd:element ref='metaData' minOccurs='0' />
   </xsd:all>
   <xsd:attribute name='video_id' type='not_empty_string' use='required'/>
  </xsd:complexType>
 </xsd:element>

 <xsd:element name='metaData'>
  <xsd:complexType>
   <xsd:sequence maxOccurs='unbounded'>
    <xsd:element ref='datumItem'/>
   </xsd:sequence>
  </xsd:complexType>
 </xsd:element>

 <xsd:element name='datumItem'>
  <xsd:complexType>
    <xsd:all>
     <xsd:element name='value' type='not_empty_string'/>
     <xsd:element name='label' type='not_empty_string'/>
    </xsd:all>
  </xsd:complexType>
 </xsd:element>

</xsd:schema>
  1. Run xgen -i xsd/example.xsd -p example_xgen -o xgen_output -l Go
  2. Get output file with the following xml objects
// Code generated by xgen. DO NOT EDIT.

package ivi_xgen

import (
	"encoding/xml"
)

// CoreContainer ...
type CoreContainer struct {
	VideoDocument []*VideoDocument `xml:"VideoDocument"`
}

// Notemptystring ...
type Notemptystring string

// VideoDocument ...
type VideoDocument struct {
	VideoidAttr string          `xml:"video_id,attr"`
	MetaData    *MetaData `xml:"metaData"`
}

// MetaData ...
type MetaData struct {
	XMLName   xml.Name   `xml:"metaData"`
	DatumItem *DatumItem `xml:"datumItem"`
}

// DatumItem ...
type DatumItem struct {
	XMLName xml.Name `xml:"datumItem"`
	Value   string   `xml:"value"`
	Label   string   `xml:"label"`
}

Describe the results you received:

The results I received:

// VideoDocument ...
type VideoDocument struct {
	VideoidAttr string          `xml:"video_id,attr"`
	MetaData    *MetaData `xml:"metaData"`
}

// MetaData ...
type MetaData struct {
	XMLName   xml.Name   `xml:"metaData"`
	DatumItem *DatumItem `xml:"datumItem"`
}

MetaData is not omitempty and DatumItem in Metadata, only allows for one DatumItem when I should be able to have multiple.

Describe the results you expected:

// VideoDocument ...
type VideoDocument struct {
	VideoidAttr string          `xml:"video_id,attr"`
	MetaData    *MetaData `xml:"metaData,omitempty"`
}

// MetaData ...
type MetaData struct {
	XMLName   xml.Name    `xml:"metaData"`
	DatumItem []DatumItem `xml:"datumItem"`
}

MetaData is optional and you can have multiple DatumItems in MetaData.

Output of go version:

go version go1.17.7 darwin/amd64

xgen version or commit ID:

v0.0.0-20220303053931-2afb9de4af9b

Environment details (OS, physical, etc.): OSX version 12.3

mikam-discovery avatar Apr 06 '22 20:04 mikam-discovery