Skip to content

Commit b8730b5

Browse files
committed
allow users to configure to skip missing keys
1 parent b121afe commit b8730b5

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

lib/jsonapi/view.ex

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ defmodule JSONAPI.View do
248248
{path, opts} = Keyword.pop(opts, :path)
249249
{paginator, opts} = Keyword.pop(opts, :paginator)
250250
{polymorphic_resource?, _opts} = Keyword.pop(opts, :polymorphic_resource?, false)
251+
{skip_missing_keys, _opts} = Keyword.pop(opts, :skip_missing_keys, false)
251252

252253
quote do
253254
alias JSONAPI.{Serializer, View}
@@ -259,6 +260,7 @@ defmodule JSONAPI.View do
259260
@path unquote(path)
260261
@paginator unquote(paginator)
261262
@polymorphic_resource? unquote(polymorphic_resource?)
263+
@skip_missing_keys unquote(skip_missing_keys)
262264

263265
@impl View
264266
def id(nil), do: nil
@@ -278,11 +280,21 @@ defmodule JSONAPI.View do
278280
function_exported?(__MODULE__, :get_field, 3) ->
279281
apply(__MODULE__, :get_field, [field, data, conn])
280282

283+
Map.has_key?(data, field) ->
284+
Map.get(data, field)
285+
286+
@skip_missing_keys ->
287+
:skip
288+
281289
true ->
282290
Map.get(data, field)
283291
end
284292

285-
Map.put(intermediate_map, field, value)
293+
if value == :skip do
294+
intermediate_map
295+
else
296+
Map.put(intermediate_map, field, value)
297+
end
286298
end)
287299
end
288300

test/jsonapi_test.exs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ defmodule JSONAPITest do
1111
}
1212

1313
defmodule PostView do
14-
use JSONAPI.View
14+
use JSONAPI.View, skip_missing_keys: true
1515

16-
def fields, do: [:text, :body, :excerpt, :first_character]
16+
def fields, do: [:text, :body, :excerpt, :first_character, :maybe_missing_key]
1717
def type, do: "mytype"
1818

1919
def relationships do
@@ -156,6 +156,40 @@ defmodule JSONAPITest do
156156
assert Map.has_key?(json, "links")
157157
end
158158

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+
159193
test "handles includes properly" do
160194
conn =
161195
:get

0 commit comments

Comments
 (0)