From: xuri Date: Wed, 9 Aug 2023 06:32:12 +0000 (+0000) Subject: [release-branch.go1.21] encoding/xml: overriding by empty namespace when no new name... X-Git-Tag: go1.21.1~11 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=2d07bb86f0a2bab639c51baca4c3cf6d9c4374ad;p=gostls13.git [release-branch.go1.21] 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. For #61881 Fixes #62051 Change-Id: I475f7b05211b618685597d3ff20b97e3bbeaf8f8 GitHub-Last-Rev: 6831c770c384831798cb1c6dc4674e5d4caa5e3c GitHub-Pull-Request: golang/go#58401 Reviewed-on: https://go-review.googlesource.com/c/go/+/522316 Run-TryBot: Ian Lance Taylor Reviewed-by: Russ Cox Auto-Submit: Ian Lance Taylor Commit-Queue: Ian Lance Taylor TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor --- diff --git a/src/encoding/xml/marshal.go b/src/encoding/xml/marshal.go index 0c3cc0dc36..ae39846f5b 100644 --- a/src/encoding/xml/marshal.go +++ b/src/encoding/xml/marshal.go @@ -543,8 +543,9 @@ func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo, startTemplat } } - // If a name was found, namespace is overridden with an empty space + // If a empty name was found, namespace is overridden with an empty space if tinfo.xmlname != nil && start.Name.Space == "" && + tinfo.xmlname.xmlns == "" && tinfo.xmlname.name == "" && len(p.tags) != 0 && p.tags[len(p.tags)-1].Space != "" { start.Attr = append(start.Attr, Attr{Name{"", xmlnsPrefix}, ""}) } diff --git a/src/encoding/xml/xml_test.go b/src/encoding/xml/xml_test.go index f5c7259cfb..42f5f5f8a6 100644 --- a/src/encoding/xml/xml_test.go +++ b/src/encoding/xml/xml_test.go @@ -1064,14 +1064,19 @@ func TestIssue7113(t *testing.T) { XMLName Name `xml:""` // Sets empty namespace } + type D struct { + XMLName Name `xml:"d"` + } + type A struct { XMLName Name `xml:""` C C `xml:""` + D D } var a A structSpace := "b" - xmlTest := `` + xmlTest := `` t.Log(xmlTest) err := Unmarshal([]byte(xmlTest), &a) if err != nil {