handle empty xml-elements for nested entities

This commit is contained in:
Benjamin Neff 2016-07-03 22:52:12 +02:00
parent dd19526c5e
commit 26b7991def
3 changed files with 19 additions and 3 deletions

View file

@ -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<Entity>] 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

View file

@ -17,7 +17,7 @@ module DiasporaFederation
class TestNestedEntity < DiasporaFederation::Entity
property :asdf
entity :test, TestEntity
entity :test, TestEntity, default: nil
entity :multi, [OtherEntity]
end

View file

@ -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
<test_nested_entity>
<asdf>FDSA</asdf>
<test_entity/>
<other_entity/>
</test_nested_entity>
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