From 26b7991defe1d84d10c1186a151676076946b26f Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sun, 3 Jul 2016 22:52:12 +0200 Subject: [PATCH] handle empty xml-elements for nested entities --- lib/diaspora_federation/entity.rb | 4 ++-- spec/entities.rb | 2 +- spec/lib/diaspora_federation/entity_spec.rb | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/diaspora_federation/entity.rb b/lib/diaspora_federation/entity.rb index 77a936a..8743c77 100644 --- a/lib/diaspora_federation/entity.rb +++ b/lib/diaspora_federation/entity.rb @@ -286,7 +286,7 @@ module DiasporaFederation # @return [Entity] parsed child entity private_class_method def self.parse_entity_from_node(type, root_node) node = root_node.xpath(type.entity_name) - type.from_xml(node.first) if node.any? + type.from_xml(node.first) if node.any? && node.first.children.any? end # Collect all nested children of that type and create an array in the data hash @@ -296,7 +296,7 @@ module DiasporaFederation # @return [Array] array with parsed child entities private_class_method def self.parse_array_from_node(type, root_node) node = root_node.xpath(type.entity_name) - node.map {|child| type.from_xml(child) } unless node.empty? + node.select {|child| child.children.any? }.map {|child| type.from_xml(child) } unless node.empty? end # Raised, if entity is not valid diff --git a/spec/entities.rb b/spec/entities.rb index 5d0aef0..5747df1 100644 --- a/spec/entities.rb +++ b/spec/entities.rb @@ -17,7 +17,7 @@ module DiasporaFederation class TestNestedEntity < DiasporaFederation::Entity property :asdf - entity :test, TestEntity + entity :test, TestEntity, default: nil entity :multi, [OtherEntity] end diff --git a/spec/lib/diaspora_federation/entity_spec.rb b/spec/lib/diaspora_federation/entity_spec.rb index 899f99d..99bb476 100644 --- a/spec/lib/diaspora_federation/entity_spec.rb +++ b/spec/lib/diaspora_federation/entity_spec.rb @@ -300,6 +300,22 @@ XML expect(entity.multi.first).to be_instance_of(Entities::OtherEntity) expect(entity.multi.first.asdf).to eq("asdf") end + + it "handles empty xml-element for nested entities" do + xml = <<-XML + + FDSA + + + +XML + + entity = Entities::TestNestedEntity.from_xml(Nokogiri::XML::Document.parse(xml).root) + + expect(entity.asdf).to eq("FDSA") + expect(entity.test).to be_nil + expect(entity.multi).to be_empty + end end context "xml_name" do