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

chore: ignore query params that start with underscore #2090

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/ae_mdw_web/plugs/paginated_plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule AeMdwWeb.Plugs.PaginatedPlug do

@spec call(Conn.t(), opts()) :: Conn.t()
def call(
%Conn{params: params, query_params: query_params, assigns: %{state: state}} = conn,
%Conn{params: params, query_params: _query_params, assigns: %{state: state}} = conn,
opts
) do
txi_scope? = Keyword.get(opts, :txi_scope?, true)
Expand All @@ -55,7 +55,7 @@ defmodule AeMdwWeb.Plugs.PaginatedPlug do
|> assign(:order_by, order_by)
|> assign(:scope, scope)
|> assign(:offset, {limit, page})
|> assign(:query, Map.drop(query_params, @pagination_params))
|> clean_query()
else
{:error, error_msg} ->
conn
Expand Down Expand Up @@ -252,4 +252,11 @@ defmodule AeMdwWeb.Plugs.PaginatedPlug do
defp generate_range(_state, scope_type, first, last) do
{scope_type, first..last}
end

defp clean_query(%Conn{query_params: query_params} = conn) do
query_params
|> Map.drop(@pagination_params)
|> Map.reject(fn {key, _val} -> String.starts_with?(key, "_") end)
|> then(&assign(conn, :query, &1))
end
end
27 changes: 27 additions & 0 deletions test/ae_mdw_web/plugs/paginated_plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,33 @@ defmodule AeMdwWeb.Plugs.PaginatedPlugTest do
|> PaginatedPlug.call([])
|> json_response(400)
end

test "it ignores query_params starting with _", %{conn: conn} do
store = empty_store()

assert %{pagination: {:forward, false, 10, false}, query: first_query} =
conn
|> with_store(store)
|> put_query(%{"_ignore_me" => "20", "direction" => "forward"})
|> PaginatedPlug.call([])
|> get_assigns()

assert map_size(first_query) == 0

assert %{pagination: {:forward, false, 10, false}, query: %{"id" => 1} = second_query} =
conn
|> with_store(store)
|> put_query(%{
"_ignore_me" => "20",
"direction" => "forward",
"limit" => "10",
"id" => 1
})
|> PaginatedPlug.call([])
|> get_assigns()

assert map_size(second_query) == 1
end
end

defp put_query(conn, query), do: %Conn{conn | params: query, query_params: query}
Expand Down
Loading