Skip to content
Merged
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
17 changes: 16 additions & 1 deletion src/minirest_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,27 @@ do_authorize(_Request, _Handler) ->
do_parse_params(Request) ->
Params = #{
bindings => cowboy_req:bindings(Request),
query_string => maps:from_list(cowboy_req:parse_qs(Request)),
query_string => parse_qs(Request),
headers => cowboy_req:headers(Request),
body => #{}
},
do_read_body(Request, Params).

parse_qs(Request) ->
lists:foldl(
fun({K, V}, MapAcc) ->
maps:update_with(
K,
fun([_|_] = OldV) -> [V | OldV];

@savonarola savonarola Mar 7, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I remember (from my times being a "web developer" 😅), the common convention understood by many frameworks is to add [] when we want an array, that is, converting a[]=1&a[]=2&a[]=3 to #{a => [1,2,3]} (and a[b]=1&a[c]=2 to #{a => #{b => 1, c => 2}}).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current implementation actually corresponds to the convention in OpenAPI 3 with style = form and explode = true.

That said, if the plan is to rely on API spec later, and for example taking only the last element if the respective spec says that the parameter is not actually an array, then it sounds fine I would say.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I remember (from my times being a "web developer" 😅), the common convention understood by many frameworks is to add [] when we want an array, that is, converting a[]=1&a[]=2&a[]=3 to #{a => [1,2,3]} (and a[b]=1&a[c]=2 to #{a => #{b => 1, c => 2}}).

this is mind blowing

@SergeTupchiy SergeTupchiy Mar 13, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think repeating QS params (without []) to represent lists is perfectly fine. My only concern is backward compatibility, which can be solved either in EMQX or probably in minirest itself (but we would need to pass an option to parse repeated params to lists).

Or we can simply ignore it: if someone is sending us repeated params while we don't expect it, it's probably OK to respond with 400 Bad Request.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's how I propose to solve backward compatibility: https://github.com/SergeTupchiy/emqx/blob/EMQX-11900-multiple-clientid-username-clients-API/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl#L463-L520

Drawbacks:

  • maybe a bit complex/over-engineered (for a quite simple task)

Advantages:

  • it relies on hocon types to decide on how to wrap/unwrap Qs params, no need to introduce any new meta field to differentiate arrays from single QS params.
  • developers can continue defining QS params schemas as before or they can simply add hocon:array(_):
hoconsc:mk(hoconsc:array(binary()), #{
   in => query,
   required => false,
   desc => <<"Client ID">>
})}
  • can handle more complex types like union(array(Type), SingleType)
  • respects custom converters, so that it's possible to define an array with a custom encoding, e. g. to parse coma separated lists

(OldV) -> [V, OldV]
end,
V,
MapAcc)
end,
#{},
cowboy_req:parse_qs(Request)
).

do_read_body(Request, Params) ->
case cowboy_req:has_body(Request) of
true ->
Expand Down
9 changes: 8 additions & 1 deletion test/minirest_handler_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ all() ->
[
t_lazy_body,
t_binary_body,
t_flex_error
t_flex_error,
t_qs_params
].

init_per_suite(Config) ->
Expand Down Expand Up @@ -69,5 +70,11 @@ t_flex_error(_Config) ->
#{<<"code">> := _, <<"message">> := _, <<"hint">> := _},
jsx:decode(iolist_to_binary(Body), [return_maps])).

t_qs_params(_Config) ->
?assertMatch(
{ok, {{_Version, 200, _Status}, _Headers, "OK"}},
httpc:request(address() ++ "/qs_params?single=foo&array=foo&array=bar")
).

address() ->
"http://localhost:" ++ integer_to_list(?PORT).
24 changes: 22 additions & 2 deletions test/minirest_test_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@

-export([lazy_body/2,
binary_body/2,
flex_error/2]).
flex_error/2,
qs_params/2]).

api_spec() ->
{
[lazy_body(),
binary_body(),
flex_error()],
flex_error(),
qs_params()],
[]
}.

Expand Down Expand Up @@ -57,6 +59,20 @@ binary_body() ->
},
{"/binary_body", MetaData, binary_body}.


qs_params() ->
MetaData = #{
get => #{
description => "parse QS params",
responses => #{
<<"200">> => #{
content => #{
'text/plain' => #{
schema => #{
type => string}}}}}}
},
{"/qs_params", MetaData, qs_params}.

flex_error() ->
MetaData = #{
get => #{
Expand All @@ -78,5 +94,9 @@ binary_body(get, _) ->
Body = <<"alldataatonce">>,
{200, #{<<"content-type">> => <<"test/plain">>}, Body}.

qs_params(get, #{query_string := Qs}) ->
#{<<"single">> := <<"foo">>, <<"array">> := [<<"bar">>, <<"foo">>]} = Qs,
{200, #{<<"content-type">> => <<"test/plain">>}, <<"OK">>}.

flex_error(get, _) ->
{400, #{message => <<"boom">>, code => 'BAD_REQUEST', hint => <<"something went wrong">>}}.