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

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

Open mikam-discovery opened 2 years ago

mikam-discovery commented 2 years ago

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"?>
``` 2. Run `xgen -i xsd/example.xsd -p example_xgen -o xgen_output -l Go` 3. 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`:** ```text go version go1.17.7 darwin/amd64 ``` **xgen version or commit ID:** ```text v0.0.0-20220303053931-2afb9de4af9b ``` **Environment details (OS, physical, etc.):** OSX version 12.3