From 0817761aeeaad3398d06d0e6677f90ef16e4a953 Mon Sep 17 00:00:00 2001 From: zhanghongtong Date: Thu, 23 Jul 2020 15:46:53 +0800 Subject: [PATCH] fix(emqx_channel): EMQ X replaces MQTT properties when node connects using "topic-alias-maximum" property --- src/emqx_channel.erl | 11 +++++++---- test/emqx_channel_SUITE.erl | 20 +++++++++++++++++--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/emqx_channel.erl b/src/emqx_channel.erl index 9bf13b3f2..c0a22474b 100644 --- a/src/emqx_channel.erl +++ b/src/emqx_channel.erl @@ -1212,14 +1212,17 @@ process_alias(_Packet, Channel) -> {ok, Channel}. %% Packing Topic Alias packing_alias(Packet = #mqtt_packet{ - variable = #mqtt_packet_publish{topic_name = Topic} = Publish + variable = #mqtt_packet_publish{ + topic_name = Topic, + properties = Prop + } = Publish }, Channel = ?IS_MQTT_V5 = #channel{topic_aliases = TopicAliases, alias_maximum = Limits}) -> case find_alias(outbound, Topic, TopicAliases) of - {ok, AliasId} -> + {ok, AliasId} -> NPublish = Publish#mqtt_packet_publish{ topic_name = <<>>, - properties = #{'Topic-Alias' => AliasId} + properties = maps:merge(Prop, #{'Topic-Alias' => AliasId}) }, {Packet#mqtt_packet{variable = NPublish}, Channel}; error -> @@ -1232,7 +1235,7 @@ packing_alias(Packet = #mqtt_packet{ NChannel = Channel#channel{topic_aliases = NTopicAliases}, NPublish = Publish#mqtt_packet_publish{ topic_name = Topic, - properties = #{'Topic-Alias' => AliasId} + properties = maps:merge(Prop, #{'Topic-Alias' => AliasId}) }, {Packet#mqtt_packet{variable = NPublish}, NChannel}; false -> {Packet, Channel} diff --git a/test/emqx_channel_SUITE.erl b/test/emqx_channel_SUITE.erl index 8a48b578f..bb6374d8c 100644 --- a/test/emqx_channel_SUITE.erl +++ b/test/emqx_channel_SUITE.erl @@ -554,15 +554,29 @@ t_process_alias(_) -> emqx_channel:process_alias(#mqtt_packet{variable = Publish}, Channel). t_packing_alias(_) -> - Packet1 = #mqtt_packet{variable = #mqtt_packet_publish{topic_name = <<"x">>}}, + Packet1 = #mqtt_packet{variable = #mqtt_packet_publish{ + topic_name = <<"x">>, + properties = #{'User-Property' => [{<<"k">>, <<"v">>}]} + }}, Packet2 = #mqtt_packet{variable = #mqtt_packet_publish{topic_name = <<"y">>}}, Channel = emqx_channel:set_field(alias_maximum, #{outbound => 1}, channel()), {RePacket1, NChannel1} = emqx_channel:packing_alias(Packet1, Channel), - ?assertEqual(#mqtt_packet{variable = #mqtt_packet_publish{topic_name = <<"x">>, properties = #{'Topic-Alias' => 1}}}, RePacket1), + ?assertEqual(#mqtt_packet{variable = #mqtt_packet_publish{ + topic_name = <<"x">>, + properties = #{ + 'Topic-Alias' => 1, + 'User-Property' => [{<<"k">>, <<"v">>}] + } + }}, RePacket1), {RePacket2, NChannel2} = emqx_channel:packing_alias(Packet1, NChannel1), - ?assertEqual(#mqtt_packet{variable = #mqtt_packet_publish{topic_name = <<>>, properties = #{'Topic-Alias' => 1}}}, RePacket2), + ?assertEqual(#mqtt_packet{variable = #mqtt_packet_publish{ + topic_name = <<>>, + properties = #{ + 'Topic-Alias' => 1, + 'User-Property' => [{<<"k">>, <<"v">>}] + }}}, RePacket2), {RePacket3, _} = emqx_channel:packing_alias(Packet2, NChannel2), ?assertEqual(#mqtt_packet{variable = #mqtt_packet_publish{topic_name = <<"y">>, properties = #{}}}, RePacket3),