@@ -13,7 +13,7 @@ class << self
13
13
14
14
def render_with_jsonapi_renderer
15
15
unlocked_params = Rails ::VERSION ::MAJOR >= 5 ? params . to_unsafe_h : params
16
- attributes = unlocked_params [ :data ] . present? ? unlocked_params [ :data ] [ :attributes ] : { }
16
+ attributes = unlocked_params [ :data ] . present? ? unlocked_params [ :data ] [ :attributes ] . merge ( id : unlocked_params [ :data ] [ :id ] ) : { }
17
17
author = Author . new ( attributes )
18
18
render jsonapi : author
19
19
end
@@ -34,6 +34,17 @@ def assert_parses(expected, actual, headers = {})
34
34
assert_equal ( expected , TestController . last_request_parameters )
35
35
end
36
36
37
+ def define_author_model_and_serializer
38
+ TestController . const_set ( :Author , Class . new ( ActiveModelSerializers ::Model ) do
39
+ attributes :name
40
+ end )
41
+ TestController . const_set ( :AuthorSerializer , Class . new ( ActiveModel ::Serializer ) do
42
+ type 'users'
43
+ attribute :id
44
+ attribute :name
45
+ end )
46
+ end
47
+
37
48
class WithoutRenderer < JsonApiRendererTest
38
49
setup do
39
50
require 'rails'
@@ -49,6 +60,7 @@ class WithoutRenderer < JsonApiRendererTest
49
60
match ':action' , to : TestController , via : [ :get , :post ]
50
61
end
51
62
end
63
+ define_author_model_and_serializer
52
64
end
53
65
54
66
def test_jsonapi_parser_not_registered
@@ -61,12 +73,12 @@ def test_jsonapi_parser_not_registered
61
73
end
62
74
63
75
def test_jsonapi_renderer_not_registered
64
- payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors "}}'
76
+ payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765 "}}'
65
77
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
66
78
post '/render_with_jsonapi_renderer' , params : payload , headers : headers
67
- assert_equal 500 , response . status
68
79
assert_equal '' , response . body
69
- assert response . request . env [ 'action_dispatch.exception' ] . is_a? ( ActionView ::MissingTemplate ) if response . request . present?
80
+ assert_equal 500 , response . status
81
+ assert_equal ActionView ::MissingTemplate , request . env [ 'action_dispatch.exception' ] . class
70
82
end
71
83
72
84
def test_jsonapi_parser
@@ -94,6 +106,7 @@ class WithRenderer < JsonApiRendererTest
94
106
match ':action' , to : TestController , via : [ :get , :post ]
95
107
end
96
108
end
109
+ define_author_model_and_serializer
97
110
end
98
111
99
112
def test_jsonapi_parser_registered
@@ -109,18 +122,13 @@ def test_jsonapi_parser_registered
109
122
def test_jsonapi_renderer_registered
110
123
expected = {
111
124
'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
- }
125
+ 'id' => '36c9c04e-86b1-4636-a5b0-8616672d1765' ,
126
+ 'type' => 'users' ,
127
+ 'attributes' => { 'name' => 'Johnny Rico' }
120
128
}
121
129
}
122
130
123
- payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors "}}'
131
+ payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765 "}}'
124
132
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
125
133
post '/render_with_jsonapi_renderer' , params : payload , headers : headers
126
134
assert_equal expected . to_json , response . body
@@ -133,10 +141,11 @@ def test_jsonapi_parser
133
141
'attributes' => {
134
142
'name' => 'John Doe'
135
143
} ,
136
- 'type' => 'users'
144
+ 'type' => 'users' ,
145
+ 'id' => '36c9c04e-86b1-4636-a5b0-8616672d1765'
137
146
}
138
147
} ,
139
- '{"data": {"attributes": {"name": "John Doe"}, "type": "users"}}' ,
148
+ '{"data": {"attributes": {"name": "John Doe"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765" }}' ,
140
149
'CONTENT_TYPE' => 'application/vnd.api+json'
141
150
)
142
151
end
0 commit comments