Skip to content

Commit 140266f

Browse files
xurigopherbot
authored andcommitted
encoding/xml: overriding by empty namespace when no new name declaration
The unmarshal and marshal XML text should be consistent if not modified deserialize variable. Fixes #61881 Change-Id: I475f7b05211b618685597d3ff20b97e3bbeaf8f8 GitHub-Last-Rev: 6831c77 GitHub-Pull-Request: #58401 Reviewed-on: https://go-review.googlesource.com/c/go/+/466295 Reviewed-by: ri xu <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Russ Cox <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent dffc47e commit 140266f

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/encoding/xml/marshal.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,9 @@ func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo, startTemplat
543543
}
544544
}
545545

546-
// If a name was found, namespace is overridden with an empty space
546+
// If a empty name was found, namespace is overridden with an empty space
547547
if tinfo.xmlname != nil && start.Name.Space == "" &&
548+
tinfo.xmlname.xmlns == "" && tinfo.xmlname.name == "" &&
548549
len(p.tags) != 0 && p.tags[len(p.tags)-1].Space != "" {
549550
start.Attr = append(start.Attr, Attr{Name{"", xmlnsPrefix}, ""})
550551
}

src/encoding/xml/xml_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1064,14 +1064,19 @@ func TestIssue7113(t *testing.T) {
10641064
XMLName Name `xml:""` // Sets empty namespace
10651065
}
10661066

1067+
type D struct {
1068+
XMLName Name `xml:"d"`
1069+
}
1070+
10671071
type A struct {
10681072
XMLName Name `xml:""`
10691073
C C `xml:""`
1074+
D D
10701075
}
10711076

10721077
var a A
10731078
structSpace := "b"
1074-
xmlTest := `<A xmlns="` + structSpace + `"><C xmlns=""></C></A>`
1079+
xmlTest := `<A xmlns="` + structSpace + `"><C xmlns=""></C><d></d></A>`
10751080
t.Log(xmlTest)
10761081
err := Unmarshal([]byte(xmlTest), &a)
10771082
if err != nil {

0 commit comments

Comments
 (0)