Skip to content

Commit aabf2d0

Browse files
paul-griffithRodneyU215
authored andcommitted
Update BlockAttachment to not send invalid JSON due to fields attribute (#473)
1 parent 557b038 commit aabf2d0

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

slack/web/classes/attachments.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,14 @@ def __init__(self, *, blocks: List[Block], color: Optional[str] = None):
211211
super().__init__(text="", color=color)
212212
self.blocks = list(blocks)
213213

214+
@JsonValidator("fields attribute cannot be populated on BlockAttachment")
215+
def fields_attribute_absent(self):
216+
return not self.fields
217+
214218
def to_dict(self) -> dict:
215219
json = super().to_dict()
216220
json.update({"blocks": extract_json(self.blocks)})
221+
del json["fields"] # cannot supply fields and blocks at the same time
217222
return json
218223

219224

tests/web/classes/test_attachments.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
from slack.errors import SlackObjectFormationError
44
from slack.web.classes.actions import ActionButton, ActionLinkButton
5+
from slack.web.classes.blocks import SectionBlock, ImageBlock
56
from slack.web.classes.attachments import (
67
Attachment,
8+
BlockAttachment,
79
AttachmentField,
810
InteractiveAttachment,
911
)
@@ -206,3 +208,16 @@ def test_actions_length(self):
206208
InteractiveAttachment(
207209
text="some text", callback_id="abc123", actions=actions
208210
).to_dict(),
211+
212+
213+
class BlockAttachmentTests(unittest.TestCase):
214+
def test_basic_json(self):
215+
blocks = [
216+
SectionBlock(text="Some text"),
217+
ImageBlock(image_url="image.jpg", alt_text="an image")
218+
]
219+
220+
self.assertDictEqual(
221+
BlockAttachment(blocks=blocks).to_dict(), {"blocks": [b.to_dict() for b in blocks]}
222+
)
223+

0 commit comments

Comments
 (0)