Merge pull request #9155 from zmstone/1014-publish-api-should-not-echo

fix(api): publish API only returns message ID
This commit is contained in:
Zaiming (Stone) Shi 2022-10-14 11:52:17 +02:00 committed by GitHub
commit 1686ca26fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 27 deletions

View File

@ -5,6 +5,9 @@
* Add `cert_common_name` and `cert_subject` placeholder support for authz_http and authz_mongo.[#8973](https://github.com/emqx/emqx/pull/8973) * Add `cert_common_name` and `cert_subject` placeholder support for authz_http and authz_mongo.[#8973](https://github.com/emqx/emqx/pull/8973)
* Use milliseconds internally in emqx_delayed to store the publish time, improving precision.[#9060](https://github.com/emqx/emqx/pull/9060) * Use milliseconds internally in emqx_delayed to store the publish time, improving precision.[#9060](https://github.com/emqx/emqx/pull/9060)
* More rigorous checking of flapping to improve stability of the system. [#9136](https://github.com/emqx/emqx/pull/9136) * More rigorous checking of flapping to improve stability of the system. [#9136](https://github.com/emqx/emqx/pull/9136)
* No message(s) echo for the message publish APIs [#9155](https://github.com/emqx/emqx/pull/9155)
Prior to this fix, the message publish APIs (`api/v5/publish` and `api/v5/publish/bulk`) echos the message back to the client in HTTP body.
This change fixed it to only send back the message ID.
## Bug fixes ## Bug fixes

View File

@ -109,15 +109,15 @@ fields(publish_message_info) ->
[ [
{id, {id,
hoconsc:mk(binary(), #{ hoconsc:mk(binary(), #{
desc => <<"Internal Message ID">> desc => <<"A globally unique message ID">>
})} })}
] ++ fields(message). ].
publish(post, #{body := Body}) -> publish(post, #{body := Body}) ->
case message(Body) of case message(Body) of
{ok, Message} -> {ok, Message} ->
_ = emqx_mgmt:publish(Message), _ = emqx_mgmt:publish(Message),
{200, format_message(Message)}; {200, format_message_response(Message)};
{error, R} -> {error, R} ->
{400, 'BAD_REQUEST', to_binary(R)} {400, 'BAD_REQUEST', to_binary(R)}
end. end.
@ -126,7 +126,7 @@ publish_batch(post, #{body := Body}) ->
case messages(Body) of case messages(Body) of
{ok, Messages} -> {ok, Messages} ->
_ = [emqx_mgmt:publish(Message) || Message <- Messages], _ = [emqx_mgmt:publish(Message) || Message <- Messages],
{200, format_message(Messages)}; {200, format_message_response(Messages)};
{error, R} -> {error, R} ->
{400, 'BAD_REQUEST', to_binary(R)} {400, 'BAD_REQUEST', to_binary(R)}
end. end.
@ -167,21 +167,10 @@ messages([MessageMap | List], Res) ->
{error, R} {error, R}
end. end.
format_message(Messages) when is_list(Messages) -> format_message_response(Messages) when is_list(Messages) ->
[format_message(Message) || Message <- Messages]; [format_message_response(Message) || Message <- Messages];
format_message(#message{ format_message_response(#message{id = ID}) ->
id = ID, qos = Qos, from = From, topic = Topic, payload = Payload, flags = Flags #{id => emqx_guid:to_hexstr(ID)}.
}) ->
#{
id => emqx_guid:to_hexstr(ID),
qos => Qos,
topic => Topic,
payload => Payload,
retain => maps:get(retain, Flags, false),
clientid => to_binary(From)
}.
to_binary(Data) when is_binary(Data) -> to_binary(Term) ->
Data; list_to_binary(io_lib:format("~p", [Term])).
to_binary(Data) ->
list_to_binary(io_lib:format("~p", [Data])).

View File

@ -47,8 +47,10 @@ t_publish_api(_) ->
Path = emqx_mgmt_api_test_util:api_path(["publish"]), Path = emqx_mgmt_api_test_util:api_path(["publish"]),
Auth = emqx_mgmt_api_test_util:auth_header_(), Auth = emqx_mgmt_api_test_util:auth_header_(),
Body = #{topic => ?TOPIC1, payload => Payload}, Body = #{topic => ?TOPIC1, payload => Payload},
{ok, _} = emqx_mgmt_api_test_util:request_api(post, Path, "", Auth, Body), {ok, Response} = emqx_mgmt_api_test_util:request_api(post, Path, "", Auth, Body),
?assertEqual(receive_assert(?TOPIC1, 0, Payload), ok), ResponseMap = emqx_json:decode(Response, [return_maps]),
?assertEqual([<<"id">>], maps:keys(ResponseMap)),
?assertEqual(ok, receive_assert(?TOPIC1, 0, Payload)),
emqtt:disconnect(Client). emqtt:disconnect(Client).
t_publish_bulk_api(_) -> t_publish_bulk_api(_) ->
@ -63,10 +65,16 @@ t_publish_bulk_api(_) ->
Auth = emqx_mgmt_api_test_util:auth_header_(), Auth = emqx_mgmt_api_test_util:auth_header_(),
Body = [#{topic => ?TOPIC1, payload => Payload}, #{topic => ?TOPIC2, payload => Payload}], Body = [#{topic => ?TOPIC1, payload => Payload}, #{topic => ?TOPIC2, payload => Payload}],
{ok, Response} = emqx_mgmt_api_test_util:request_api(post, Path, "", Auth, Body), {ok, Response} = emqx_mgmt_api_test_util:request_api(post, Path, "", Auth, Body),
ResponseMap = emqx_json:decode(Response, [return_maps]), ResponseList = emqx_json:decode(Response, [return_maps]),
?assertEqual(2, erlang:length(ResponseMap)), ?assertEqual(2, erlang:length(ResponseList)),
?assertEqual(receive_assert(?TOPIC1, 0, Payload), ok), lists:foreach(
?assertEqual(receive_assert(?TOPIC2, 0, Payload), ok), fun(ResponseMap) ->
?assertEqual([<<"id">>], maps:keys(ResponseMap))
end,
ResponseList
),
?assertEqual(ok, receive_assert(?TOPIC1, 0, Payload)),
?assertEqual(ok, receive_assert(?TOPIC2, 0, Payload)),
emqtt:disconnect(Client). emqtt:disconnect(Client).
receive_assert(Topic, Qos, Payload) -> receive_assert(Topic, Qos, Payload) ->