Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit non-Object meta #124

Merged
merged 4 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


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