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

Deserializer plug #222

Merged
merged 25 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
7 changes: 7 additions & 0 deletions lib/jsonapi/utils/data_to_params.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule JSONAPI.Utils.DataToParams do
alias JSONAPI.Utils.String, as: JString

@spec process(map) :: map
def process(%{"data" => nil}), do: nil
def process(%{"data" => _} = incoming) do
incoming
|> flatten_incoming()
Expand All @@ -15,6 +16,9 @@ defmodule JSONAPI.Utils.DataToParams do
end
def process(incoming), do: incoming

defp flatten_incoming(%{"data" => data}) when is_list(data) do
data
end
defp flatten_incoming(%{"data" => data} = incoming) do
incoming
|> Map.merge(data)
Expand Down Expand Up @@ -46,6 +50,9 @@ defmodule JSONAPI.Utils.DataToParams do

{key, %{"data" => %{"id" => id}}}, acc ->
Map.put(acc, transform_fields("#{key}-id"), id)

{key, %{"data" => list}}, acc when is_list(list) ->
Map.put(acc, transform_fields("#{key}-id"), Enum.map(list, &(Map.get(&1, "id"))))
end)
|> Map.merge(data)
|> Map.drop(["relationships"])
Expand Down
20 changes: 20 additions & 0 deletions test/jsonapi/plugs/deserializer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ defmodule JSONAPI.DeserializerTest do
assert result.params == %{"some-nonsense" => "yup"}
end

test "works with basic list of data" do
req_body = Jason.encode!(%{
"data" => [
%{"id" => "1", "type" => "car"},
%{"id" => "2", "type" => "car"}
]
})

conn =
Plug.Test.conn("POST", "/", req_body)
|> put_req_header("content-type", @ct)
|> put_req_header("accept", @ct)

result = ExamplePlug.call(conn, [])
assert result.params == [
%{"id" => "1", "type" => "car"},
%{"id" => "2", "type" => "car"}
]
end

test "deserializes attribute key names" do
req_body =
Jason.encode!(%{
Expand Down
84 changes: 84 additions & 0 deletions test/utils/data_to_params_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,64 @@ defmodule JSONAPI.DataToParamsTest do
}
end

test "converts to many relationship" do
incoming = %{
"data" => %{
"id" => "1",
"type" => "user",
"attributes" => %{
"foo-bar" => true
},
"relationships" => %{
"baz" => %{
"data" => [
%{"id" => "2", "type" => "baz"},
%{"id" => "3", "type" => "baz"}
]
}
}
}
}

result = JSONAPI.Utils.DataToParams.process(incoming)

assert result == %{
"id" => "1",
"type" => "user",
"foo-bar" => true,
"baz-id" => ["2", "3"]
}
end

test "converts polymorphic" do
incoming = %{
"data" => %{
"id" => "1",
"type" => "user",
"attributes" => %{
"foo-bar" => true
},
"relationships" => %{
"baz" => %{
"data" => [
%{"id" => "2", "type" => "baz"},
%{"id" => "3", "type" => "yooper"}
]
}
}
}
}

result = JSONAPI.Utils.DataToParams.process(incoming)

assert result == %{
"id" => "1",
"type" => "user",
"foo-bar" => true,
"baz-id" => ["2", "3"]
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't this mean you have a bug? Imagine a system like this... I have a super class called "Car", and I want to create two records, a "Ford" and a "Subaru". The way I'm reading this you've lost the typing information that the user provided — thus in my scenario I wouldn't know that a user was creating a "Subaru" and a "Ford".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed! Great point.

2481ee9

e4ffe06

}
end

test "processes single includes" do
incoming = %{
"data" => %{
Expand Down Expand Up @@ -151,6 +209,22 @@ defmodule JSONAPI.DataToParamsTest do
}
end

test "processes simple array of data" do
incoming = %{
"data" => [
%{"id" => "1", "type" => "user"},
%{"id" => "2", "type" => "user"}
]
}

result = JSONAPI.Utils.DataToParams.process(incoming)

assert result == [
%{"id" => "1", "type" => "user"},
%{"id" => "2", "type" => "user"}
]
end

test "processes empty keys" do
incoming = %{
"data" => %{
Expand Down Expand Up @@ -185,4 +259,14 @@ defmodule JSONAPI.DataToParamsTest do
"type" => "user"
}
end

test "processes nil data" do
incoming = %{
"data" => nil
}

result = JSONAPI.Utils.DataToParams.process(incoming)

assert result == nil
end
end