Skip to content

Commit

Permalink
Add more patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Oct 28, 2024
1 parent 00d919e commit e6548d3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
15 changes: 9 additions & 6 deletions slack_sdk/models/basic_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ 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"}]
# Usually, Block Kit components do not allow an empty array for a property value, but there are some exceptions.
EMPTY_ALLOWED_TYPE_AND_PROPERTY_LIST = [
{"type": "rich_text_section", "property": "elements"},
{"type": "rich_text_list", "property": "elements"},
{"type": "rich_text_preformatted", "property": "elements"},
{"type": "rich_text_quote", "property": "elements"},
]


class JsonObject(BaseObject, metaclass=ABCMeta):
Expand Down Expand Up @@ -57,9 +61,8 @@ def is_not_empty(self, key: str) -> bool:
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:
# Usually, Block Kit components do not allow an empty array for a property value, but there are some exceptions.
# The following code deals with these exceptions:
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"]:
Expand Down
14 changes: 10 additions & 4 deletions tests/slack_sdk/models/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,15 +1149,21 @@ def test_parsing_empty_block_elements(self):
"block_id": "my-block",
"type": "rich_text",
"elements": [
{
"type": "rich_text_section",
"elements": [],
},
{"type": "rich_text_section", "elements": []},
{"type": "rich_text_list", "style": "bullet", "elements": []},
{"type": "rich_text_preformatted", "elements": []},
{"type": "rich_text_quote", "elements": []},
],
}
block = RichTextBlock(**empty_element_block)
self.assertIsInstance(block.elements[0], RichTextSectionElement)
self.assertIsNotNone(block.elements[0].elements)
self.assertIsNotNone(block.elements[1].elements)
self.assertIsNotNone(block.elements[2].elements)
self.assertIsNotNone(block.elements[3].elements)

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

0 comments on commit e6548d3

Please sign in to comment.