From c0a8d719ef23d433731d80d136465bbcdb71923f Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Mon, 18 Sep 2017 15:02:56 +0200 Subject: [PATCH] Rewrote api helpers to use string ids, which is what our ember client sends This reveals a bug in the comment controller, which is now fixed. --- .../controllers/comment_controller.ex | 1 + lib/code_corps_web/plugs/ids_to_integers.ex | 10 ++++-- test/support/json_api_helpers.ex | 32 +++++++++---------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/lib/code_corps_web/controllers/comment_controller.ex b/lib/code_corps_web/controllers/comment_controller.ex index bc5103919..9e4384208 100644 --- a/lib/code_corps_web/controllers/comment_controller.ex +++ b/lib/code_corps_web/controllers/comment_controller.ex @@ -5,6 +5,7 @@ defmodule CodeCorpsWeb.CommentController do action_fallback CodeCorpsWeb.FallbackController plug CodeCorpsWeb.Plug.DataToAttributes + plug CodeCorpsWeb.Plug.IdsToIntegers @spec index(Conn.t, map) :: Conn.t def index(%Conn{} = conn, %{} = params) do diff --git a/lib/code_corps_web/plugs/ids_to_integers.ex b/lib/code_corps_web/plugs/ids_to_integers.ex index 191844e10..9b78eaf56 100644 --- a/lib/code_corps_web/plugs/ids_to_integers.ex +++ b/lib/code_corps_web/plugs/ids_to_integers.ex @@ -1,7 +1,11 @@ defmodule CodeCorpsWeb.Plug.IdsToIntegers do @moduledoc ~S""" - Converts id values (primary or relationships) in a conn params map into - integers, if applicable. + Converts `id` values in a `conn` parameters map into integers, if applicable. + + The JSON API specification expects `id` values in resource objects to be + strings. + + See http://jsonapi.org/format/#document-resource-object-identification """ alias Plug.Conn @@ -22,7 +26,7 @@ defmodule CodeCorpsWeb.Plug.IdsToIntegers do @spec convert_key_value(tuple) :: tuple defp convert_key_value({key, value}) do - case key |> convert?() do + case convert?(key) do true -> {key, value |> ensure_integer()} false -> {key, value} end diff --git a/test/support/json_api_helpers.ex b/test/support/json_api_helpers.ex index 0f28dc1ed..41946cbc8 100644 --- a/test/support/json_api_helpers.ex +++ b/test/support/json_api_helpers.ex @@ -3,14 +3,19 @@ defmodule CodeCorps.JsonAPIHelpers do with attributes <- build_attributes(attrs), relationships <- build_relationships(attrs) do - build_payload(attributes, relationships) + %{ + "data" => %{ + "attributes" => attributes, + "relationships" => relationships + } + } end end defp build_attributes(attrs) do attrs |> Enum.filter(&is_attribute(&1)) - |> Enum.reduce(%{}, &insert_string_key(&1, &2)) + |> Enum.reduce(%{}, &add_attribute(&1, &2)) end defp build_relationships(attrs) do @@ -19,11 +24,12 @@ defmodule CodeCorps.JsonAPIHelpers do |> Enum.reduce(%{}, &Map.merge(&2, &1)) end - defp build_relationship({key, record}) do - with id <- record.id, - type <- model_name_as_string(record) + defp build_relationship({atom_key, record}) do + with id <- record.id |> to_correct_type(), + type <- record |> model_name_as_string(), + string_key = atom_key |> Atom.to_string do - %{} |> Map.put(Atom.to_string(key), %{"data" => %{"id" => id, "type" => type}}) + %{} |> Map.put(string_key, %{"data" => %{"id" => id, "type" => type}}) end end @@ -40,16 +46,10 @@ defmodule CodeCorps.JsonAPIHelpers do |> String.downcase end - defp insert_string_key({key, value}, map) do - with string_key <- Atom.to_string(key), do: Map.put(map, string_key, value) + defp add_attribute({key, value}, %{} = attrs) do + attrs |> Map.put(key |> Atom.to_string, value) end - defp build_payload(attributes, relationships) do - %{ - "data" => %{ - "attributes" => attributes, - "relationships" => relationships - } - } - end + defp to_correct_type(value) when is_integer(value), do: value |> Integer.to_string + defp to_correct_type(value), do: value end