Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/xml: allow mismatched XMLName / field metadata tag names #48450

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/encoding/xml/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2490,3 +2490,34 @@ func TestInvalidXMLName(t *testing.T) {
t.Errorf("error %q does not contain %q", err, want)
}
}

func TestMismatchedTagNames(t *testing.T) {
type InnerStruct struct {
XMLName Name `xml:"innerStruct"`
Name string `xml:"name"`
}

type Outer struct {
XMLName Name `xml:"outer"`
Inner *InnerStruct `xml:"inner"`
}

dec := NewDecoder(strings.NewReader(`<?xml version="1.0"?>
<outer>
<inner>
<name>foo</name>
</inner>
</outer>"`))

holder := &Outer{}
if err := dec.Decode(holder); err != nil {
t.Errorf("Unexpected error: %q", err)
}
if holder == nil {
t.Error("Holder should be non-nil")
} else if holder.Inner == nil {
t.Error("Inner should be non-nil")
} else if holder.Inner.Name != "foo" {
t.Errorf("Expected name to be %q, but was %q", "foo", holder.Inner.Name)
}
}
3 changes: 0 additions & 3 deletions src/encoding/xml/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,6 @@ func (d *Decoder) unmarshal(val reflect.Value, start *StartElement) error {
// Validate and assign element name.
if tinfo.xmlname != nil {
finfo := tinfo.xmlname
if finfo.name != "" && finfo.name != start.Name.Local {
return UnmarshalError("expected element type <" + finfo.name + "> but have <" + start.Name.Local + ">")
}
if finfo.xmlns != "" && finfo.xmlns != start.Name.Space {
e := "expected element <" + finfo.name + "> in name space " + finfo.xmlns + " but have "
if start.Name.Space == "" {
Expand Down
11 changes: 0 additions & 11 deletions src/encoding/xml/typeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,6 @@ func structFieldInfo(typ reflect.Type, f *reflect.StructField) (*fieldInfo, erro
finfo.parents = parents[:len(parents)-1]
}

// If the field type has an XMLName field, the names must match
// so that the behavior of both marshaling and unmarshaling
// is straightforward and unambiguous.
if finfo.flags&fElement != 0 {
ftyp := f.Type
xmlname := lookupXMLName(ftyp)
if xmlname != nil && xmlname.name != finfo.name {
return nil, fmt.Errorf("xml: name %q in tag of %s.%s conflicts with name %q in %s.XMLName",
finfo.name, typ, f.Name, xmlname.name, ftyp)
}
}
return finfo, nil
}

Expand Down