Skip to content

fix: merge body params into params as per Plug conventions #596

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

Closed
wants to merge 1 commit into from
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
3 changes: 2 additions & 1 deletion lib/open_api_spex/operation2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ defmodule OpenApiSpex.Operation2 do
end

defp maybe_replace_body(conn, _body, false), do: conn
defp maybe_replace_body(conn, body, true), do: %{conn | body_params: body}
defp maybe_replace_body(conn = %{params: params}, body, true),
do: %{conn | body_params: body, params: Map.merge(params, body)}

defp cast_parameters(conn, operation, spec, opts) do
CastParameters.cast(conn, operation, spec, opts)
Expand Down
9 changes: 7 additions & 2 deletions test/operation2_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ defmodule OpenApiSpex.Operation2Test do

describe "cast/4" do
test "cast request body" do
conn = create_conn(%{"user" => %{"email" => "[email protected]"}})
conn =
create_conn(%{"user" => %{"email" => "[email protected]"}})
|> Map.put(:params, %{"id" => 1})

assert {:ok, conn} =
Operation2.cast(
Expand All @@ -185,7 +187,10 @@ defmodule OpenApiSpex.Operation2Test do
"application/json"
)

assert %Plug.Conn{} = conn
assert %Plug.Conn{
body_params: %{user: %{email: "[email protected]"}},
params: %{user: %{email: "[email protected]"}}
} = conn
end

test "cast request body with replace_params: false" do
Expand Down