Skip to content

Commit

Permalink
Enable rich_text_section element to have an empty 'elements' property
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Oct 28, 2024
1 parent 5c8f595 commit 00d919e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
14 changes: 14 additions & 0 deletions slack_sdk/models/basic_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ def __str__(self):
return f"<slack_sdk.{self.__class__.__name__}>"


# Usually, Block Kit components do not allow an empty array for a property value,
# but there are some exceptions (as of October 2024, only rich_text_section's elements property).
EMPTY_ALLOWED_TYPE_AND_PROPERTY_LIST = [{"type": "rich_text_section", "property": "elements"}]


class JsonObject(BaseObject, metaclass=ABCMeta):
"""The base class for JSON serializable class objects"""

Expand Down Expand Up @@ -51,6 +56,15 @@ def is_not_empty(self, key: str) -> bool:
value = getattr(self, key, None)
if value is None:
return False

# Usually, Block Kit components do not allow an empty array for a property value,
# but there are some exceptions (as of October 2024, only rich_text_section's elements property).
# The following code deals with this exception:
type_value = getattr(self, "type", None)
for empty_allowed in EMPTY_ALLOWED_TYPE_AND_PROPERTY_LIST:
if type_value == empty_allowed["type"] and key == empty_allowed["property"]:
return True

has_len = getattr(value, "__len__", None) is not None
if has_len: # skipcq: PYL-R1705
return len(value) > 0
Expand Down
18 changes: 18 additions & 0 deletions tests/slack_sdk/models/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1143,3 +1143,21 @@ def test_elements_are_parsed(self):
self.assertIsInstance(block.elements[3], RichTextListElement)
self.assertIsInstance(block.elements[3].elements[0], RichTextSectionElement)
self.assertIsInstance(block.elements[3].elements[0].elements[0], RichTextElementParts.Text)

def test_parsing_empty_block_elements(self):
empty_element_block = {
"block_id": "my-block",
"type": "rich_text",
"elements": [
{
"type": "rich_text_section",
"elements": [],
},
],
}
block = RichTextBlock(**empty_element_block)
self.assertIsInstance(block.elements[0], RichTextSectionElement)
self.assertIsNotNone(block.elements[0].elements)

block_dict = block.to_dict()
self.assertIsNotNone(block_dict["elements"][0].get("elements"))

0 comments on commit 00d919e

Please sign in to comment.