Skip to content

Commit f607b54

Browse files
authored
Merge pull request #251 from mvz/root-element-namespace
Fix namespace prefix rendering on root element when specified as symbol
2 parents 93cf0e5 + e33b67f commit f607b54

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

lib/happymapper/class_methods.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def after_parse(&block)
177177
# element.
178178
#
179179
def namespace(namespace = nil)
180-
@namespace = namespace if namespace
180+
@namespace = namespace.to_s if namespace
181181
@namespace if defined? @namespace
182182
end
183183

spec/features/to_xml_with_namespaces_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,36 @@ def initialize(parameters)
299299
XML
300300
end
301301
end
302+
303+
context "with namespace set on the root object using a symbol" do
304+
let(:klass) do
305+
Class.new do
306+
include HappyMapper
307+
308+
register_namespace "prefix", "http://www.unicornland.com/prefix"
309+
namespace :prefix
310+
tag :foo
311+
has_one :bar, String
312+
end
313+
end
314+
315+
it "renders xml that can be parsed by the same class" do
316+
obj = klass.new
317+
obj.bar = "foobar"
318+
copy = klass.parse(obj.to_xml)
319+
expect(copy.bar).to eq obj.bar
320+
end
321+
322+
it "renders the namespace prefix on all elements" do
323+
obj = klass.new
324+
obj.bar = "foobar"
325+
326+
expect(obj.to_xml).to eq <<~XML
327+
<?xml version="1.0"?>
328+
<prefix:foo xmlns:prefix="http://www.unicornland.com/prefix">
329+
<prefix:bar>foobar</prefix:bar>
330+
</prefix:foo>
331+
XML
332+
end
333+
end
302334
end

spec/happymapper/class_methods_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,16 @@
1414
expect { klass.tag "foo:bar" }.to raise_error HappyMapper::SyntaxError
1515
end
1616
end
17+
18+
describe "#namespace" do
19+
it "allows string values" do
20+
klass.namespace "foo"
21+
expect(klass.namespace).to eq "foo"
22+
end
23+
24+
it "converts symbol values to string" do
25+
klass.namespace :foo
26+
expect(klass.namespace).to eq "foo"
27+
end
28+
end
1729
end

0 commit comments

Comments
 (0)