Skip to content

Commit 9644523

Browse files
committed
allow users to configure to skip missing keys
1 parent bc95701 commit 9644523

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
@@ -168,6 +168,7 @@ defmodule JSONAPI.View do
168168
{namespace, opts} = Keyword.pop(opts, :namespace)
169169
{path, opts} = Keyword.pop(opts, :path)
170170
{paginator, _opts} = Keyword.pop(opts, :paginator)
171+
{skip_missing_keys, _opts} = Keyword.pop(opts, :skip_missing_keys, false)
171172

172173
quote do
173174
alias JSONAPI.{Serializer, View}
@@ -178,6 +179,7 @@ defmodule JSONAPI.View do
178179
@namespace unquote(namespace)
179180
@path unquote(path)
180181
@paginator unquote(paginator)
182+
@skip_missing_keys unquote(skip_missing_keys)
181183

182184
@impl View
183185
def id(nil), do: nil
@@ -197,11 +199,21 @@ defmodule JSONAPI.View do
197199
function_exported?(__MODULE__, :get_field, 3) ->
198200
apply(__MODULE__, :get_field, [field, data, conn])
199201

202+
Map.has_key?(data, field) ->
203+
Map.get(data, field)
204+
205+
@skip_missing_keys ->
206+
:skip
207+
200208
true ->
201209
Map.get(data, field)
202210
end
203211

204-
Map.put(intermediate_map, field, value)
212+
if value == :skip do
213+
intermediate_map
214+
else
215+
Map.put(intermediate_map, field, value)
216+
end
205217
end)
206218
end
207219

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)