Skip to content

Commit

Permalink
Omit non-Object meta (#124)
Browse files Browse the repository at this point in the history
* Omit meta from serialized output when the value would be nil, and therefore not an object, as the specification requires.

Cf. http://jsonapi.org/format/#document-meta

Where specified, a meta member can be used to include non-standard meta-information. The value of each meta member MUST be an object (a “meta object”).

* Pull common test data payload into @default_data

* Since the JSONAPI spec requires that meta be an Object, check for is_map rather than not nil

* Credo fixes for untouched code
  • Loading branch information
kbaird authored and Jason S committed Aug 28, 2018
1 parent eda4774 commit 70e04b5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
3 changes: 1 addition & 2 deletions lib/jsonapi/query_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ defmodule JSONAPI.QueryParser do
requested_fields =
value
|> String.split(",")
|> Enum.map(&String.to_atom/1)
|> Enum.into(MapSet.new())
|> Enum.into(MapSet.new(), &String.to_atom/1)

unless MapSet.subset?(requested_fields, valid_fields) do
bad_fields =
Expand Down
10 changes: 8 additions & 2 deletions lib/jsonapi/serializer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ defmodule JSONAPI.Serializer do

encoded_data = %{
data: encoded_data,
included: flatten_included(to_include),
meta: meta
included: flatten_included(to_include)
}

encoded_data =
if is_map(meta) do
Map.put(encoded_data, :meta, meta)
else
encoded_data
end

merge_links(encoded_data, data, view, conn, remove_links?(), with_pagination?())
end

Expand Down
4 changes: 1 addition & 3 deletions lib/jsonapi/utils/underscore.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ defmodule JSONAPI.Utils.Underscore do
end

def underscore(value) when is_map(value) do
value
|> Enum.map(&underscore/1)
|> Enum.into(%{})
Enum.into(value, %{}, &underscore/1)
end

def underscore({key, value}) do
Expand Down
53 changes: 35 additions & 18 deletions test/jsonapi_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ defmodule JSONAPITest do
use ExUnit.Case
use Plug.Test

@default_data %{
id: 1,
text: "Hello",
body: "Hi",
author: %{username: "jason", id: 2},
other_user: %{username: "josh", id: 3}
}

defmodule PostView do
use JSONAPI.View

Expand Down Expand Up @@ -83,15 +91,7 @@ defmodule JSONAPITest do
conn =
:get
|> conn("/posts")
|> Plug.Conn.assign(:data, [
%{
id: 1,
text: "Hello",
body: "Hi",
author: %{username: "jason", id: 2},
other_user: %{username: "josh", id: 3}
}
])
|> Plug.Conn.assign(:data, [@default_data])
|> Plug.Conn.assign(:meta, %{total_pages: 1})
|> MyPostPlug.call([])

Expand Down Expand Up @@ -133,15 +133,7 @@ defmodule JSONAPITest do
test "handles includes properly" do
conn =
conn(:get, "/posts?include=other_user")
|> Plug.Conn.assign(:data, [
%{
id: 1,
text: "Hello",
body: "Hi",
author: %{username: "jason", id: 2},
other_user: %{username: "josh", id: 3}
}
])
|> Plug.Conn.assign(:data, [@default_data])
|> Plug.Conn.fetch_query_params()
|> MyPostPlug.call([])

Expand Down Expand Up @@ -272,4 +264,29 @@ defmodule JSONAPITest do

assert Map.has_key?(json, "links")
end

test "omits explicit nil meta values as per http://jsonapi.org/format/#document-meta" do
conn =
:get
|> conn("/posts")
|> Plug.Conn.assign(:data, [@default_data])
|> Plug.Conn.assign(:meta, nil)
|> MyPostPlug.call([])

json = conn.resp_body |> Jason.decode!()

refute Map.has_key?(json, "meta")
end

test "omits implicit nil meta values as per http://jsonapi.org/format/#document-meta" do
conn =
:get
|> conn("/posts")
|> Plug.Conn.assign(:data, [@default_data])
|> MyPostPlug.call([])

json = conn.resp_body |> Jason.decode!()

refute Map.has_key?(json, "meta")
end
end

0 comments on commit 70e04b5

Please sign in to comment.