Skip to content

Commit 39d9704

Browse files
authored
Enable rich_text_* elements to have an empty 'elements' property (#1576)
1 parent 7e586bd commit 39d9704

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

slack_sdk/models/basic_objects.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ def __str__(self):
1212
return f"<slack_sdk.{self.__class__.__name__}>"
1313

1414

15+
# Usually, Block Kit components do not allow an empty array for a property value, but there are some exceptions.
16+
EMPTY_ALLOWED_TYPE_AND_PROPERTY_LIST = [
17+
{"type": "rich_text_section", "property": "elements"},
18+
{"type": "rich_text_list", "property": "elements"},
19+
{"type": "rich_text_preformatted", "property": "elements"},
20+
{"type": "rich_text_quote", "property": "elements"},
21+
]
22+
23+
1524
class JsonObject(BaseObject, metaclass=ABCMeta):
1625
"""The base class for JSON serializable class objects"""
1726

@@ -51,6 +60,14 @@ def is_not_empty(self, key: str) -> bool:
5160
value = getattr(self, key, None)
5261
if value is None:
5362
return False
63+
64+
# Usually, Block Kit components do not allow an empty array for a property value, but there are some exceptions.
65+
# The following code deals with these exceptions:
66+
type_value = getattr(self, "type", None)
67+
for empty_allowed in EMPTY_ALLOWED_TYPE_AND_PROPERTY_LIST:
68+
if type_value == empty_allowed["type"] and key == empty_allowed["property"]:
69+
return True
70+
5471
has_len = getattr(value, "__len__", None) is not None
5572
if has_len: # skipcq: PYL-R1705
5673
return len(value) > 0

tests/slack_sdk/models/test_blocks.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,3 +1143,27 @@ def test_elements_are_parsed(self):
11431143
self.assertIsInstance(block.elements[3], RichTextListElement)
11441144
self.assertIsInstance(block.elements[3].elements[0], RichTextSectionElement)
11451145
self.assertIsInstance(block.elements[3].elements[0].elements[0], RichTextElementParts.Text)
1146+
1147+
def test_parsing_empty_block_elements(self):
1148+
empty_element_block = {
1149+
"block_id": "my-block",
1150+
"type": "rich_text",
1151+
"elements": [
1152+
{"type": "rich_text_section", "elements": []},
1153+
{"type": "rich_text_list", "style": "bullet", "elements": []},
1154+
{"type": "rich_text_preformatted", "elements": []},
1155+
{"type": "rich_text_quote", "elements": []},
1156+
],
1157+
}
1158+
block = RichTextBlock(**empty_element_block)
1159+
self.assertIsInstance(block.elements[0], RichTextSectionElement)
1160+
self.assertIsNotNone(block.elements[0].elements)
1161+
self.assertIsNotNone(block.elements[1].elements)
1162+
self.assertIsNotNone(block.elements[2].elements)
1163+
self.assertIsNotNone(block.elements[3].elements)
1164+
1165+
block_dict = block.to_dict()
1166+
self.assertIsNotNone(block_dict["elements"][0].get("elements"))
1167+
self.assertIsNotNone(block_dict["elements"][1].get("elements"))
1168+
self.assertIsNotNone(block_dict["elements"][2].get("elements"))
1169+
self.assertIsNotNone(block_dict["elements"][3].get("elements"))

0 commit comments

Comments
 (0)