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
13 changes: 3 additions & 10 deletions lib/jsonapi/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,9 @@ def check_sort_criteria(resource_klass, sort_criteria)
end

def parse_add_operation(data)
if data.is_a?(Array)
data.each do |p|
@operations.push JSONAPI::CreateResourceOperation.new(@resource_klass,
parse_params(verify_and_remove_type(p),
@resource_klass.createable_fields(@context)))
end
else
@operations.push JSONAPI::CreateResourceOperation.new(@resource_klass,
parse_params(verify_and_remove_type(data),
@resource_klass.createable_fields(@context)))
Array.wrap(data).each do |p|
values = parse_params(verify_and_remove_type(p), @resource_klass.createable_fields(@context))
@operations.push JSONAPI::CreateResourceOperation.new(@resource_klass, values)
end
rescue JSONAPI::Exceptions::Error => e
@errors.concat(e.errors)
Expand Down
2 changes: 1 addition & 1 deletion lib/jsonapi/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def change(callback)
@changing = true
run_callbacks callback do
yield
save if @save_needed
save if @save_needed || is_new?
end
end
end
Expand Down
37 changes: 36 additions & 1 deletion test/integration/requests/request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,41 @@ def test_post_single
assert_equal 201, status
end

def test_post_single_missing_data_contents
post '/posts',
{
'data' => {
}
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE

assert_equal 400, status
end

def test_post_single_minimal_valid
post '/comments',
{
'data' => {
'type' => 'comments'
}
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE

assert_equal 201, status
assert_nil json_response['data']['body']
assert_nil json_response['data']['links']['post']['linkage']
assert_nil json_response['data']['links']['author']['linkage']
end

def test_post_single_minimal_invalid
post '/posts',
{
'data' => {
'type' => 'posts'
}
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE

assert_equal 422, status
end

def test_update_association_without_content_type
ruby = Section.find_by(name: 'ruby')
patch '/posts/3/links/section', { 'data' => {type: 'sections', id: ruby.id.to_s }}.to_json
Expand Down Expand Up @@ -189,7 +224,7 @@ def test_patch_update_association_has_many_acts_as_set
def test_post_update_association_has_many
rogue = Comment.find_by(body: 'Rogue Comment Here')
post '/posts/5/links/comments', { 'data' => [{type: 'comments', id: rogue.id.to_s }]}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE

assert_equal 204, status
end

Expand Down