Skip to content

Commit

Permalink
Fix bug where map without rules returns empty map
Browse files Browse the repository at this point in the history
  • Loading branch information
martinthenth committed Nov 15, 2024
1 parent d7ca6e9 commit 62e2666
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ erl_crash.dump
*.beam
/config/*.secret.exs
.elixir_ls/
.idea/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.1.1

- Fixes a regression where `:map` without rules returns an empty map instead of the user's input (https://github.com/martinthenth/goal/issues/112).

# 1.1.0

- Adds support for enum arrays (https://github.com/martinthenth/goal/pull/107 - by [@davorbadrov](https://github.com/davorbadrov))
Expand Down
18 changes: 14 additions & 4 deletions lib/goal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,11 @@ defmodule Goal do
quote do
properties = Enum.reduce(unquote(children), %{}, &Map.merge(&2, &1))

%{unquote(name) => [{:type, unquote(type)} | [properties: properties]]}
if properties == %{} do
%{unquote(name) => [type: unquote(type)]}
else
%{unquote(name) => [type: unquote(type), properties: properties]}
end
end
end

Expand All @@ -594,9 +598,15 @@ defmodule Goal do
quote do
properties = Enum.reduce(unquote(children), %{}, &Map.merge(&2, &1))

%{
unquote(name) => [{:type, unquote(type)} | [{:required, true} | [properties: properties]]]
}
if properties == %{} do
%{unquote(name) => [{:type, unquote(type)} | {:required, true}]}
else
%{
unquote(name) => [
{:type, unquote(type)} | [{:required, true} | [properties: properties]]
]
}
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Goal.MixProject do
use Mix.Project

@version "1.1.0"
@version "1.1.1"
@source_url "https://github.com/martinthenth/goal"
@changelog_url "https://github.com/martinthenth/goal/blob/main/CHANGELOG.md"

Expand Down
38 changes: 34 additions & 4 deletions test/goal_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule GoalTest do
optional(:last_name, :string)
required(:any_1)
optional(:any_2)
optional(:fields, :map)
end

defparams :negatives do
Expand Down Expand Up @@ -91,7 +92,8 @@ defmodule GoalTest do
first_name: [type: :string],
last_name: [type: :string],
any_1: [type: :any, required: true],
any_2: [type: :any]
any_2: [type: :any],
fields: [type: :map]
}

assert schema(:index) == %{
Expand Down Expand Up @@ -156,11 +158,11 @@ defmodule GoalTest do
test "changeset/2" do
assert %Ecto.Changeset{
action: nil,
changes: %{},
changes: %{id: 123, any_1: 123, fields: %{"hello" => "world"}},
errors: [],
data: %{},
valid?: true
} = changeset(:show, %{id: 123, any_1: 123})
} = changeset(:show, %{id: 123, any_1: 123, fields: %{"hello" => "world"}})

assert %Ecto.Changeset{
action: nil,
Expand Down Expand Up @@ -906,6 +908,26 @@ defmodule GoalTest do
}
end

test "unstructured map" do
data = %{
"map" => %{
"string" => "hello",
"integer" => 5
}
}

schema = %{map: [type: :map]}

changeset = Goal.build_changeset(schema, data)

assert changes_on(changeset) == %{
map: %{
"string" => "hello",
"integer" => 5
}
}
end

test "invalid nested map" do
schema = %{
key_1: [type: :string],
Expand Down Expand Up @@ -1787,14 +1809,22 @@ defmodule GoalTest do
assert Goal.recase_keys(schema, params, opts) == %{first_name: "Jane", last_name: "Doe"}
end

test "map" do
test "empty map" do
schema = %{description: [type: :map, required: true]}
params = %{"description" => %{}}
opts = [recase_keys: [from: :camel_case]]

assert Goal.recase_keys(schema, params, opts) == %{description: %{}}
end

test "non-empty map" do
schema = %{description: [type: :map]}
params = %{"description" => %{"hello" => "world"}}
opts = [recase_keys: [from: :camel_case]]

assert Goal.recase_keys(schema, params, opts) == %{description: %{"hello" => "world"}}
end

test "nested map" do
schema = %{
first_name: [type: :string],
Expand Down

0 comments on commit 62e2666

Please sign in to comment.