-
Notifications
You must be signed in to change notification settings - Fork 197
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
Params by plug convention should contain merged path, body and query parameters #588
Comments
The library was initially implemented with all params merged into If your body schema is a struct, the adding additional query params creates a struct with undeclared fields set. It technically works since structs are just maps, but it is confusing and may cause type errors in future elixir versions. |
If I may, I'd like to add a +1 here that this is confusing and I'd argue to revert it. I don't care whether the |
FYI for whomever is interested, this plug merges the params again. Just add it behind the defmodule JadeWeb.Plugs.MergeParamsFromOpenApiSpex do
@moduledoc """
Merges the `body_params` into the `params` field if the request was validated by OpenApiSpex.
For context, see: https://github.com/open-api-spex/open_api_spex/issues/588
"""
@behaviour Plug
@impl Plug
def init(opts), do: opts
@impl Plug
def call(
%Plug.Conn{
params: params,
body_params: body_params,
private: private
} = conn,
_opts
)
when is_map_key(private, :open_api_spex) do
body_params = if is_struct(body_params), do: Map.from_struct(body_params), else: body_params
params = Map.merge(params, body_params)
Map.put(conn, :params, params)
end
def call(conn, _opts), do: conn
end |
You can also use open_api_spex/lib/open_api_spex/plug/cast_and_validate.ex Lines 21 to 23 in abe90e3
|
Ah interesting! I'd still like to use the casted parameters though, just not split between params and body_params. So, I'm okay with my plug mentioned above. |
We've also faced this problem and in our controllers |
According to Plug documentation
conn.params
isWhen using
OpenApiSpex.Plug.CastAndValidate
plug the params will overwrite only with params from:path | :query | :header | :cookie
meaning that there is never parameters fromrequest_body
there.This should be clearly stated in
CastAndValidate
plug like it is for cast plug as it can cause some confusion.The text was updated successfully, but these errors were encountered: