Permalink
Browse files

FIX: Optional Element/Namespace not present

  This test and fix consider the possibility that an optional element, with a
  unique namespace is not present.  When the namespace is not defined because
  the element is not present the parser should not fail.
  • Loading branch information...
1 parent fd1e898 commit 9614221a80ff3bda18ff859aa751dff29cf52fd3 @burtlo committed Jan 24, 2011
Showing with 42 additions and 1 deletion.
  1. +4 −0 lib/happymapper.rb
  2. +38 −1 spec/happymapper_spec.rb
View
4 lib/happymapper.rb
@@ -125,7 +125,11 @@ def parse(xml, options = {})
[options[:tag], options[:name], tag_name].compact.each do |xpath_ext|
+ begin
nodes = node.xpath(xpath + xpath_ext.to_s, namespaces)
+ rescue
+ break
+ end
break if nodes && !nodes.empty?
end
View
39 spec/happymapper_spec.rb
@@ -454,6 +454,20 @@ class Article
end
+class PartiallyBadArticle
+ include HappyMapper
+
+ tag 'Article'
+ namespace 'article'
+
+ element :title, String
+ element :text, String
+ has_many :photos, 'Photo', :tag => 'Photo', :namespace => 'photo'
+ has_many :videos, 'Video', :tag => 'Video', :namespace => 'video'
+
+ element :publish_options, PublishOptions, :tag => 'publishOptions', :namespace => 'article'
+
+end
class Photo
include HappyMapper
@@ -466,6 +480,18 @@ class Photo
end
+class Video
+ include HappyMapper
+
+ tag 'Video'
+ namespace 'video'
+
+ element :title, String
+ element :publish_options, PublishOptions, :tag => 'publishOptions', :namespace => 'video'
+
+end
+
+
describe HappyMapper do
describe "being included into another class" do
@@ -858,6 +884,17 @@ class Thing
@article = Article.parse(fixture_file('subclass_namespace.xml'))
end
- end
+ end
+
+ context "Namespace is missing because an optional element that uses it is not present" do
+ it "should parse successfully" do
+ @article = PartiallyBadArticle.parse(fixture_file('subclass_namespace.xml'))
+ @article.should_not be_nil
+ @article.title.should_not be_nil
+ @article.text.should_not be_nil
+ @article.photos.should_not be_nil
+ @article.photos.first.title.should_not be_nil
+ end
+ end
end

0 comments on commit 9614221

Please sign in to comment.