Skip to content

Commit 31c13c0

Browse files
committed
Merge pull request #707 from cerebris/id_respect_creatable
Make id respect creatable_fields
2 parents 09b5f2c + b8e2aff commit 31c13c0

3 files changed

Lines changed: 37 additions & 4 deletions

File tree

lib/jsonapi/request_parser.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,14 @@ def verify_permitted_params(params, allowed_fields)
537537
end
538538
end
539539
end
540-
when 'type', 'id'
540+
when 'type'
541+
when 'id'
542+
unless formatted_allowed_fields.include?(:id)
543+
params_not_allowed.push(:id)
544+
unless JSONAPI.configuration.raise_if_parameters_not_allowed
545+
params.delete :id
546+
end
547+
end
541548
else
542549
params_not_allowed.push(key)
543550
end

test/controllers/controller_test.rb

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

514+
def test_create_simple_id_not_allowed
515+
set_content_type_header!
516+
post :create, params:
517+
{
518+
data: {
519+
type: 'posts',
520+
id: 'asdfg',
521+
attributes: {
522+
title: 'JR is Great',
523+
body: 'JSONAPIResources is the greatest thing since unsliced bread.'
524+
},
525+
relationships: {
526+
author: {data: {type: 'people', id: '3'}}
527+
}
528+
}
529+
}
530+
531+
assert_response :bad_request
532+
assert_match /id is not allowed/, response.body
533+
assert_equal nil,response.location
534+
end
535+
514536
def test_create_link_to_missing_object
515537
set_content_type_header!
516538
post :create, params:
@@ -563,6 +585,7 @@ def test_create_extra_param_allow_extra_params
563585
{
564586
data: {
565587
type: 'posts',
588+
id: 'my_id',
566589
attributes: {
567590
asdfg: 'aaaa',
568591
title: 'JR is Great',
@@ -581,10 +604,13 @@ def test_create_extra_param_allow_extra_params
581604
assert_equal 'JR is Great', json_response['data']['attributes']['title']
582605
assert_equal 'JSONAPIResources is the greatest thing since unsliced bread.', json_response['data']['attributes']['body']
583606

584-
assert_equal 1, json_response['meta']["warnings"].count
607+
assert_equal 2, json_response['meta']["warnings"].count
585608
assert_equal "Param not allowed", json_response['meta']["warnings"][0]["title"]
586-
assert_equal "asdfg is not allowed.", json_response['meta']["warnings"][0]["detail"]
609+
assert_equal "id is not allowed.", json_response['meta']["warnings"][0]["detail"]
587610
assert_equal '105', json_response['meta']["warnings"][0]["code"]
611+
assert_equal "Param not allowed", json_response['meta']["warnings"][1]["title"]
612+
assert_equal "asdfg is not allowed.", json_response['meta']["warnings"][1]["detail"]
613+
assert_equal '105', json_response['meta']["warnings"][1]["code"]
588614
assert_equal json_response['data']['links']['self'], response.location
589615
ensure
590616
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
@@ -959,7 +959,7 @@ def self.updatable_fields(context)
959959
end
960960

961961
def self.creatable_fields(context)
962-
super(context) - [:subject]
962+
super(context) - [:subject, :id]
963963
end
964964

965965
def self.sortable_fields(context)

0 commit comments

Comments
 (0)