open-api-spex / open_api_spex

Open API Specifications for Elixir Plug applications
Mozilla Public License 2.0
681 stars 177 forks source link

Params by plug convention should contain merged path, body and query parameters #588

Closed hamir-suspect closed 1 month ago

hamir-suspect commented 6 months ago

According to Plug documentation conn.params is

the request params, the result of merging the :path_params on top of :body_params on top of :query_params

When using OpenApiSpex.Plug.CastAndValidate plug the params will overwrite only with params from :path | :query | :header | :cookie meaning that there is never parameters from request_body there.

This should be clearly stated in CastAndValidate plug like it is for cast plug as it can cause some confusion.

mbuhot commented 1 month ago

The library was initially implemented with all params merged into conn.params as per the docs. Unfortunately it is problematic with struct based body schemas.

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.