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: fail to roundtrip with interface inside #46432

Open
StepanBakshayev opened this issue May 28, 2021 · 1 comment
Open

encoding/xml: fail to roundtrip with interface inside #46432

StepanBakshayev opened this issue May 28, 2021 · 1 comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@StepanBakshayev
Copy link

StepanBakshayev commented May 28, 2021

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

$ go version
go version go1.16.2 linux/amd64

Does this issue reproduce with the latest release?

Yes... as playground says "The current version is go1.16.4."

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/stepan/.cache/go-build"
GOENV="/home/stepan/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/stepan/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/stepan/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.16"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.16/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.2"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/stepan/develop/ziax-webhook/go.mod"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3078049904=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I use interface to build structure of xml in runtime. I dump to xml successfully, but fail to load data back.

What did you expect to see?

I expect to do roundtrip encoding-decoding with the same structure of type-value combination in golang.

What did you see instead?

The current implementation use name of interface field in parsing instead of XMLName inside referenced structure.

Testcase

Source of https://play.golang.org/p/bAMPYS-rjSe :

package main

import (
	"bytes"
	"encoding/xml"
	"fmt"
)

func main() {
	type Envelope struct {
		XMLName struct {} `xml:"envelope"`
		Body interface {}
	}
	type Data struct {
		XMLName struct {} `xml:"person"`
		Name string `xml:"name"`
	}

	envelopeOrigin := Envelope {
		Body: Data {Name: "Ivanov"},
	}

	var buf bytes.Buffer
	enc := xml.NewEncoder(&buf)
	enc.Encode(envelopeOrigin)
	enc.Flush()

	if buf.String() != "<envelope><person><name>Ivanov</name></person></envelope>" {
		panic("Test is broken.")
	}

	envelopeReconstructed := Envelope {
		Body: Data {},
	}
	dec := xml.NewDecoder(&buf)
	dec.Decode(&envelopeReconstructed)

	if envelopeOrigin != envelopeReconstructed {
		panic(fmt.Sprintf("Deserialization failed origin %+v does not match reconstructed %+v", envelopeOrigin.Body.(Data), envelopeReconstructed.Body.(Data)))
	}
}
@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 30, 2021
@seankhliao
Copy link
Member

seems non trivial to fix, #6836 / CL 33140043 added partial support but i believe the unmarshaling still operates primarily on type info

cc @rsc

@seankhliao seankhliao added this to the Unplanned milestone Aug 20, 2022
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

2 participants