Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mikam-discovery opened this issue Apr 6, 2022 · 0 comments

Comments

@mikam-discovery
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant