@@ -12,8 +12,16 @@ class << self
1212 end
1313
1414 def render_with_jsonapi_renderer
15- unlocked_params = Rails ::VERSION ::MAJOR >= 5 ? params . to_unsafe_h : params
16- attributes = unlocked_params [ :data ] . present? ? unlocked_params [ :data ] [ :attributes ] : { }
15+ permitted_params = params . permit ( data : [ :id , :type , attributes : [ :name ] ] )
16+ permitted_params = permitted_params . to_h . with_indifferent_access
17+ attributes =
18+ if permitted_params [ :data ]
19+ permitted_params [ :data ] [ :attributes ] . merge ( id : permitted_params [ :data ] [ :id ] )
20+ else
21+ # Rails returns empty params when no mime type can be negotiated.
22+ # (Until https://github.com/rails/rails/pull/26632 is reviewed.)
23+ permitted_params
24+ end
1725 author = Author . new ( attributes )
1826 render jsonapi : author
1927 end
@@ -34,6 +42,17 @@ def assert_parses(expected, actual, headers = {})
3442 assert_equal ( expected , TestController . last_request_parameters )
3543 end
3644
45+ def define_author_model_and_serializer
46+ TestController . const_set ( :Author , Class . new ( ActiveModelSerializers ::Model ) do
47+ attributes :name
48+ end )
49+ TestController . const_set ( :AuthorSerializer , Class . new ( ActiveModel ::Serializer ) do
50+ type 'users'
51+ attribute :id
52+ attribute :name
53+ end )
54+ end
55+
3756 class WithoutRenderer < JsonApiRendererTest
3857 setup do
3958 require 'rails'
@@ -49,6 +68,7 @@ class WithoutRenderer < JsonApiRendererTest
4968 match ':action' , to : TestController , via : [ :get , :post ]
5069 end
5170 end
71+ define_author_model_and_serializer
5272 end
5373
5474 def test_jsonapi_parser_not_registered
@@ -61,12 +81,12 @@ def test_jsonapi_parser_not_registered
6181 end
6282
6383 def test_jsonapi_renderer_not_registered
64- payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors "}}'
84+ payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765 "}}'
6585 headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
6686 post '/render_with_jsonapi_renderer' , params : payload , headers : headers
67- assert_equal 500 , response . status
6887 assert_equal '' , response . body
69- assert response . request . env [ 'action_dispatch.exception' ] . is_a? ( ActionView ::MissingTemplate ) if response . request . present?
88+ assert_equal 500 , response . status
89+ assert_equal ActionView ::MissingTemplate , request . env [ 'action_dispatch.exception' ] . class
7090 end
7191
7292 def test_jsonapi_parser
@@ -94,6 +114,7 @@ class WithRenderer < JsonApiRendererTest
94114 match ':action' , to : TestController , via : [ :get , :post ]
95115 end
96116 end
117+ define_author_model_and_serializer
97118 end
98119
99120 def test_jsonapi_parser_registered
@@ -109,18 +130,13 @@ def test_jsonapi_parser_registered
109130 def test_jsonapi_renderer_registered
110131 expected = {
111132 'data' => {
112- 'id' => 'author' ,
113- 'type' => 'authors' ,
114- 'attributes' => { 'name' => 'Johnny Rico' } ,
115- 'relationships' => {
116- 'posts' => { 'data' => nil } ,
117- 'roles' => { 'data' => nil } ,
118- 'bio' => { 'data' => nil }
119- }
133+ 'id' => '36c9c04e-86b1-4636-a5b0-8616672d1765' ,
134+ 'type' => 'users' ,
135+ 'attributes' => { 'name' => 'Johnny Rico' }
120136 }
121137 }
122138
123- payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors "}}'
139+ payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765 "}}'
124140 headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
125141 post '/render_with_jsonapi_renderer' , params : payload , headers : headers
126142 assert_equal expected . to_json , response . body
@@ -133,10 +149,11 @@ def test_jsonapi_parser
133149 'attributes' => {
134150 'name' => 'John Doe'
135151 } ,
136- 'type' => 'users'
152+ 'type' => 'users' ,
153+ 'id' => '36c9c04e-86b1-4636-a5b0-8616672d1765'
137154 }
138155 } ,
139- '{"data": {"attributes": {"name": "John Doe"}, "type": "users"}}' ,
156+ '{"data": {"attributes": {"name": "John Doe"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765" }}' ,
140157 'CONTENT_TYPE' => 'application/vnd.api+json'
141158 )
142159 end
0 commit comments