Skip to content

Commit

Permalink
encoding/xml: check for exported fields in embedded structs
Browse files Browse the repository at this point in the history
Addresses issue golang#12367.

Must be checked in before CL 14010.

Change-Id: I4523a1de112ed02371504e27882659bce8028a9f
Reviewed-on: https://go-review.googlesource.com/14012
Reviewed-by: Russ Cox <[email protected]>
  • Loading branch information
mpvl committed Oct 26, 2015
1 parent a30dd9c commit 34f04a6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/encoding/xml/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type EmbedA struct {
EmbedC
EmbedB EmbedB
FieldA string
embedD
}

type EmbedB struct {
Expand All @@ -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
Expand Down Expand Up @@ -711,6 +717,9 @@ var marshalTests = []struct {
},
},
FieldA: "A.A",
embedD: embedD{
FieldE: "A.D.E",
},
},
ExpectXML: `<EmbedA>` +
`<FieldB>A.C.B</FieldB>` +
Expand All @@ -724,6 +733,7 @@ var marshalTests = []struct {
`<FieldC>A.B.C.C</FieldC>` +
`</EmbedB>` +
`<FieldA>A.A</FieldA>` +
`<FieldE>A.D.E</FieldE>` +
`</EmbedA>`,
},

Expand Down
2 changes: 1 addition & 1 deletion src/encoding/xml/typeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 34f04a6

Please sign in to comment.