Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested query filters #302

Merged
merged 2 commits into from
Aug 3, 2023
Merged

Conversation

TylerPachal
Copy link
Contributor

According to the spec, you can filter on included resources by doing something like:

/people?filter[car][make]=honda

When I try a request like this, everything works besides for the construction of the pagination urls.

This PR fixes the Utils.List.to_list_of_query_string_components/1 function so that its output is valid for the URI.encode_query/1 function.


Before this PR:

iex(1)> %{"filter" => %{"car" => %{"make" => "honda", "model" => "civic"}}} |> JSONAPI.Utils.List.to_list_of_query_string_components() |> URI.encode_query()
** (Protocol.UndefinedError) protocol String.Chars not implemented for %{"make" => "honda", "model" => "civic"} of type Map. This protocol is implemented for the following type(s): Atom, BitString, Cldr.Calendar.Duration, Cldr.Currency, Cldr.LanguageTag, Cldr.LanguageTag.T, Cldr.LanguageTag.U, Date, DateTime, Decimal, Float, Hex.Solver.Assignment, Hex.Solver.Constraints.Empty, Hex.Solver.Constraints.Range, Hex.Solver.Constraints.Union, Hex.Solver.Incompatibility, Hex.Solver.PackageRange, Hex.Solver.Term, Integer, List, NaiveDateTime, OpenApiSpex.Cast.Error, Phoenix.LiveComponent.CID, Postgrex.Copy, Postgrex.Query, Time, URI, Version, Version.Requirement
    (elixir 1.14.3) lib/string/chars.ex:3: String.Chars.impl_for!/1
    (elixir 1.14.3) lib/string/chars.ex:22: String.Chars.to_string/1
    (elixir 1.14.3) lib/uri.ex:153: URI.encode_kv_pair/2
    (elixir 1.14.3) lib/enum.ex:1755: anonymous fn/2 in Enum.map_join/3
    (elixir 1.14.3) lib/enum.ex:4289: Enum.map_intersperse_list/3
    (elixir 1.14.3) lib/enum.ex:1755: Enum.map_join/3
    iex:2: (file)

After this PR:

iex(1)> %{"filter" => %{"car" => %{"make" => "honda", "model" => "civic"}}} |> JSONAPI.Utils.List.to_list_of_query_string_components() |> URI.encode_query()
"filter%5Bcar%5D%5Bmake%5D=honda&filter%5Bcar%5D%5Bmodel%5D=civic"

@TylerPachal TylerPachal requested a review from a team as a code owner August 2, 2023 14:40
@@ -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})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annotation: This fix was just to make a recursive call, so that nested maps can be handled.

@mattpolzin mattpolzin merged commit 8247e10 into beam-community:main Aug 3, 2023
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants