Skip to content

Commit 5a6e5b4

Browse files
committed
Merge pull request #155 from cerebris/pr/153
Pr/153
2 parents a0e523a + 72b4495 commit 5a6e5b4

3 files changed

Lines changed: 40 additions & 12 deletions

File tree

lib/jsonapi/request.rb

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,9 @@ def check_sort_criteria(resource_klass, sort_criteria)
198198
end
199199

200200
def parse_add_operation(data)
201-
if data.is_a?(Array)
202-
data.each do |p|
203-
@operations.push JSONAPI::CreateResourceOperation.new(@resource_klass,
204-
parse_params(verify_and_remove_type(p),
205-
@resource_klass.createable_fields(@context)))
206-
end
207-
else
208-
@operations.push JSONAPI::CreateResourceOperation.new(@resource_klass,
209-
parse_params(verify_and_remove_type(data),
210-
@resource_klass.createable_fields(@context)))
201+
Array.wrap(data).each do |p|
202+
values = parse_params(verify_and_remove_type(p), @resource_klass.createable_fields(@context))
203+
@operations.push JSONAPI::CreateResourceOperation.new(@resource_klass, values)
211204
end
212205
rescue JSONAPI::Exceptions::Error => e
213206
@errors.concat(e.errors)

lib/jsonapi/resource.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def change(callback)
4646
@changing = true
4747
run_callbacks callback do
4848
yield
49-
save if @save_needed
49+
save if @save_needed || is_new?
5050
end
5151
end
5252
end

test/integration/requests/request_test.rb

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,41 @@ def test_post_single
156156
assert_equal 201, status
157157
end
158158

159+
def test_post_single_missing_data_contents
160+
post '/posts',
161+
{
162+
'data' => {
163+
}
164+
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
165+
166+
assert_equal 400, status
167+
end
168+
169+
def test_post_single_minimal_valid
170+
post '/comments',
171+
{
172+
'data' => {
173+
'type' => 'comments'
174+
}
175+
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
176+
177+
assert_equal 201, status
178+
assert_nil json_response['data']['body']
179+
assert_nil json_response['data']['links']['post']['linkage']
180+
assert_nil json_response['data']['links']['author']['linkage']
181+
end
182+
183+
def test_post_single_minimal_invalid
184+
post '/posts',
185+
{
186+
'data' => {
187+
'type' => 'posts'
188+
}
189+
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
190+
191+
assert_equal 422, status
192+
end
193+
159194
def test_update_association_without_content_type
160195
ruby = Section.find_by(name: 'ruby')
161196
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
189224
def test_post_update_association_has_many
190225
rogue = Comment.find_by(body: 'Rogue Comment Here')
191226
post '/posts/5/links/comments', { 'data' => [{type: 'comments', id: rogue.id.to_s }]}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
192-
227+
193228
assert_equal 204, status
194229
end
195230

0 commit comments

Comments
 (0)