golang/go

encoding/xml: failure to handle conflicting tags in different namespaces

jbardin opened this issue · 9 comments

What does 'go version' print?

go1.3.1

What steps reproduce the problem?

Try to decode document with the same tag in multiple namespaces
<example>
    <title>Example</title>
    <link>http://example.com/default<;/link>
    <ns:link xmlns:ns="http://www.w3.org/2005/Atom";>http://example.com/ns<;/ns:link>
</example>`

http://play.golang.org/p/FT56UXolGO


What happened?
main.Example field "Link" with tag "link" conflicts with field
"AtomLink" with tag "http://www.w3.org/2005/Atom link"

What should have happened instead?

It should be possible to decode both tags in this document.

Also, if the name-spaced tag isn't provided, the second "link" element shadows
the first though it's in another namespace.

Comment 1:

Labels changed: added repo-main, release-none.

Comment 3 by glen.newton:

I am also encountering this issue & is impacting my application. Note that issue only
occurs when one of the namespaces is empty. See same example with two explicit name
spaces: http://play.golang.org/p/tpGAWsc_Yo <-- Works OK.

I just ran into this issue working with a legacy SOAP API that had a lot of namespaces in the fields.

I am also having this issue (funnily enough with RSS readers as well). Is there a workaround, yet or do we have to wait for Go 10 (ie. #14407)

I found a workaround by creating a custom string-type with a custom unmarshal function. If anyone is interested, here it is:

type NonNamespaceString string

func (s *NonNamespaceString) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	if start.Name.Space != "" {
		// We do not want a namespace, so we need to consume it
		d.Skip()
		return nil
	} else {
		str := ""
		d.DecodeElement(&str, &start)
		*s = NonNamespaceString(str)
	}

	return nil
}

I also create a gist with a unit test, if you are interested.

It seems to work and can almost be used as a drop-in replacement for any string. You just need to cast it back to a string if you want to use it.

Change https://golang.org/cl/106575 mentions this issue: encoding/xml : add check of namespaces to detect field names conflicts

iwdgo commented

Comparing namespaces of fields was missing and detecting false conflicts. A fix is submitted.

Change https://golang.org/cl/109855 mentions this issue: encoding/xml : Fixes to enforce XML namespace standard

Any update on this? I'm running go 1.12.6 and I'm still getting this issue.