Skip to content

Commit 7da6f2b

Browse files
authored
Merge pull request #154 from tfwright/fix/tolerate-invalid-pagination-params
fix: normalize invalid pagination params in the url
2 parents cae1b35 + 7782870 commit 7da6f2b

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

lib/live_admin/components/container.ex

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,21 @@ defmodule LiveAdmin.Components.Container do
263263
params =
264264
{defaults, types}
265265
|> Ecto.Changeset.cast(params, Map.keys(types))
266-
|> Ecto.Changeset.apply_action!(:update)
266+
|> Ecto.Changeset.apply_changes()
267267

268268
assign(socket, params)
269269
end
270270

271271
defp redirect_with_params(socket, params) do
272-
if Enum.all?(["per", "page", "sort-attr", "sort-dir"], &Map.has_key?(params, &1)) &&
273-
Map.get(params, "prefix") == socket.assigns.prefix do
272+
canonical = %{
273+
"page" => to_string(socket.assigns.page),
274+
"per" => to_string(socket.assigns.per),
275+
"sort-attr" => to_string(socket.assigns.sort_attr),
276+
"sort-dir" => to_string(socket.assigns.sort_dir),
277+
"prefix" => socket.assigns.prefix
278+
}
279+
280+
if Enum.all?(canonical, fn {key, val} -> Map.get(params, key) == val end) do
274281
socket
275282
else
276283
push_navigate(socket,

test/live_admin/components/container_test.exs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,23 @@ defmodule LiveAdmin.Components.ContainerTest do
100100
end
101101
end
102102

103+
describe "list resource with invalid page param" do
104+
setup %{conn: conn} do
105+
Repo.insert!(%User{})
106+
107+
{:error, {:live_redirect, %{to: redirect_to}}} =
108+
live(conn, "/user?prefix=public&per=10&page=abc&sort-attr=id&sort-dir=asc")
109+
110+
%{redirect_to: redirect_to}
111+
end
112+
113+
test "redirects to url with the invalid param replaced by the default", %{
114+
redirect_to: redirect_to
115+
} do
116+
assert redirect_to =~ ~r/page=1(?![0-9])/
117+
end
118+
end
119+
103120
describe "list resource with search param not matching any records" do
104121
setup %{conn: conn} do
105122
Repo.insert!(%User{name: "Tom"})

0 commit comments

Comments
 (0)