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: embedded struct with XMLName field not being ignored #10538

Open
xiofen opened this Issue Apr 22, 2015 · 5 comments

Comments

Projects
None yet
7 participants
@xiofen

xiofen commented Apr 22, 2015

(See https://groups.google.com/forum/#!topic/golang-nuts/5aFvL5dZHvk )

The embedded struct with field name XMLName of type xml.Name (with no tag) is being ignored.

issue #7614 fixed a similar problem when the embedded struct had a tag.

Here's the test:

package main

import (
    "encoding/xml"
    "fmt"
)

type element struct {
    XMLName  xml.Name
    Children []interface{}
}

type svgstr struct {
    element
    Height string `xml:"height,attr,omitempty"`
    Width  string `xml:"width,attr,omitempty"`
}

type svgstr2 struct {
    XMLName  xml.Name
    Children []interface{}
    Height   string `xml:"height,attr,omitempty"`
    Width    string `xml:"width,attr,omitempty"`
}

func main() {
    s := svgstr{
        element: element{XMLName: xml.Name{Local: "svg", Space: "www.etc"}},
        Width:   "400",
        Height:  "200",
    }

    b, _ := xml.MarshalIndent(s, "", " ")
    fmt.Println(string(b))

    //normal behaviour below for comparison . no issues here
    s2 := svgstr2{
        XMLName: xml.Name{Local: "svg", Space: "www.etc"},
        Width:   "400",
        Height:  "200",
    }

    b, _ = xml.MarshalIndent(s2, "", " ")
    fmt.Println(string(b)) //works fine when not embedded
}

The output I get is:

<svgstr height="200" width="400"></svgstr>
<svg xmlns="www.etc" height="200" width="400"></svg>

Both should give the same result, as the second line, with name as "svg" and a xmlns attribute also supplied.

@ianlancetaylor ianlancetaylor added this to the Go1.5Maybe milestone Jun 3, 2015

@rsc rsc changed the title from encoding/xml - embedded struct with XMLName field not being ignored to encoding/xml: embedded struct with XMLName field not being ignored Jun 26, 2015

@rsc rsc modified the milestones: Go1.5, Go1.5Maybe Jul 20, 2015

@rsc

This comment has been minimized.

Contributor

rsc commented Jul 23, 2015

See #11841.

@rsc rsc modified the milestones: Go1.6, Go1.5 Jul 23, 2015

@rsc

This comment has been minimized.

Contributor

rsc commented Nov 25, 2015

Blocked on #13400.

@rsc rsc modified the milestones: Go1.7, Go1.6 Nov 25, 2015

@rsc rsc modified the milestones: Go1.8, Go1.7 May 18, 2016

@rsc rsc modified the milestones: Go1.9Early, Go1.8 Oct 26, 2016

@bradfitz bradfitz modified the milestones: Go1.9Early, Go1.10Early May 3, 2017

@bradfitz bradfitz modified the milestones: Go1.10Early, Go1.10 Jun 14, 2017

@rsc rsc modified the milestones: Go1.10, Go1.11 Nov 22, 2017

@gopherbot

This comment has been minimized.

gopherbot commented Apr 21, 2018

Change https://golang.org/cl/108616 mentions this issue: encoding/xml : fix panic on embedded unexported XMLName

@iWdGo

This comment has been minimized.

Contributor

iWdGo commented Apr 21, 2018

By laws of reflection, unexported fields are unreachable by .Value. Documentation mentions that reflect package is used. XMLName are allowed at any level of an inner struct but laws of
reflection still prevail.

The "element" field above is not exported and no value can be obtained for the XMLName. This is the origin of the difference and is normal. This issue can cause panic is the cases documented for the reflect
package.

The submitted fix checks that the assigned XMLName value can be obtained by the reflect package.
Otherwise, it is discarded like other fields.

@gopherbot

This comment has been minimized.

gopherbot commented Apr 27, 2018

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

@ianlancetaylor ianlancetaylor removed the Blocked label May 17, 2018

@bradfitz bradfitz modified the milestones: Go1.11, Go1.12 May 18, 2018

@ianlancetaylor ianlancetaylor added this to the Go1.12 milestone Jun 1, 2018

@ianlancetaylor ianlancetaylor modified the milestones: Go1.12, Go1.13 Dec 12, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment