Only add optional properties to generated XML and JSON when not nil

This commit is contained in:
Benjamin Neff 2017-01-07 18:14:34 +01:00
parent af7b6485ac
commit f5ff4a71e6
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
9 changed files with 46 additions and 9 deletions

View file

@ -270,6 +270,8 @@ module DiasporaFederation
end
def normalize_property(name, value)
return nil if optional_nil_value?(name, value)
case self.class.class_props[name]
when :string
value.to_s
@ -315,8 +317,9 @@ module DiasporaFederation
def json_data
enriched_properties.map {|key, value|
type = self.class.class_props[key]
next if optional_nil_value?(key, value)
if !value.nil? && type.instance_of?(Class) && value.respond_to?(:to_json)
if !value.nil? && type.instance_of?(Class)
entity_data = value.to_json
[key, entity_data] unless entity_data.nil?
elsif type.instance_of?(Array)
@ -328,6 +331,10 @@ module DiasporaFederation
}.compact.to_h
end
def optional_nil_value?(name, value)
value.nil? && self.class.optional_props.include?(name)
end
# Raised, if entity is not valid
class ValidationError < RuntimeError
end

View file

@ -55,7 +55,7 @@ module DiasporaFederation
Fabricator(:profile_entity, class_name: DiasporaFederation::Entities::Profile) do
author { Fabricate.sequence(:diaspora_id) }
first_name "my name"
last_name ""
last_name nil
image_url "/assets/user/default.png"
image_url_medium "/assets/user/default.png"
image_url_small "/assets/user/default.png"

View file

@ -11,6 +11,11 @@ module DiasporaFederation
property :test4, :boolean, default: -> { true }
end
class TestOptionalEntity < DiasporaFederation::Entity
property :test1, :string, optional: true
property :test2, :string
end
class OtherEntity < DiasporaFederation::Entity
property :asdf, :string
end
@ -54,6 +59,7 @@ module DiasporaFederation
property :test4, :integer
property :test5, :timestamp
entity :test6, TestEntity
property :test7, :string, optional: true
entity :multi, [OtherEntity]
end

View file

@ -21,7 +21,6 @@ module DiasporaFederation
<profile>
<author>#{data[:profile].author}</author>
<first_name>#{data[:profile].first_name}</first_name>
<last_name/>
<image_url>#{data[:profile].image_url}</image_url>
<image_url_medium>#{data[:profile].image_url}</image_url_medium>
<image_url_small>#{data[:profile].image_url}</image_url_small>

View file

@ -10,7 +10,6 @@ module DiasporaFederation
<profile>
<author>#{data[:profile].author}</author>
<first_name>#{data[:profile].first_name}</first_name>
<last_name/>
<image_url>#{data[:profile].image_url}</image_url>
<image_url_medium>#{data[:profile].image_url}</image_url_medium>
<image_url_small>#{data[:profile].image_url}</image_url_small>

View file

@ -6,7 +6,6 @@ module DiasporaFederation
<profile>
<author>#{data[:author]}</author>
<first_name>#{data[:first_name]}</first_name>
<last_name/>
<image_url>#{data[:image_url]}</image_url>
<image_url_medium>#{data[:image_url]}</image_url_medium>
<image_url_small>#{data[:image_url]}</image_url_small>
@ -27,7 +26,6 @@ XML
"entity_data": {
"author": "#{data[:author]}",
"first_name": "#{data[:first_name]}",
"last_name": "",
"image_url": "#{data[:image_url]}",
"image_url_medium": "#{data[:image_url]}",
"image_url_small": "#{data[:image_url]}",

View file

@ -103,8 +103,6 @@ XML
"lng": "#{location.lng}"
}
},
"poll": null,
"event": null,
"public": #{data[:public]}
}
}

View file

@ -142,6 +142,35 @@ module DiasporaFederation
end
end
context "optional properties" do
it "contains nodes for optional properties when not nil" do
entity = Entities::TestOptionalEntity.new(test1: "aa", test2: "bb")
xml_children = entity.to_xml.children
expect(xml_children).to have_exactly(2).items
xml_children.each do |node|
expect(%w[test1 test2]).to include(node.name)
end
end
it "contains no nodes for optional nil properties" do
entity = Entities::TestOptionalEntity.new(test2: "bb")
xml_children = entity.to_xml.children
expect(xml_children).to have_exactly(1).items
xml_children.each do |node|
expect(%w[test2]).to include(node.name)
end
end
it "contains nodes for non optional properties when nil" do
entity = Entities::TestOptionalEntity.new(test1: "aa", test2: nil)
xml_children = entity.to_xml.children
expect(xml_children).to have_exactly(2).items
xml_children.each do |node|
expect(%w[test1 test2]).to include(node.name)
end
end
end
it "replaces invalid XML characters" do
entity = Entities::TestEntity.new(test: "asdfasdf asdf💩asdf\nasdf")
xml = entity.to_xml.to_xml

View file

@ -139,7 +139,8 @@ shared_examples "a JSON Entity" do
entity_data.delete(:parent)
nested_elements, simple_props = entity_data.partition {|_key, value| value.is_a?(Array) || value.is_a?(Hash) }
expect(to_json_output).to include_json(entity_data: simple_props.to_h)
expect(to_json_output).to include_json(entity_data: simple_props.reject {|_key, value| value.nil? }.to_h)
nested_elements.each {|key, value|
type = described_class.class_props[key]
if value.is_a?(Array)