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: XML tag name that consist of ':' doesn't get umarshaled properly #62075

Open
ormergi opened this issue Aug 16, 2023 · 8 comments
Open
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@ormergi
Copy link

ormergi commented Aug 16, 2023

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

$ go version
go version go1.19.9 linux/amd64

Does this issue reproduce with the latest release?

Reproduces using go 1.21 with go.dev/play

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

go version go1.19.9 linux/amd64

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/usr/bin"
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/gimme/.gimme/versions/go1.19.9.linux.amd64"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/gimme/.gimme/versions/go1.19.9.linux.amd64/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.9"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/root/go/src/kubevirt.io/kubevirt/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3975233845=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Umarshaling XML to a struct that has attribute with XML tag whos name consist of : wont unmarshal correctly and end up empty.

For example:

type QemuDomain struct {
	XMLName xml.Name `xml:"domain"`
	XmlNs   string   `xml:"xmlns:qemu,attr,omitempty"` <------ XmlNs set as XML attribute, with a name consist of ':'
}

The XmlNs attribute end up with empty string.

Reproducer https://go.dev/play/p/5l9WHxVBO5S

What did you expect to see?

I expect the XmlNs attribute to umarshal correctly and wont end up empty.

What did you see instead?

The XmlNs attribute end up empty.

@dmitshur dmitshur changed the title xml/encoding: XML tag name that consist of ':' doesnt get umarshaled properly encoding/xml: XML tag name that consist of ':' doesn't get umarshaled properly Aug 16, 2023
@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 16, 2023
@dmitshur dmitshur added this to the Backlog milestone Aug 16, 2023
@dmitshur
Copy link
Contributor

CC @rsc, @ianlancetaylor.

@jupenur
Copy link

jupenur commented Aug 16, 2023

@ormergi in the struct tag the namespace separator is space, i.e. xmlns qemu instead of xmlns:qemu: https://go.dev/play/p/FHqeA6az2AU

@ormergi
Copy link
Author

ormergi commented Aug 17, 2023

@ormergi in the struct tag the namespace separator is space, i.e. xmlns qemu instead of xmlns:qemu: https://go.dev/play/p/FHqeA6az2AU

@jupenur thanks for the feedback
I have tried what you suggest but it seems that marshaling the object again result with XmlNs not as expected:

<domain xmlns:_xmlns="xmlns" _xmlns:qemu="http://libvirt.org/schemas/domain/qemu/1.0"></domain>

Instead of:

`<domain xmlns:qemu="http://libvirt.org/schemas/domain/qemu/1.0"></domain>`

https://go.dev/play/p/2zdkbKMAZC1

@seankhliao
Copy link
Member

why aren't you using encoding/xml.Name.Space?

@zephyrtronium
Copy link
Contributor

We just hit this as well. I can't speak to @ormergi's case, but we are generating about a thousand Go types from XSD for an unfortunately picky downstream API. That API appears to require namespace prefixes rather than xmlns attributes on all but the top level. Using xml.Name creates exactly those xmlns attributes. The only way we've found to marshal attributes with namespace prefixes is to put the prefix in the XML tag like xml:"pidx:Invoice", but this causes unmarshalling to ignore those elements, per this issue.

Related:

@ydnar
Copy link

ydnar commented Aug 24, 2023

We have a fix in #48641: encoding/xml: support xmlns prefixes

https://go-review.googlesource.com/c/go/+/355353

@SoMuchForSubtlety
Copy link

Has there been any progress on this?

@hellodword
Copy link

hellodword commented May 9, 2024

My workaround using encoding/xml: https://gist.github.com/hellodword/fa97208b0e81baed8a75f8e40fb2ffea

I must to say it's very dirty and not working for many cases, but it works for me. Hope it'll be helpful for someone :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

8 participants