From 98868c36151cff660328ec4bfd9bb757036bfa17 Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi <16166434+thalesmg@users.noreply.github.com> Date: Thu, 7 Nov 2024 13:38:20 -0300 Subject: [PATCH] fix(publish api): return 200 for delayed messages Fixes https://emqx.atlassian.net/browse/EMQX-12780 Fixes https://github.com/emqx/emqx/issues/13215 --- .../src/emqx_mgmt_api_publish.erl | 2 ++ .../test/emqx_mgmt_api_publish_SUITE.erl | 21 +++++++++++++++++++ changes/ce/fix-14182.en.md | 1 + 3 files changed, 24 insertions(+) create mode 100644 changes/ce/fix-14182.en.md diff --git a/apps/emqx_management/src/emqx_mgmt_api_publish.erl b/apps/emqx_management/src/emqx_mgmt_api_publish.erl index 9b5de578fc6..dd8fda9d414 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_publish.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_publish.erl @@ -272,6 +272,8 @@ is_ok_deliver({_NodeOrShare, _MatchedTopic, {error, _}}) -> false. %% No preceding payload format indicator to compare against. %% Content-Type check should be done at HTTP layer but not here. %% 153 Payload format invalid 400 +publish_result_to_http_reply(#message{topic = <<"$delayed/", _/binary>>} = Message, []) -> + {?ALL_IS_WELL, make_publish_response(Message)}; publish_result_to_http_reply(_Message, []) -> %% matched no subscriber {?PARTIALLY_OK, make_publish_error_response(?RC_NO_MATCHING_SUBSCRIBERS)}; diff --git a/apps/emqx_management/test/emqx_mgmt_api_publish_SUITE.erl b/apps/emqx_management/test/emqx_mgmt_api_publish_SUITE.erl index 0e4b8aece44..d833442b669 100644 --- a/apps/emqx_management/test/emqx_mgmt_api_publish_SUITE.erl +++ b/apps/emqx_management/test/emqx_mgmt_api_publish_SUITE.erl @@ -407,6 +407,27 @@ t_publish_offline_api(_) -> ResponseMap = decode_json(Response), ?assertEqual([<<"id">>], lists:sort(maps:keys(ResponseMap))). +%% Checks that we return HTTP response status code 200 for delayed messages, even if there +%% are no subscribers at the time of publishing. +t_delayed_message_always_200({init, Config}) -> + Config; +t_delayed_message_always_200({'end', _Config}) -> + ok; +t_delayed_message_always_200(Config) when is_list(Config) -> + Request = #{ + <<"topic">> => <<"$delayed/1/t">>, + <<"payload">> => <<"delayed">> + }, + ?assertMatch( + {200, #{<<"id">> := _}}, + emqx_mgmt_api_test_util:simple_request( + post, + emqx_mgmt_api_test_util:api_path(["publish"]), + Request + ) + ), + ok. + receive_assert(Topic, Qos, Payload) -> receive {publish, Message} -> diff --git a/changes/ce/fix-14182.en.md b/changes/ce/fix-14182.en.md new file mode 100644 index 00000000000..428556acec6 --- /dev/null +++ b/changes/ce/fix-14182.en.md @@ -0,0 +1 @@ +Previously, if a delayed message was published via the `POST /publish` HTTP API, a 202 reponse with the reason code 16 ("no matching subscribers") would be returned. Now, a 200 response is sent along with the message identifier.