Skip to content
Closed
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
32 changes: 23 additions & 9 deletions lib/phoenix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ defmodule Phoenix do

"""
def json_library do
Application.get_env(:phoenix, :json_library, Jason)
Application.get_env(:phoenix, :json_library) || default_json_library()
end

if Code.ensure_loaded?(JSON) do
defp default_json_library, do: JSON
else
defp default_json_library, do: Jason
end

@doc """
Expand All @@ -63,14 +69,22 @@ defmodule Phoenix do
end

defp warn_on_missing_json_library do
configured_lib = Application.get_env(:phoenix, :json_library)

if configured_lib && not Code.ensure_loaded?(configured_lib) do
IO.warn("""
found #{inspect(configured_lib)} in your application configuration
for Phoenix JSON encoding, but module #{inspect(configured_lib)} is not available.
Ensure #{inspect(configured_lib)} is listed as a dependency in mix.exs.
""")
lib = json_library()

if not Code.ensure_loaded?(lib) do
if configured_lib = Application.get_env(:phoenix, :json_library) do
IO.warn("""
found #{inspect(configured_lib)} in your application configuration
for Phoenix JSON encoding, but module #{inspect(configured_lib)} is not available.
Ensure #{inspect(configured_lib)} is listed as a dependency in mix.exs.
""")
else
IO.warn("""
Phoenix requires a JSON library. By default Phoenix uses #{inspect(lib)},
but module #{inspect(lib)} is not available.
Ensure #{inspect(lib)} is listed as a dependency in mix.exs.
""")
end
end
end
end
9 changes: 8 additions & 1 deletion lib/phoenix/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,14 @@ defmodule Phoenix.Router do
end

defp build_pipes(name, pipe_through) do
plugs = pipe_through |> Enum.reverse() |> Enum.map(&{&1, [], true})
plugs =
pipe_through
|> Enum.reverse()
|> Enum.map(fn
{plug, opts} -> {plug, opts, true}
plug -> {plug, [], true}
end)

opts = [init_mode: Phoenix.plug_init_mode(), log_on_halt: :debug]
{conn, body} = Plug.Builder.compile(__ENV__, plugs, opts)

Expand Down
18 changes: 18 additions & 0 deletions test/phoenix/router/pipeline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ defmodule Phoenix.Router.PipelineTest.Router do
get "/", SampleController, :index
end

defmodule GreetingPlug do
def init(opts), do: opts
def call(conn, opts) do
greeting = Keyword.get(opts, :greeting, "hello")
conn |> Plug.Conn.assign(:greeting, greeting)
end
end

scope "/greeting" do
pipe_through [{GreetingPlug, greeting: "Hola!"}]
get "/", SampleController, :index
end

defp stop(conn, _) do
conn |> send_resp(200, "stop") |> halt
end
Expand Down Expand Up @@ -114,6 +127,11 @@ defmodule Phoenix.Router.PipelineTest do
assert conn.resp_body == "stop"
end

test "supports module plug with options" do
conn = call(Router, :get, "/greeting")
assert conn.assigns[:greeting] == "Hola!"
end

test "wraps failures on call" do
assert_raise Plug.Conn.WrapperError, fn ->
call(Router, :get, "/route_that_crashes")
Expand Down