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

encoding/xml: Marshalling change for namespace of XML-element without xml tag in corresponding go-struct in go 1.21+ #64166

Closed
LenaBullens opened this issue Nov 15, 2023 · 1 comment

Comments

@LenaBullens
Copy link

LenaBullens commented Nov 15, 2023

What version of Go are you using (go version)?

1.21.4

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

windows/amd64

What did you do?

Consider following code snippet:

func main() {
	type B struct {
		XMLName xml.Name
		C       bool     `xml:"c"`
	}

	type A struct {
		XMLName xml.Name `xml:"http://example.com a"`
		B       B
	}

	a := A{
		B: B{
			C: true,
		},
	}

	output, err := xml.Marshal(&a)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Printf("output: %s", string(output))
}

Output in go 1.20.X

output: <a xmlns="http://example.com"><B><c>true</c></B></a>

Output in go 1.21+ (including 1.21.4)

output: <a xmlns="http://example.com"><B xmlns=""><c>true</c></B></a>

Remarks

This issue seems related to #61881 If one adds a tag to the XMLName field in B

	type B struct {
		XMLName xml.Name `xml:"b"`
		C       bool     `xml:"c"`
	}

then the empty namespace does not show up in the marshall result (in 1.21.1+).

@seankhliao
Copy link
Member

I believe this is working as intended.
The order of precedence we have is:

  • the tag on the XMLName field, if the data is a struct
  • the value of the XMLName field of type Name
  • the tag of the struct field used to obtain the data
  • the name of the struct field used to obtain the data
  • the name of the marshaled type

and #7113 / CL 108796 requires the enforcement of xmlns values from xml.Name

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Nov 15, 2023
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

2 participants