Skip to content

Commit d579a9f

Browse files
committed
Make id respect creatable_fields
1 parent df16278 commit d579a9f

3 files changed

Lines changed: 37 additions & 4 deletions

File tree

lib/jsonapi/request.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,14 @@ def verify_permitted_params(params, allowed_fields)
532532
end
533533
end
534534
end
535-
when 'type', 'id'
535+
when 'type'
536+
when 'id'
537+
unless formatted_allowed_fields.include?(:id)
538+
params_not_allowed.push(:id)
539+
unless JSONAPI.configuration.raise_if_parameters_not_allowed
540+
params.delete :id
541+
end
542+
end
536543
else
537544
params_not_allowed.push(key)
538545
end

test/controllers/controller_test.rb

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,28 @@ def test_create_simple
456456
assert_equal json_response['data']['links']['self'], response.location
457457
end
458458

459+
def test_create_simple_id_not_allowed
460+
set_content_type_header!
461+
post :create, params:
462+
{
463+
data: {
464+
type: 'posts',
465+
id: 'asdfg',
466+
attributes: {
467+
title: 'JR is Great',
468+
body: 'JSONAPIResources is the greatest thing since unsliced bread.'
469+
},
470+
relationships: {
471+
author: {data: {type: 'people', id: '3'}}
472+
}
473+
}
474+
}
475+
476+
assert_response :bad_request
477+
assert_match /id is not allowed/, response.body
478+
assert_equal nil,response.location
479+
end
480+
459481
def test_create_link_to_missing_object
460482
set_content_type_header!
461483
post :create, params:
@@ -508,6 +530,7 @@ def test_create_extra_param_allow_extra_params
508530
{
509531
data: {
510532
type: 'posts',
533+
id: 'my_id',
511534
attributes: {
512535
asdfg: 'aaaa',
513536
title: 'JR is Great',
@@ -526,10 +549,13 @@ def test_create_extra_param_allow_extra_params
526549
assert_equal 'JR is Great', json_response['data']['attributes']['title']
527550
assert_equal 'JSONAPIResources is the greatest thing since unsliced bread.', json_response['data']['attributes']['body']
528551

529-
assert_equal 1, json_response['meta']["warnings"].count
552+
assert_equal 2, json_response['meta']["warnings"].count
530553
assert_equal "Param not allowed", json_response['meta']["warnings"][0]["title"]
531-
assert_equal "asdfg is not allowed.", json_response['meta']["warnings"][0]["detail"]
554+
assert_equal "id is not allowed.", json_response['meta']["warnings"][0]["detail"]
532555
assert_equal '105', json_response['meta']["warnings"][0]["code"]
556+
assert_equal "Param not allowed", json_response['meta']["warnings"][1]["title"]
557+
assert_equal "asdfg is not allowed.", json_response['meta']["warnings"][1]["detail"]
558+
assert_equal '105', json_response['meta']["warnings"][1]["code"]
533559
assert_equal json_response['data']['links']['self'], response.location
534560
ensure
535561
JSONAPI.configuration.raise_if_parameters_not_allowed = true

test/fixtures/active_record.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ def self.updatable_fields(context)
978978
end
979979

980980
def self.creatable_fields(context)
981-
super(context) - [:subject]
981+
super(context) - [:subject, :id]
982982
end
983983

984984
def self.sortable_fields(context)

0 commit comments

Comments
 (0)