diff --git a/lib/jsonapi/request.rb b/lib/jsonapi/request.rb index a09f35275..8fda570fa 100644 --- a/lib/jsonapi/request.rb +++ b/lib/jsonapi/request.rb @@ -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) diff --git a/lib/jsonapi/resource.rb b/lib/jsonapi/resource.rb index eca620854..e1cb7212b 100644 --- a/lib/jsonapi/resource.rb +++ b/lib/jsonapi/resource.rb @@ -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 diff --git a/test/integration/requests/request_test.rb b/test/integration/requests/request_test.rb index 4050a7724..14a17ba92 100644 --- a/test/integration/requests/request_test.rb +++ b/test/integration/requests/request_test.rb @@ -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 @@ -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