Skip to content

Commit

Permalink
[release-branch.go1.21] encoding/xml: overriding by empty namespace w…
Browse files Browse the repository at this point in the history
…hen 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: 6831c77
GitHub-Pull-Request: #58401
Reviewed-on: https://go-review.googlesource.com/c/go/+/522316
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Commit-Queue: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
  • Loading branch information
xuri authored and gopherbot committed Aug 30, 2023
1 parent 745b81b commit 2d07bb8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/encoding/xml/marshal.go
Expand Up @@ -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}, ""})
}
Expand Down
7 changes: 6 additions & 1 deletion src/encoding/xml/xml_test.go
Expand Up @@ -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 := `<A xmlns="` + structSpace + `"><C xmlns=""></C></A>`
xmlTest := `<A xmlns="` + structSpace + `"><C xmlns=""></C><d></d></A>`
t.Log(xmlTest)
err := Unmarshal([]byte(xmlTest), &a)
if err != nil {
Expand Down

0 comments on commit 2d07bb8

Please sign in to comment.