Modify our CastAndValidate plug to include the new functionality provided by the :replace_params config option

This allows us to configure Open API Spex to not overwrite the params with the casted versions which violates the Plug.Conn.t() contract

https://github.com/open-api-spex/open_api_spex/issues/92
https://github.com/open-api-spex/open_api_spex/pull/425
This commit is contained in:
Mark Felder 2024-01-30 14:49:55 -05:00
parent 91a70ba552
commit 608466d098
1 changed files with 8 additions and 6 deletions

View File

@ -27,10 +27,12 @@ def init(opts) do
@impl Plug
def call(conn, %{operation_id: operation_id, render_error: render_error}) do
def call(conn, %{operation_id: operation_id, render_error: render_error} = opts) do
{spec, operation_lookup} = PutApiSpec.get_spec_and_operation_lookup(conn)
operation = operation_lookup[operation_id]
cast_opts = opts |> Map.take([:replace_params]) |> Map.to_list()
content_type =
case Conn.get_req_header(conn, "content-type") do
[header_value | _] ->
@ -44,7 +46,7 @@ def call(conn, %{operation_id: operation_id, render_error: render_error}) do
conn = Conn.put_private(conn, :operation_id, operation_id)
case cast_and_validate(spec, operation, conn, content_type, strict?()) do
case cast_and_validate(spec, operation, conn, content_type, strict?(), cast_opts) do
{:ok, conn} ->
conn
@ -94,11 +96,11 @@ def call(
def call(conn, opts), do: OpenApiSpex.Plug.CastAndValidate.call(conn, opts)
defp cast_and_validate(spec, operation, conn, content_type, true = _strict) do
OpenApiSpex.cast_and_validate(spec, operation, conn, content_type)
defp cast_and_validate(spec, operation, conn, content_type, true = _strict, cast_opts) do
OpenApiSpex.cast_and_validate(spec, operation, conn, content_type, cast_opts)
end
defp cast_and_validate(spec, operation, conn, content_type, false = _strict) do
defp cast_and_validate(spec, operation, conn, content_type, false = _strict, cast_opts) do
case OpenApiSpex.cast_and_validate(spec, operation, conn, content_type) do
{:ok, conn} ->
{:ok, conn}
@ -123,7 +125,7 @@ defp cast_and_validate(spec, operation, conn, content_type, false = _strict) do
end)
conn = %Conn{conn | query_params: query_params}
OpenApiSpex.cast_and_validate(spec, operation, conn, content_type)
OpenApiSpex.cast_and_validate(spec, operation, conn, content_type, cast_opts)
end
end