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

Un-resolve xml attribute namespaces #255

Merged
merged 1 commit into from
Jan 13, 2021
Merged
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
32 changes: 31 additions & 1 deletion encoding/xml/xml_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,44 @@ func (d NodeDecoder) Token() (t xml.StartElement, done bool, err error) {
}

if t, ok := token.(xml.StartElement); ok {
return t, false, err
return restoreAttrNamespaces(t), false, err
}

// skip token if it is a comment or preamble or empty space value due to indentation
// or if it's a value and is not expected
}
}

// restoreAttrNamespaces update XML attributes to restore the short namespaces found within
// the raw XML document.
func restoreAttrNamespaces(node xml.StartElement) xml.StartElement {
if len(node.Attr) == 0 {
return node
}

// Generate a mapping of XML namespace values to their short names.
ns := map[string]string{}
for _, a := range node.Attr {
if a.Name.Space == "xmlns" {
ns[a.Value] = a.Name.Local
break
}
}

for i, a := range node.Attr {
if a.Name.Space == "xmlns" {
continue
}
// By default, xml.Decoder will fully resolve these namespaces. So if you had <foo xmlns:bar=baz bar:bin=hi/>
// then by default the second attribute would have the `Name.Space` resolved to `baz`. But we need it to
// continue to resolve as `bar` so we can easily identify it later on.
if v, ok := ns[node.Attr[i].Name.Space]; ok {
node.Attr[i].Name.Space = v
}
}
return node
}

// GetElement looks for the given tag name at the current level, and returns the element if found, and
// skipping over non-matching elements. Returns an error if the node is not found, or if an error occurs while walking
// the document.
Expand Down
24 changes: 24 additions & 0 deletions encoding/xml/xml_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ func TestXMLNodeDecoder_Token(t *testing.T) {
Attr: []xml.Attr{},
},
},
"attr with namespace": {
responseBody: bytes.NewReader([]byte(`<Response><Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser"></Grantee></Response>`)),
expectedStartElement: xml.StartElement{
Name: xml.Name{
Local: "Grantee",
},
Attr: []xml.Attr{
{
Name: xml.Name{
Space: "xmlns",
Local: "xsi",
},
Value: "http://www.w3.org/2001/XMLSchema-instance",
},
{
Name: xml.Name{
Space: "xsi",
Local: "type",
},
Value: "CanonicalUser",
},
},
},
},
}

for name, c := range cases {
Expand Down