Skip to content

Commit a30d9af

Browse files
committed
Allow no routing key
to be set in both target address and subject.
1 parent ed99d1d commit a30d9af

File tree

2 files changed

+21
-41
lines changed

2 files changed

+21
-41
lines changed

deps/rabbit/src/rabbit_amqp_session.erl

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,10 +1592,9 @@ incoming_link_transfer(
15921592
_ -> Anns0#{?ANN_ROUTING_KEYS => [LinkRKey]}
15931593
end,
15941594
Mc0 = mc:init(mc_amqp, Sections, Anns),
1595-
Mc = rabbit_message_interceptor:intercept(Mc0),
1595+
Mc1 = rabbit_message_interceptor:intercept(Mc0),
1596+
{Mc, RoutingKey} = ensure_routing_key(Mc1),
15961597
check_user_id(Mc, User),
1597-
RoutingKeys = mc:routing_keys(Mc),
1598-
RoutingKey = routing_key(RoutingKeys, XName),
15991598
messages_received(Settled),
16001599
case rabbit_exchange:lookup(XName) of
16011600
{ok, Exchange} ->
@@ -1642,6 +1641,20 @@ incoming_link_transfer(
16421641
{error, [Disposition, Detach]}
16431642
end.
16441643

1644+
ensure_routing_key(Mc) ->
1645+
case mc:routing_keys(Mc) of
1646+
[RoutingKey] ->
1647+
{Mc, RoutingKey};
1648+
[] ->
1649+
%% Set the default routing key of AMQP 0.9.1 'basic.publish'{}.
1650+
%% For example, when the client attached to target /exchange/amq.fanout and sends a
1651+
%% message without setting a 'subject' in the message properties, the routing key is
1652+
%% ignored during routing, but receiving code paths still expect some routing key to be set.
1653+
DefaultRoutingKey = <<"">>,
1654+
Mc1 = mc:set_annotation(?ANN_ROUTING_KEYS, [DefaultRoutingKey], Mc),
1655+
{Mc1, DefaultRoutingKey}
1656+
end.
1657+
16451658
process_routing_confirm([], _SenderSettles = true, _, U) ->
16461659
rabbit_global_counters:messages_unroutable_dropped(?PROTOCOL, 1),
16471660
{U, []};
@@ -2159,13 +2172,6 @@ remove_link_from_outgoing_unsettled_map(Ctag, Map)
21592172
Acc
21602173
end, {Map, []}, Map).
21612174

2162-
routing_key([RoutingKey], _XName) ->
2163-
RoutingKey;
2164-
routing_key([], XName) ->
2165-
protocol_error(?V_1_0_AMQP_ERROR_INVALID_FIELD,
2166-
"Publishing to ~ts failed since no routing key was provided",
2167-
[rabbit_misc:rs(XName)]).
2168-
21692175
messages_received(Settled) ->
21702176
rabbit_global_counters:messages_received(?PROTOCOL, 1),
21712177
case Settled of

deps/rabbit/test/amqp_client_SUITE.erl

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ groups() ->
6161
link_target_classic_queue_deleted,
6262
link_target_quorum_queue_deleted,
6363
target_queues_deleted_accepted,
64-
no_routing_key,
6564
events,
6665
sync_get_unsettled_classic_queue,
6766
sync_get_unsettled_quorum_queue,
@@ -320,9 +319,8 @@ sender_settle_mode_unsettled_fanout(Config) ->
320319
OpnConf = connection_config(Config),
321320
{ok, Connection} = amqp10_client:open_connection(OpnConf),
322321
{ok, Session} = amqp10_client:begin_session_sync(Connection),
323-
Address = <<"/exchange/amq.fanout/ignored">>,
324-
{ok, Sender} = amqp10_client:attach_sender_link(
325-
Session, <<"test-sender">>, Address, unsettled),
322+
Address = <<"/exchange/amq.fanout">>,
323+
{ok, Sender} = amqp10_client:attach_sender_link(Session, <<"test-sender">>, Address, unsettled),
326324
ok = wait_for_credit(Sender),
327325

328326
%% Send many messages aync.
@@ -796,7 +794,7 @@ multiple_sessions(Config) ->
796794
ok = rabbit_ct_client_helpers:close_channel(Ch),
797795

798796
%% Send on each session.
799-
TargetAddr = <<"/exchange/amq.fanout/ignored">>,
797+
TargetAddr = <<"/exchange/amq.fanout">>,
800798
{ok, Sender1} = amqp10_client:attach_sender_link_sync(
801799
Session1, <<"sender link 1">>, TargetAddr, settled, configuration),
802800
ok = wait_for_credit(Sender1),
@@ -1087,30 +1085,6 @@ rabbit_queue_type_deliver_to_q1(Qs, Msg, Opts, QTypeState) ->
10871085
1 = length(Q1),
10881086
meck:passthrough([Q1, Msg, Opts, QTypeState]).
10891087

1090-
%% Set routing key neither in target address nor in message subject.
1091-
no_routing_key(Config) ->
1092-
OpnConf = connection_config(Config),
1093-
{ok, Connection} = amqp10_client:open_connection(OpnConf),
1094-
{ok, Session} = amqp10_client:begin_session_sync(Connection),
1095-
Address = <<"/exchange/amq.direct">>,
1096-
{ok, Sender} = amqp10_client:attach_sender_link(
1097-
Session, <<"test-sender">>, Address),
1098-
ok = wait_for_credit(Sender),
1099-
Msg = amqp10_msg:new(<<0>>, <<1>>, true),
1100-
ok = amqp10_client:send_msg(Sender, Msg),
1101-
receive
1102-
{amqp10_event,
1103-
{session, Session,
1104-
{ended,
1105-
#'v1_0.error'{
1106-
condition = ?V_1_0_AMQP_ERROR_INVALID_FIELD,
1107-
description = {utf8, <<"Publishing to exchange 'amq.direct' in vhost '/' "
1108-
"failed since no routing key was provided">>}}}}} -> ok
1109-
after 5000 -> flush(missing_ended),
1110-
ct:fail("did not receive expected error")
1111-
end,
1112-
ok = amqp10_client:close_connection(Connection).
1113-
11141088
events(Config) ->
11151089
ok = event_recorder:start(Config),
11161090

@@ -2085,7 +2059,7 @@ last_queue_confirms(Config) ->
20852059
{ok, Connection} = amqp10_client:open_connection(OpnConf),
20862060
{ok, Session} = amqp10_client:begin_session_sync(Connection),
20872061

2088-
AddressFanout = <<"/exchange/amq.fanout/ignored">>,
2062+
AddressFanout = <<"/exchange/amq.fanout">>,
20892063
{ok, SenderFanout} = amqp10_client:attach_sender_link(
20902064
Session, <<"sender-1">>, AddressFanout, unsettled),
20912065
ok = wait_for_credit(SenderFanout),
@@ -2160,7 +2134,7 @@ target_queue_deleted(Config) ->
21602134
{ok, Connection} = amqp10_client:open_connection(OpnConf),
21612135
{ok, Session} = amqp10_client:begin_session_sync(Connection),
21622136

2163-
Address = <<"/exchange/amq.fanout/ignored">>,
2137+
Address = <<"/exchange/amq.fanout">>,
21642138
{ok, Sender} = amqp10_client:attach_sender_link(
21652139
Session, <<"sender">>, Address, unsettled),
21662140
ok = wait_for_credit(Sender),

0 commit comments

Comments
 (0)