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

Transform relationship keys with shallow field transformation options (#314) #315

Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
## NEXT
...

## 1.7.1 (2024-02-23)
* Fix bug where underscore/dasherize misses single characters by @protestContest in https://github.com/beam-community/jsonapi/pull/316
* Transform relationship keys with shallow field transformation options (#314) by @protestContest in https://github.com/beam-community/jsonapi/pull/315
mattpolzin marked this conversation as resolved.
Show resolved Hide resolved

**Full Changelog**: https://github.com/beam-community/jsonapi/compare/1.7.0...1.7.1

## 1.7.0 (2024-02-15)

## What's Changed
Expand Down
4 changes: 4 additions & 0 deletions lib/jsonapi/utils/string.ex
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,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
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule JSONAPI.Mixfile do
def project do
[
app: :jsonapi,
version: "1.7.0",
version: "1.7.1",
package: package(),
compilers: compilers(Mix.env()),
description: description(),
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
Loading