Skip to content

Commit

Permalink
Transform relationship keys with shallow field transformation options
Browse files Browse the repository at this point in the history
  • Loading branch information
protestContest committed Feb 22, 2024
1 parent 2edf707 commit 811750d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/jsonapi/utils/string.ex
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ defmodule JSONAPI.Utils.String do
end)
end

def expand_root_keys(key, fun) when is_binary(key) or is_atom(key) do
fun.(key)
end

def expand_root_keys(value, _fun), do: expand_fields(value, &to_string/1)

defp maybe_expand_fields(values, fun) when is_list(values) do
Expand Down
3 changes: 3 additions & 0 deletions test/jsonapi/serializer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -667,12 +667,15 @@ defmodule JSONAPI.SerializerTest do
encoded = Serializer.serialize(PostView, data, nil)

attributes = encoded[:data][:attributes]
relationships = encoded[:data][:relationships]
included = encoded[:included]

assert attributes["full-description"] == data[:full_description]
assert attributes["body"]["data_attr"] == "foo"
assert attributes["inserted-at"] == data[:inserted_at]

assert Map.has_key?(relationships, "best-comments")

author = Enum.find(included, &(&1[:type] == "user" && &1[:id] == "2"))
assert author != nil
assert author[:attributes]["last-name"] == "bonds"
Expand Down

0 comments on commit 811750d

Please sign in to comment.