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: Unmarshal nested field tags only work with dashes removed #2406

Closed
gopherbot opened this Issue Oct 28, 2011 · 6 comments

Comments

Projects
None yet
3 participants
@gopherbot
Copy link

gopherbot commented Oct 28, 2011

by pcrosby:

Run this test:

import (
        "strings"
        "testing"
        "xml"
)

var cr =
`<customer><id>xxxx</id><parent-dash><child-dash><name>xxx</name></child-dash><child-dash><name>yyy</name></child-dash></parent-dash></customer>`

type RespBug struct {
    ID string `xml:"id"`
    Children []Child `xml:"parent-dash>child-dash"`
}

type RespWorks struct {
    ID string `xml:"id"`
    Children []Child `xml:"parentdash>childdash"`
}

type Child struct {
    Name string `xml:"name"`
}


func TestBug(t *testing.T) {
        r := new(RespBug)
        err := xml.Unmarshal(strings.NewReader(cr), r)
        if err != nil {
                t.Errorf("didn't expect error: %s", err)
        }
        if len(r.Children) != 2 {
                t.Errorf("expected 2 children, got %d", len(r.Children))
        }
}

func TestWorks(t *testing.T) {
        r := new(RespWorks)
        err := xml.Unmarshal(strings.NewReader(cr), r)
        if err != nil {
                t.Errorf("didn't expect error: %s", err)
        }
        if len(r.Children) != 2 {
                t.Errorf("expected 2 children, got %d", len(r.Children))
        }
}

/* end of code */

TestBug will fail, TestWorks will pass.  It took a lot of investigation to find out that
the xml package was stripping the dashes.

What is the expected output?

expected TestBug to pass.  The xml tag in RespBug has dashes in it, just like the xml it
is parsing, thus it would make sense that the fieldPath should work with dashes in it.

What do you see instead?

TestBug fails yet TestWorks passes.

Which compiler are you using (5g, 6g, 8g, gccgo)?

6g, 8g

Which operating system are you using?

mac and linux
Which revision are you using?  (hg identify)


Please provide any additional information below.
@gopherbot

This comment has been minimized.

Copy link
Author

gopherbot commented Oct 28, 2011

Comment 1 by pcrosby:

Sorry, I left out my go revision:
c1702f36df03+ (release-branch.r60) release/release.r60.3
@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented Oct 31, 2011

Comment 2:

This is part of a bigger change: we need to fix the xml tags
to match the ones used by package json.

Status changed to Accepted.

@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented Dec 9, 2011

Comment 3:

Labels changed: added priority-later.

@rsc

This comment has been minimized.

Copy link
Contributor

rsc commented Dec 12, 2011

Comment 4:

Labels changed: added priority-go1.

@niemeyer

This comment has been minimized.

Copy link
Contributor

niemeyer commented Dec 29, 2011

Comment 5:

Owner changed to @niemeyer.

Status changed to Started.

@niemeyer

This comment has been minimized.

Copy link
Contributor

niemeyer commented Jan 13, 2012

Comment 6:

This issue was closed by revision 1627b46.

Status changed to Fixed.

@mikioh mikioh changed the title xml.Unmarshal nested field tags only work with dashes removed encoding/xml: Unmarshal nested field tags only work with dashes removed Jan 9, 2015

@rsc rsc added this to the Go1 milestone Apr 10, 2015

@rsc rsc removed the priority-go1 label Apr 10, 2015

@golang golang locked and limited conversation to collaborators Jun 24, 2016

This issue was closed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.