Skip to content

Commit

Permalink
Fix issue with missing namespaces
Browse files Browse the repository at this point in the history
Newer versions of Nokogiri (>= 1.12.0) can use libxml >= 2.9.12 which breaks the
automatic addition of the parent namespace when adding a child.
This was already the case for JRuby, so added the same workaround
for when nokogiri is using this newer version of libxml.

Ref: sparklemotion/nokogiri#425
  • Loading branch information
jdongelmans committed Aug 12, 2021
1 parent 24b3881 commit 0b861de
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
1 change: 1 addition & 0 deletions .rspec
@@ -0,0 +1 @@
--format progress --color
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -16,7 +16,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'Saml'
rdoc.options << '--line-numbers'
rdoc.rdoc_files.include('README.rdoc')
rdoc.rdoc_files.include('README.md')
rdoc.rdoc_files.include('lib/**/*.rb')
end

Expand Down
12 changes: 7 additions & 5 deletions lib/xmlmapper.rb
Expand Up @@ -687,14 +687,16 @@ def to_xml(builder = nil, default_namespace = nil, namespace_override = nil,
# an empty element will be written to the xml
#
if value.nil? && element.options[:single] && element.options[:state_when_nil]
# NOTE In JRuby 9.0.4.0+ and Nokogiri version 1.6.8, Nokogiri::XML::Builder::NodeBuilder
# does not retain the XML namespace prefix for an element when adding an element
# to a parent node.
#
# NOTE
# In JRuby 9.0.4.0+ and Nokogiri version 1.6.8 or with Nokogiri version >= 1.12.0 (libxml >= 2.9.12),
# the Nokogiri::XML::Builder::NodeBuilder does not retain the XML namespace prefix for an element
# when adding an element to a parent node.
#
# The namespace prefix must be specified when adding the node to its parent.
# This issue manifests when setting an element's :state_when_nil' option to true.
#
# This JRuby workaround is intended for XML element prefixes that originate from a
# This workaround is intended for XML element prefixes that originate from a
# single namespace defined in 'registered_namespaces'. If there are
# multiple namespaces defined in the 'registered_namespaces' array,
# then the first namespace is selected.
Expand All @@ -707,7 +709,7 @@ def to_xml(builder = nil, default_namespace = nil, namespace_override = nil,
# 3. Adding namespace-less node to namespaced parent attaches the parent namespace to the child
# https://github.com/sparklemotion/nokogiri/issues/425
#
if RUBY_ENGINE == 'jruby' && !registered_namespaces.empty?
if (RUBY_ENGINE == 'jruby' || Nokogiri.uses_libxml?('>= 2.9.12')) && !registered_namespaces.empty?
ns = registered_namespaces.keys.first.to_sym
xml[ns].send("#{tag}_","")
else
Expand Down
2 changes: 1 addition & 1 deletion lib/xmlmapper/version.rb
@@ -1,3 +1,3 @@
module XmlMapper
VERSION = "0.7.3"
VERSION = '0.8.0'.freeze
end

0 comments on commit 0b861de

Please sign in to comment.