@@ -11,9 +11,9 @@ defmodule JSONAPITest do
11
11
}
12
12
13
13
defmodule PostView do
14
- use JSONAPI.View
14
+ use JSONAPI.View , skip_missing_keys: true
15
15
16
- def fields , do: [ :text , :body , :excerpt , :first_character ]
16
+ def fields , do: [ :text , :body , :excerpt , :first_character , :maybe_missing_key ]
17
17
def type , do: "mytype"
18
18
19
19
def relationships do
@@ -156,6 +156,40 @@ defmodule JSONAPITest do
156
156
assert Map . has_key? ( json , "links" )
157
157
end
158
158
159
+ test "does not include keys that are missing in data" do
160
+ conn =
161
+ :get
162
+ |> conn ( "/posts" )
163
+ |> Plug.Conn . assign ( :data , [ @ default_data ] )
164
+ |> Plug.Conn . assign ( :meta , % { total_pages: 1 } )
165
+ |> Plug.Conn . fetch_query_params ( )
166
+ |> MyPostPlug . call ( [ ] )
167
+
168
+ json = Jason . decode! ( conn . resp_body )
169
+
170
+ data_list = Map . get ( json , "data" )
171
+ [ data | _ ] = data_list
172
+ refute Map . has_key? ( data [ "attributes" ] , "maybe_missing_key" )
173
+ end
174
+
175
+ test "does include 'maybe_missing_key' if not configured" do
176
+ data = Map . put ( @ default_data , :maybe_missing_key , "foo" )
177
+
178
+ conn =
179
+ :get
180
+ |> conn ( "/posts" )
181
+ |> Plug.Conn . assign ( :data , [ data ] )
182
+ |> Plug.Conn . assign ( :meta , % { total_pages: 1 } )
183
+ |> Plug.Conn . fetch_query_params ( )
184
+ |> MyPostPlug . call ( [ ] )
185
+
186
+ json = Jason . decode! ( conn . resp_body )
187
+
188
+ data_list = Map . get ( json , "data" )
189
+ [ data | _ ] = data_list
190
+ assert Map . has_key? ( data [ "attributes" ] , "maybe_missing_key" )
191
+ end
192
+
159
193
test "handles includes properly" do
160
194
conn =
161
195
:get
0 commit comments