From 811750d2e98c3222b96245eb7081f8ee6325e060 Mon Sep 17 00:00:00 2001 From: Zack Michener Date: Thu, 22 Feb 2024 12:04:09 -0800 Subject: [PATCH] Transform relationship keys with shallow field transformation options --- lib/jsonapi/utils/string.ex | 4 ++++ test/jsonapi/serializer_test.exs | 3 +++ 2 files changed, 7 insertions(+) diff --git a/lib/jsonapi/utils/string.ex b/lib/jsonapi/utils/string.ex index bbe308df..aa5b7f56 100644 --- a/lib/jsonapi/utils/string.ex +++ b/lib/jsonapi/utils/string.ex @@ -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 diff --git a/test/jsonapi/serializer_test.exs b/test/jsonapi/serializer_test.exs index 65543c3f..14cb113c 100644 --- a/test/jsonapi/serializer_test.exs +++ b/test/jsonapi/serializer_test.exs @@ -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"