Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ def attributes
attributes_map.each do |attribute_name, attr_data|
next if !should_include_attr?(attribute_name, attr_data)
value = evaluate_attr_or_block(attribute_name, attr_data[:attr_or_block])
# Only include the attribute if it's not nil or if the key exists in the object
# This prevents setting all fields to null for included records
if value.nil?
# Check if the key actually exists in the object (for Hash objects)
# If the key exists in the object, it means that the value is purposely set to null
next unless @object.is_a?(Hash) && @object.key?(attribute_name.to_s)
end
attributes[format_name(attribute_name)] = value
end
attributes
Expand Down Expand Up @@ -138,9 +145,9 @@ def relationships
end

object = has_one_relationship(attribute_name, attr_data)
if object.nil? || object.empty?
data[formatted_attribute_name]['data'] = nil
else
# Only include 'data' key if the relationship object exists
# Omit 'data' key entirely when null instead of setting it to null
unless object.nil? || (object.respond_to?(:empty?) && object.empty?)
relation = datasource.get_collection(@options[:class_name].gsub('::', '__'))
.schema[:fields][attribute_name.to_s]
options = @options.clone
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,60 @@ def respond_to?(arg)
}
)
end

it 'includes null attributes in the returned result' do
attributes = {
'title' => 'Harry potter and the goblet of fire',
'published_at' => nil,
'price' => nil
}
book = { 'id' => 1 }.merge(attributes)

datasource = Datasource.new
collection = build_collection(
name: 'book',
schema: {
fields: {
'id' => ColumnSchema.new(
column_type: 'Number',
is_primary_key: true,
filter_operators: [Operators::IN, Operators::EQUAL]
),
'title' => ColumnSchema.new(column_type: 'String'),
'published_at' => ColumnSchema.new(column_type: 'Date'),
'price' => ColumnSchema.new(column_type: 'Number')
}
},
create: book,
list: [book]
)

datasource.add_collection(collection)
ForestAdminAgent::Builder::AgentFactory.instance.add_datasource(datasource)
ForestAdminAgent::Builder::AgentFactory.instance.build
args[:params][:data] = { attributes: attributes, type: 'books' }

result = store.handle_request(args)
expect(collection).to have_received(:create) do |caller, data|
expect(caller).to be_instance_of(Components::Caller)
expect(data).to eq(attributes)
end
expect(result[:name]).to eq('book')
expect(result[:content]).to eq(
'data' =>
{
'type' => 'book',
'id' => '1',
'attributes' => {
'id' => 1,
'title' => 'Harry potter and the goblet of fire',
'published_at' => nil,
'price' => nil
},
'links' => { 'self' => '/forest/book/1' }
}
)
end
end

describe 'with relation' do
Expand Down Expand Up @@ -184,7 +238,6 @@ def respond_to?(arg)
'links' => { 'self' => '/forest/person/1' },
'relationships' => {
'passport' => {
'data' => nil,
'links' => { 'related' => { 'href' => '/forest/person/1/relationships/passport' } }
}
}
Expand Down Expand Up @@ -221,7 +274,6 @@ def respond_to?(arg)
'links' => { 'self' => '/forest/passport/1' },
'relationships' => {
'person' => {
'data' => nil,
'links' => {
'related' => {
'href' => '/forest/passport/1/relationships/person'
Expand Down