Skip to content

Commit 430d735

Browse files
authored
Fix #721 Passing a global dict object without channel prop can cause issues among requests (#722)
1 parent 2100249 commit 430d735

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

slack_bolt/context/say/async_say.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Optional, Union, Dict, Sequence
22

33
from slack_bolt.context.say.internals import _can_say
4+
from slack_bolt.util.utils import create_copy
45
from slack_sdk.models.attachments import Attachment
56
from slack_sdk.models.blocks import Block
67
from slack_sdk.web.async_client import AsyncWebClient
@@ -45,7 +46,7 @@ async def __call__(
4546
**kwargs,
4647
)
4748
elif isinstance(text_or_whole_response, dict):
48-
message: dict = text_or_whole_response
49+
message: dict = create_copy(text_or_whole_response)
4950
if "channel" not in message:
5051
message["channel"] = channel or self.channel
5152
return await self.client.chat_postMessage(**message)

slack_bolt/context/say/say.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from slack_sdk.web import SlackResponse
77

88
from slack_bolt.context.say.internals import _can_say
9+
from slack_bolt.util.utils import create_copy
910

1011

1112
class Say:
@@ -46,7 +47,7 @@ def __call__(
4647
**kwargs,
4748
)
4849
elif isinstance(text_or_whole_response, dict):
49-
message: dict = text_or_whole_response
50+
message: dict = create_copy(text_or_whole_response)
5051
if "channel" not in message:
5152
message["channel"] = channel or self.channel
5253
return self.client.chat_postMessage(**message)

tests/slack_bolt/context/test_say.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,16 @@ def test_say_invalid(self):
4343
say = Say(client=self.web_client, channel="C111")
4444
with pytest.raises(ValueError):
4545
say([])
46+
47+
def test_say_shared_dict_as_arg(self):
48+
# this shared dict object must not be modified by say method
49+
shared_template_dict = {"text": "Hi there!"}
50+
say = Say(client=self.web_client, channel="C111")
51+
response: SlackResponse = say(shared_template_dict)
52+
assert response.status_code == 200
53+
assert shared_template_dict.get("channel") is None
54+
55+
say = Say(client=self.web_client, channel="C222")
56+
response: SlackResponse = say(shared_template_dict)
57+
assert response.status_code == 200
58+
assert shared_template_dict.get("channel") is None

tests/slack_bolt_async/context/test_async_say.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,17 @@ async def test_say_invalid(self):
5353
say = AsyncSay(client=self.web_client, channel="C111")
5454
with pytest.raises(ValueError):
5555
await say([])
56+
57+
@pytest.mark.asyncio
58+
async def test_say_shared_dict_as_arg(self):
59+
# this shared dict object must not be modified by say method
60+
shared_template_dict = {"text": "Hi there!"}
61+
say = AsyncSay(client=self.web_client, channel="C111")
62+
response: AsyncSlackResponse = await say(shared_template_dict)
63+
assert response.status_code == 200
64+
assert shared_template_dict.get("channel") is None
65+
66+
say = AsyncSay(client=self.web_client, channel="C222")
67+
response: AsyncSlackResponse = await say(shared_template_dict)
68+
assert response.status_code == 200
69+
assert shared_template_dict.get("channel") is None

0 commit comments

Comments
 (0)