@@ -12,8 +12,16 @@ class << self
12
12
end
13
13
14
14
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
17
25
author = Author . new ( attributes )
18
26
render jsonapi : author
19
27
end
@@ -34,6 +42,17 @@ def assert_parses(expected, actual, headers = {})
34
42
assert_equal ( expected , TestController . last_request_parameters )
35
43
end
36
44
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
+
37
56
class WithoutRenderer < JsonApiRendererTest
38
57
setup do
39
58
require 'rails'
@@ -49,6 +68,7 @@ class WithoutRenderer < JsonApiRendererTest
49
68
match ':action' , to : TestController , via : [ :get , :post ]
50
69
end
51
70
end
71
+ define_author_model_and_serializer
52
72
end
53
73
54
74
def test_jsonapi_parser_not_registered
@@ -61,12 +81,12 @@ def test_jsonapi_parser_not_registered
61
81
end
62
82
63
83
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 "}}'
65
85
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
66
86
post '/render_with_jsonapi_renderer' , params : payload , headers : headers
67
- assert_equal 500 , response . status
68
87
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
70
90
end
71
91
72
92
def test_jsonapi_parser
@@ -94,6 +114,7 @@ class WithRenderer < JsonApiRendererTest
94
114
match ':action' , to : TestController , via : [ :get , :post ]
95
115
end
96
116
end
117
+ define_author_model_and_serializer
97
118
end
98
119
99
120
def test_jsonapi_parser_registered
@@ -109,18 +130,13 @@ def test_jsonapi_parser_registered
109
130
def test_jsonapi_renderer_registered
110
131
expected = {
111
132
'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' }
120
136
}
121
137
}
122
138
123
- payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors "}}'
139
+ payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765 "}}'
124
140
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
125
141
post '/render_with_jsonapi_renderer' , params : payload , headers : headers
126
142
assert_equal expected . to_json , response . body
@@ -133,10 +149,11 @@ def test_jsonapi_parser
133
149
'attributes' => {
134
150
'name' => 'John Doe'
135
151
} ,
136
- 'type' => 'users'
152
+ 'type' => 'users' ,
153
+ 'id' => '36c9c04e-86b1-4636-a5b0-8616672d1765'
137
154
}
138
155
} ,
139
- '{"data": {"attributes": {"name": "John Doe"}, "type": "users"}}' ,
156
+ '{"data": {"attributes": {"name": "John Doe"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765" }}' ,
140
157
'CONTENT_TYPE' => 'application/vnd.api+json'
141
158
)
142
159
end
0 commit comments