-
Notifications
You must be signed in to change notification settings - Fork 5
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
Fix an issue with namespaces when an element is unmarshalled #32
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems, there's a bit simpler solution.
for space, uri := range namespaces {
prefix := "xmlns:" + space
if space == "" {
prefix = "xmlns"
}
doc.Root().CreateAttr(prefix, uri)
}
I was confused by NamespaceURI()
function which says that
If the element is part of the XML default namespace, NamespaceURI returns the empty string.
But it actually returns a default namespace. So we can skip the part inside for _, childEl := range el.FindElements("//*") {
and just use "xmlns" key for default namespace.
Source of the NamespaceURI()
. Here's a check for Space
property and if it's empty that means it's a part of default namespace.
// NamespaceURI returns the XML namespace URI associated with the element. If
// the element is part of the XML default namespace, NamespaceURI returns the
// empty string.
func (e *Element) NamespaceURI() string {
if e.Space == "" {
return e.findDefaultNamespaceURI()
}
return e.findLocalNamespaceURI(e.Space)
}
That would solve the issue with default namespace not being correctly added to attributes, but it doesn't solve the problem with the namespaces not being picked up correctly. If we keep the
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, sounds good! Could you maybe add some comments to the namespace searching section so that will be easier to navigate and read the code in the future?
if ns != "" { | ||
namespaces[childEl.Space] = ns | ||
currentElement := el | ||
for currentElement != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a new test case for testing this functionality (elements from the default namespace)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a test for it 👍
Thanks!!! This is great! |
What
Change the logic of namespace retrieval for element unmarshalling. The new logic:
Also correctly apply default namespaces (previously they would have been added like
xmlns:=value
, now they are added asxmlns=value
).Why
A valid XML lead to the following error:
expected element <Assertion> in name space urn:oasis:names:tc:SAML:2.0:assertion but have no name space
XML: