config :phoenix, :json_library, JasonHello, I'm sure it is a known issue but I could not find a duplicate.
With this command: mix phx.gen.json Content Version versions changelog_id:references:changelogs datamap:map metamap:map I generated a test: test/myapp_web/controllers/version_controller_test.exs.
The test for "create version" is correct:
describe "create version" do
test "renders version when data is valid", %{conn: conn} do
conn = post(conn, Routes.version_path(conn, :create), version: @create_attrs)
assert %{"id" => id} = json_response(conn, 201)["data"]
conn = get(conn, Routes.version_path(conn, :show, id))
assert %{
"id" => id,
"datamap" => %{},
"metamap" => %{}
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, Routes.version_path(conn, :create), version: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
But the test for "update version" is incorrect as it tries to match empty tuples and not maps in the last assert:
describe "update version" do
setup [:create_version]
test "renders version when data is valid", %{conn: conn, version: %Version{id: id} = version} do
conn = put(conn, Routes.version_path(conn, :update, version), version: @update_attrs)
assert %{"id" => ^id} = json_response(conn, 200)["data"]
conn = get(conn, Routes.version_path(conn, :show, id))
assert %{
"id" => id,
"datamap" => {},
"metamap" => {}
} = json_response(conn, 200)["data"]
end
@lud @chrismccord I believe this is fixed in master branch.
Previosuly, it was
assert %{
"id" => id<%= for {key, val} <- schema.params.update do %>,
"<%= key %>" => <%= Phoenix.json_library().encode!(val) %><% end %>
} = json_response(conn, 200)["data"]
it enocde the val but didnt decode and inspect the val
which is fixed in master
assert %{
"id" => id<%= for {key, val} <- schema.params.update |> Phoenix.json_library().encode!() |> Phoenix.json_library().decode!() do %>,
"<%= key %>" => <%= inspect(val) %><% end %>
} = json_response(conn, 200)["data"]
Fixed as mentioned, thank you @lud for reporting and thank you @sushant12 for verifying!
Most helpful comment
Fixed as mentioned, thank you @lud for reporting and thank you @sushant12 for verifying!