lib/dato/json_api_serializer.rb in dato-0.7.14 vs lib/dato/json_api_serializer.rb in dato-0.7.15
- old
+ new
@@ -21,12 +21,14 @@
end
data[:type] = type
data[:attributes] = serialized_attributes(resource)
- if relationships.any?
- data[:relationships] = serialized_relationships(resource)
+ serialized_relationships = serialized_relationships(resource)
+
+ if serialized_relationships
+ data[:relationships] = serialized_relationships
end
{ data: data }
end
@@ -80,11 +82,11 @@
elsif required_relationships.include?(relationship)
throw "Required attribute: #{relationship}"
end
end
- result
+ result.empty? ? nil : result
end
def attributes(resource)
if type == 'item'
return resource.keys.map(&:to_sym) - %i[
@@ -108,10 +110,14 @@
def relationships
@relationships ||= JsonSchemaRelationships.new(link.schema).relationships
end
def required_relationships
- (link_relationships.required || []).map(&:to_sym)
+ if link.schema.properties['data'].required.include?("relationships")
+ (link_relationships.required || []).map(&:to_sym)
+ else
+ []
+ end
end
def link_attributes
link.schema.properties['data'].properties['attributes']
end