]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/xml: check for exported fields in embedded structs
authorMarcel van Lohuizen <mpvl@golang.org>
Fri, 28 Aug 2015 08:21:45 +0000 (10:21 +0200)
committerMarcel van Lohuizen <mpvl@golang.org>
Mon, 26 Oct 2015 11:26:11 +0000 (11:26 +0000)
Addresses issue #12367.

Must be checked in before CL 14010.

Change-Id: I4523a1de112ed02371504e27882659bce8028a9f
Reviewed-on: https://go-review.googlesource.com/14012
Reviewed-by: Russ Cox <rsc@golang.org>
src/encoding/xml/marshal_test.go
src/encoding/xml/typeinfo.go

index 330fbee1afe2b488a65291c16067f62331231efe..aab94b16f3597760244ccc512b40718e7aa17f71 100644 (file)
@@ -139,6 +139,7 @@ type EmbedA struct {
        EmbedC
        EmbedB EmbedB
        FieldA string
+       embedD
 }
 
 type EmbedB struct {
@@ -153,6 +154,11 @@ type EmbedC struct {
        FieldC  string
 }
 
+type embedD struct {
+       fieldD string
+       FieldE string // Promoted and visible when embedD is embedded.
+}
+
 type NameCasing struct {
        XMLName struct{} `xml:"casing"`
        Xy      string
@@ -711,6 +717,9 @@ var marshalTests = []struct {
                                },
                        },
                        FieldA: "A.A",
+                       embedD: embedD{
+                               FieldE: "A.D.E",
+                       },
                },
                ExpectXML: `<EmbedA>` +
                        `<FieldB>A.C.B</FieldB>` +
@@ -724,6 +733,7 @@ var marshalTests = []struct {
                        `<FieldC>A.B.C.C</FieldC>` +
                        `</EmbedB>` +
                        `<FieldA>A.A</FieldA>` +
+                       `<FieldE>A.D.E</FieldE>` +
                        `</EmbedA>`,
        },
 
index 22248d20a6d669e348c93f7d4b5feb558eee8e6d..6766b88f09a194546589ca2f320dcc063d62ea0f 100644 (file)
@@ -60,7 +60,7 @@ func getTypeInfo(typ reflect.Type) (*typeInfo, error) {
                n := typ.NumField()
                for i := 0; i < n; i++ {
                        f := typ.Field(i)
-                       if f.PkgPath != "" || f.Tag.Get("xml") == "-" {
+                       if (f.PkgPath != "" && !f.Anonymous) || f.Tag.Get("xml") == "-" {
                                continue // Private field
                        }