From 85e12cea93568601a4875a4b26bf3514ecc38dfe Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sat, 23 Oct 2021 02:45:14 +0200 Subject: [PATCH] Only parse each nested element name once A child elements should only appear once or it is part of a nested array (photos, poll answers). So each element name only needs to be parsed once, because the way `parse_array_from_node` works is, that it already parses the full array with one call, so calling it multiple times again parses the full array a second time. closes #118 --- lib/diaspora_federation/parsers/xml_parser.rb | 2 +- spec/lib/diaspora_federation/parsers/xml_parser_spec.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/diaspora_federation/parsers/xml_parser.rb b/lib/diaspora_federation/parsers/xml_parser.rb index 2e04418..d8295dc 100644 --- a/lib/diaspora_federation/parsers/xml_parser.rb +++ b/lib/diaspora_federation/parsers/xml_parser.rb @@ -12,7 +12,7 @@ module DiasporaFederation def parse(root_node) from_xml_sanity_validation(root_node) - hash = root_node.element_children.map {|child| + hash = root_node.element_children.uniq(&:name).map {|child| xml_name = child.name property = entity_type.find_property_for_xml_name(xml_name) if property diff --git a/spec/lib/diaspora_federation/parsers/xml_parser_spec.rb b/spec/lib/diaspora_federation/parsers/xml_parser_spec.rb index fdcf4b6..9183314 100644 --- a/spec/lib/diaspora_federation/parsers/xml_parser_spec.rb +++ b/spec/lib/diaspora_federation/parsers/xml_parser_spec.rb @@ -149,6 +149,14 @@ XML expect(parsed[0][:multi].first.to_h).to eq(child_entity2.to_h) expect(parsed[0][:asdf]).to eq("QWERT") end + + it "parses array entities only once" do + expect(Entities::OtherEntity).to receive(:from_xml).twice.and_call_original + + parsed = Parsers::XmlParser.new(Entities::TestNestedEntity).parse(nested_payload) + + expect(parsed[0][:multi]).to have(2).items + end end it "doesn't drop extra properties" do