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

Element ref not generating and linking types correctly. #130

Open
oldelvet opened this issue Feb 10, 2021 · 3 comments
Open

Element ref not generating and linking types correctly. #130

oldelvet opened this issue Feb 10, 2021 · 3 comments

Comments

@oldelvet
Copy link

I have been experimenting with go-xml to see if I can use it to interface with an existing Java server application. The existing schema used by the Java code is not generating Go code as I would expect. In have cut things down to a minimal example that exhibits the problem. I can see two issues that probably have a common cause:

  • c4d6128 "Ensure all elements have a type" changes the code generation to use type string instead of struct User (or Anon1)
  • With or without c4d6128 applied the User element is generated as a type struct twice. Once as type User and once as Anon1. The Ulist struct references User entries of type Anon1.

If I change the User element to use a separate complexType definition then only one User struct is defined and Ulist references that as I would expect.

It seems like the element ref is not linking up the types correctly when generating the code.

Test files are attached but the key parts are (testref.xsd)

<xs:element ref="my:user" minOccurs="0" maxOccurs="unbounded">

gets generated into either (testref.go)

User []Anon1 xml:"http://example.com/public/schema/test1 user,omitempty"

or with c4d6128 applied (testref2.go)

User []string xml:"http://example.com/public/schema/test1 user,omitempty"

If I convert to a separate complexType (testtype.xsd) it generates as (testtype.go)

User []Usert xml:"http://example.com/public/schema/test1 user,omitempty"

with only a single structure (named Usert) in the code.

Test files in testdata.zip used

wsdlgen as follows.

//go:generate wsdlgen -pkg testref -r "Type$DOLLAR -> " -o testref.go testref.xsd
//go:generate wsdlgen -pkg testtype -r "Type$DOLLAR -> " -o testtype.go testtype.xsd

@yugendra
Copy link

yugendra commented Apr 5, 2021

I am also facing similar issue with xsdgen. Not sure if it is exactly the same issue but my XSD and generated code is as below:

XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.example.org/schemas" targetNamespace="http://www.example.org/schemas" elementFormDefault="qualified">
	<xsd:complexType name="ElementOneType">
       	<xsd:element ref="ElementTwo" minOccurs="0"/>
    </xsd:complexType>
    <xsd:element name="ElementOne" type="ElementOneType">
		<xsd:annotation>
			<xsd:documentation>ElementOne documentation.</xsd:documentation>
		</xsd:annotation>
	</xsd:element>
    <xsd:complexType name="ElementTwoType">
		<xsd:attribute name="elementTwoID" type="xsd:string" use="required">
			<xsd:annotation>
				<xsd:documentation>elementTwoID documentation.</xsd:documentation>
			</xsd:annotation>
		</xsd:attribute>
	</xsd:complexType>
    <xsd:element name="ElementTwo" type="ElementTwoType">
		<xsd:annotation>
			<xsd:documentation>ElementTwo documentation.</xsd:documentation>
		</xsd:annotation>
	</xsd:element>
</xsd:schema>

Generated GO code:

// Code generated by xsdgen. DO NOT EDIT.

package go_xml_test

type ElementOneType struct {
	ElementTwo string `xml:"http://www.example.org/schemas ElementTwo,omitempty"`
}

type ElementTwoType struct {
	ElementTwoID string `xml:"elementTwoID,attr"`
}

xsdgen is as follows.

/xsdgen -o test.go -pkg generated test.xsd

As we can see in generated code type for ElementTwo is string not struct ElementTwoType.
Is it the same issue or do we need separate issue for this?

@oldelvet
Copy link
Author

Yes. That seems to be the same issue I am encountering.

@ivanrybin
Copy link

ivanrybin commented Jan 22, 2022

Just convert ref attribute to name + type attributes

Before
<xsd:element ref="sns:SomeElement">

After
<xsd:element name="SomeElement" type="sns:SomeElement">

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

3 participants