Skip to content

Commit

Permalink
Retain route order for warn on verify (phoenixframework#5454)
Browse files Browse the repository at this point in the history
  • Loading branch information
phoebe100 authored May 24, 2023
1 parent a409855 commit 6bee327
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/phoenix/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -506,10 +506,15 @@ defmodule Phoenix.Router do
{matches, {pipelines, _}} =
Enum.map_reduce(routes_with_exprs, {[], %{}}, &build_match/2)

verifies =
routes_per_path =
routes_with_exprs
|> Enum.group_by(&elem(&1, 1).path, &elem(&1, 0))
|> Enum.map(&build_verify/1)

verifies =
routes_with_exprs
|> Enum.map(&elem(&1, 1).path)
|> Enum.uniq()
|> Enum.map(&build_verify(&1, routes_per_path))

verify_catch_all =
quote generated: true do
Expand Down Expand Up @@ -574,7 +579,9 @@ defmodule Phoenix.Router do
end
end

defp build_verify({path, routes}) do
defp build_verify(path, routes_per_path) do
routes = Map.get(routes_per_path, path)

forward_plug =
Enum.find_value(routes, fn
%{kind: :forward, plug: plug} -> plug
Expand Down
21 changes: 21 additions & 0 deletions test/phoenix/verified_routes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ defmodule Phoenix.VerifiedRoutesTest do
forward "/plug_forward", UserController
end

defmodule CatchAllWarningRouter do
use Phoenix.Router
alias Phoenix.VerifiedRoutesTest.PostController

get "/", PostController, :root
get "/*path", PostController, :root, warn_on_verify: true
end

# Emulate regular endpoint functions

defmodule Endpoint do
Expand Down Expand Up @@ -528,6 +536,19 @@ defmodule Phoenix.VerifiedRoutesTest do
assert warnings =~
~s|no route path for Phoenix.VerifiedRoutesTest.Router matches "/should-warn/foobar"|
end

test "~p does not warn if route without warn_on_verify: true matches first" do
warnings =
ExUnit.CaptureIO.capture_io(:stderr, fn ->
defmodule VerifyFalse do
use Phoenix.VerifiedRoutes, endpoint: unquote(@endpoint), router: CatchAllWarningRouter

def test, do: ~p"/"
end
end)

assert warnings == ""
end
end
end
end

0 comments on commit 6bee327

Please sign in to comment.