From 8247e10aafa67af887ced190d19d464c056051e2 Mon Sep 17 00:00:00 2001 From: Tyler Pachal Date: Thu, 3 Aug 2023 13:39:40 -0300 Subject: [PATCH] Nested query filters (#302) Co-authored-by: Tyler Pachal --- lib/jsonapi/utils/list.ex | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/jsonapi/utils/list.ex b/lib/jsonapi/utils/list.ex index 84e3d7b3..62023294 100644 --- a/lib/jsonapi/utils/list.ex +++ b/lib/jsonapi/utils/list.ex @@ -18,6 +18,8 @@ defmodule JSONAPI.Utils.List do iex> to_list_of_query_string_components(%{"filters" => %{"age" => 18, "name" => "John"}}) [{"filters[age]", 18}, {"filters[name]", "John"}] + iex> to_list_of_query_string_components(%{"filter" => %{"age" => 18, "car" => %{"make" => "honda", "model" => "civic"}}}) + [{"filter[age]", 18}, {"filter[car][make]", "honda"}, {"filter[car][model]", "civic"}] """ @spec to_list_of_query_string_components(map()) :: list(tuple()) def to_list_of_query_string_components(map) when is_map(map) do @@ -29,7 +31,9 @@ defmodule JSONAPI.Utils.List do end defp do_to_list_of_query_string_components({key, value}) when is_map(value) do - Enum.flat_map(value, fn {k, v} -> to_list_of_two_elem_tuple("#{key}[#{k}]", v) end) + Enum.flat_map(value, fn {k, v} -> + do_to_list_of_query_string_components({"#{key}[#{k}]", v}) + end) end defp do_to_list_of_query_string_components({key, value}),