fix: bad auto_subscribe api schema

This commit is contained in:
Zhongwen Deng 2023-01-10 21:53:54 +08:00
parent e67657d6d9
commit 5415e341fb
4 changed files with 53 additions and 27 deletions

View File

@ -51,8 +51,21 @@ max_limit() ->
list() ->
format(emqx_conf:get([auto_subscribe, topics], [])).
update(Topics) ->
update_(Topics).
update(Topics) when length(Topics) =< ?MAX_AUTO_SUBSCRIBE ->
case
emqx_conf:update(
[auto_subscribe, topics],
Topics,
#{rawconf_with_defaults => true, override_to => cluster}
)
of
{ok, #{raw_config := NewTopics}} ->
{ok, NewTopics};
{error, Reason} ->
{error, Reason}
end;
update(_Topics) ->
{error, quota_exceeded}.
post_config_update(_KeyPath, _Req, NewTopics, _OldConf, _AppEnvs) ->
Config = emqx_conf:get([auto_subscribe], #{}),
@ -95,22 +108,6 @@ format(Rule = #{topic := Topic}) when is_map(Rule) ->
nl => maps:get(nl, Rule, 0)
}.
update_(Topics) when length(Topics) =< ?MAX_AUTO_SUBSCRIBE ->
case
emqx_conf:update(
[auto_subscribe, topics],
Topics,
#{rawconf_with_defaults => true, override_to => cluster}
)
of
{ok, #{raw_config := NewTopics}} ->
{ok, NewTopics};
{error, Reason} ->
{error, Reason}
end;
update_(_Topics) ->
{error, quota_exceeded}.
update_hook() ->
update_hook(emqx_conf:get([auto_subscribe], #{})).

View File

@ -34,7 +34,7 @@
-include_lib("emqx/include/emqx_placeholder.hrl").
api_spec() ->
emqx_dashboard_swagger:spec(?MODULE).
emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}).
paths() ->
["/mqtt/auto_subscribe"].
@ -46,15 +46,15 @@ schema("/mqtt/auto_subscribe") ->
description => ?DESC(list_auto_subscribe_api),
tags => [<<"Auto Subscribe">>],
responses => #{
200 => hoconsc:ref(emqx_auto_subscribe_schema, "auto_subscribe")
200 => topics()
}
},
put => #{
description => ?DESC(update_auto_subscribe_api),
tags => [<<"Auto Subscribe">>],
'requestBody' => hoconsc:ref(emqx_auto_subscribe_schema, "auto_subscribe"),
'requestBody' => topics(),
responses => #{
200 => hoconsc:ref(emqx_auto_subscribe_schema, "auto_subscribe"),
200 => topics(),
409 => emqx_dashboard_swagger:error_codes(
[?EXCEED_LIMIT],
?DESC(update_auto_subscribe_api_response409)
@ -63,14 +63,17 @@ schema("/mqtt/auto_subscribe") ->
}
}.
topics() ->
Fields = emqx_auto_subscribe_schema:fields("auto_subscribe"),
{topics, Topics} = lists:keyfind(topics, 1, Fields),
Topics.
%%%==============================================================================================
%% api apply
auto_subscribe(get, _) ->
{200, emqx_auto_subscribe:list()};
auto_subscribe(put, #{body := #{}}) ->
{400, #{code => ?BAD_REQUEST, message => <<"Request body required">>}};
auto_subscribe(put, #{body := Params}) ->
case emqx_auto_subscribe:update(Params) of
auto_subscribe(put, #{body := Topics}) when is_list(Topics) ->
case emqx_auto_subscribe:update(Topics) of
{error, quota_exceeded} ->
Message = list_to_binary(
io_lib:format(

View File

@ -151,6 +151,32 @@ t_update(_) ->
ResponseMap = emqx_json:decode(Response, [return_maps]),
?assertEqual(1, erlang:length(ResponseMap)),
BadBody1 = #{topic => ?TOPIC_S},
?assertMatch(
{error, {"HTTP/1.1", 400, "Bad Request"}},
emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, BadBody1)
),
BadBody2 = [#{topic => ?TOPIC_S, qos => 3}],
?assertMatch(
{error, {"HTTP/1.1", 400, "Bad Request"}},
emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, BadBody2)
),
BadBody3 = [#{topic => ?TOPIC_S, rh => 10}],
?assertMatch(
{error, {"HTTP/1.1", 400, "Bad Request"}},
emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, BadBody3)
),
BadBody4 = [#{topic => ?TOPIC_S, rap => -1}],
?assertMatch(
{error, {"HTTP/1.1", 400, "Bad Request"}},
emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, BadBody4)
),
BadBody5 = [#{topic => ?TOPIC_S, nl => -1}],
?assertMatch(
{error, {"HTTP/1.1", 400, "Bad Request"}},
emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, BadBody5)
),
{ok, Client} = emqtt:start_link(#{username => ?CLIENT_USERNAME, clientid => ?CLIENT_ID}),
{ok, _} = emqtt:connect(Client),
timer:sleep(100),

View File

@ -705,7 +705,7 @@ typename_to_spec("service_account_json()", _Mod) ->
typename_to_spec("#{" ++ _, Mod) ->
typename_to_spec("map()", Mod);
typename_to_spec("qos()", _Mod) ->
#{type => string, enum => [0, 1, 2]};
#{type => integer, minimum => 0, maximum => 2, example => 0};
typename_to_spec("{binary(), binary()}", _Mod) ->
#{type => object, example => #{}};
typename_to_spec("comma_separated_list()", _Mod) ->