Skip to content

encoding/xml: embedded interface doesn't get marshaled correctly #69941

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

Closed
otaxhu opened this issue Oct 20, 2024 · 6 comments
Closed

encoding/xml: embedded interface doesn't get marshaled correctly #69941

otaxhu opened this issue Oct 20, 2024 · 6 comments
Labels
Documentation Issues describing a change to documentation. NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@otaxhu
Copy link

otaxhu commented Oct 20, 2024

Go version

go version go1.22.4 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/albian/.cache/go-build'
GOENV='/home/albian/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/albian/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/albian/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.4'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/albian/dev/problem/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2437414976=/tmp/go-build -gno-record-gcc-switches'

What did you do?

Embedding interface type in a struct, pass struct that implements that interface, then marshal it:
https://go.dev/play/p/SBfpU8nOB2S

Embedding concrete struct type in a struct, then marshal it:
https://go.dev/play/p/Wwf-DztRIlO

What did you see happen?

Output of first Playground link (https://go.dev/play/p/SBfpU8nOB2S):

<animal xmlns="urn:whatever">
    <Animal>
        <sound>Woof!</sound>
    </Animal>
</animal>

Output of second Playground link (https://go.dev/play/p/Wwf-DztRIlO):

<animal xmlns="urn:whatever">
    <sound>Woof!</sound>
</animal>

What did you expect to see?

I expected to get second output in both first and second Playground links.

According to the encoding/xml documentation, it says this two things:

https://pkg.go.dev/encoding/xml#Marshal

  • an anonymous struct field is handled as if the fields of its value were part of the outer struct.

Marshal handles an interface value by marshaling the value it contains or, if the interface value is nil, by writing nothing.

So it should know that the interface value is a struct and is embedded, so it should treat the inner struct values as if it were part of the outer struct (basically accomplishing the second output)

@otaxhu otaxhu changed the title encoding/xml: Interface doesn't get marshaled correctly encoding/xml: embedded interface doesn't get marshaled correctly Oct 20, 2024
@seankhliao
Copy link
Member

this is the same behavior as json, perhaps we just need to document this, ref #54322

@otaxhu
Copy link
Author

otaxhu commented Oct 20, 2024

I don't think it should be like that, we are embedding the interface, so it should follow the semantics of embedding (outer struct contains all of the fields of inner struct).

@seankhliao
Copy link
Member

interfaces don't have fields though, they have methods. and (most) methods of structs aren't called when marshaling

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/621076 mentions this issue: encoding/xml: document that embedded interfaces look like non-embedded ones

@otaxhu
Copy link
Author

otaxhu commented Oct 20, 2024

Ok that makes sense, sorry.

Updating the docs would be good.

@dmitshur dmitshur added Documentation Issues describing a change to documentation. NeedsFix The path to resolution is known, but the work has not been done. labels Oct 21, 2024
@dmitshur dmitshur added this to the Go1.24 milestone Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Issues describing a change to documentation. NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants