aboutsummaryrefslogtreecommitdiff
path: root/src/encoding
diff options
context:
space:
mode:
authorConstantin Konstantinidis <constantinkonstantinidis@gmail.com>2022-07-05 07:46:07 +0200
committerGopher Robot <gobot@golang.org>2023-01-31 18:23:50 +0000
commitdfd2ddd6717acb16b43ecde5150a7d8916ac5484 (patch)
tree3ebcd0a04d5bbf2fc0ebf3088822c45e21a08954 /src/encoding
parent1fc585dc2f2e5dd456031708602841e138240551 (diff)
downloadgo-dfd2ddd6717acb16b43ecde5150a7d8916ac5484.tar.xz
encoding/xml: allow overriding by empty namespace
The namespace defined by xmlns="value" can be overridden in every included tag by the empty namespace xmlns="" without a prefix. Method to calculate indent of XML handles depth of tag and its associated namespace is still active even when no indent is required. An XMLName field in a struct means that namespace must be enforced even if empty. This occurs only on an inner tag as an override of any non-empty namespace of its outer tag. An attribute is added to have the required namespace display. Fixes #7113 Change-Id: I57f2308e98c66f04108ab136d350bdc3a6091e98 Reviewed-on: https://go-review.googlesource.com/c/go/+/108796 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/encoding')
-rw-r--r--src/encoding/xml/marshal.go5
-rw-r--r--src/encoding/xml/xml_test.go50
2 files changed, 55 insertions, 0 deletions
diff --git a/src/encoding/xml/marshal.go b/src/encoding/xml/marshal.go
index 07b6042da8..0c3cc0dc36 100644
--- a/src/encoding/xml/marshal.go
+++ b/src/encoding/xml/marshal.go
@@ -543,6 +543,11 @@ func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo, startTemplat
}
}
+ // If a name was found, namespace is overridden with an empty space
+ if tinfo.xmlname != nil && start.Name.Space == "" &&
+ len(p.tags) != 0 && p.tags[len(p.tags)-1].Space != "" {
+ start.Attr = append(start.Attr, Attr{Name{"", xmlnsPrefix}, ""})
+ }
if err := p.writeStart(&start); err != nil {
return err
}
diff --git a/src/encoding/xml/xml_test.go b/src/encoding/xml/xml_test.go
index 30fb94da6d..8205ac3148 100644
--- a/src/encoding/xml/xml_test.go
+++ b/src/encoding/xml/xml_test.go
@@ -1059,6 +1059,56 @@ func TestIssue12417(t *testing.T) {
}
}
+func TestIssue7113(t *testing.T) {
+ type C struct {
+ XMLName Name `xml:""` // Sets empty namespace
+ }
+
+ type A struct {
+ XMLName Name `xml:""`
+ C C `xml:""`
+ }
+
+ var a A
+ structSpace := "b"
+ xmlTest := `<A xmlns="` + structSpace + `"><C xmlns=""></C></A>`
+ t.Log(xmlTest)
+ err := Unmarshal([]byte(xmlTest), &a)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if a.XMLName.Space != structSpace {
+ t.Errorf("overidding with empty namespace: unmarshalling, got %s, want %s\n", a.XMLName.Space, structSpace)
+ }
+ if len(a.C.XMLName.Space) != 0 {
+ t.Fatalf("overidding with empty namespace: unmarshalling, got %s, want empty\n", a.C.XMLName.Space)
+ }
+
+ var b []byte
+ b, err = Marshal(&a)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(a.C.XMLName.Space) != 0 {
+ t.Errorf("overidding with empty namespace: marshaling, got %s in C tag which should be empty\n", a.C.XMLName.Space)
+ }
+ if string(b) != xmlTest {
+ t.Fatalf("overidding with empty namespace: marshalling, got %s, want %s\n", b, xmlTest)
+ }
+ var c A
+ err = Unmarshal(b, &c)
+ if err != nil {
+ t.Fatalf("second Unmarshal failed: %s", err)
+ }
+ if c.XMLName.Space != "b" {
+ t.Errorf("overidding with empty namespace: after marshaling & unmarshaling, XML name space: got %s, want %s\n", a.XMLName.Space, structSpace)
+ }
+ if len(c.C.XMLName.Space) != 0 {
+ t.Errorf("overidding with empty namespace: after marshaling & unmarshaling, got %s, want empty\n", a.C.XMLName.Space)
+ }
+}
+
func TestIssue20396(t *testing.T) {
var attrError = UnmarshalError("XML syntax error on line 1: expected attribute name in element")