Skip to content

Commit

Permalink
Transform relationship keys with shallow field transformation options (
Browse files Browse the repository at this point in the history
…#314) (#315)

* Transform relationship keys with shallow field transformation options

* bump patch version

* Add line item for other bug fix

---------

Co-authored-by: Mathew Polzin <[email protected]>
  • Loading branch information
protestContest and mattpolzin authored Feb 24, 2024
1 parent f3db97d commit 00d01cf
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
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

**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

0 comments on commit 00d01cf

Please sign in to comment.