Skip to content

Commit

Permalink
Fix #1369 ImageBlock title parsing defaults to incorrect behaviour wh…
Browse files Browse the repository at this point in the history
…en given a string (#1374)
  • Loading branch information
seratch authored May 27, 2023
1 parent e63a94f commit 027a287
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
17 changes: 15 additions & 2 deletions slack_sdk/models/blocks/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .block_elements import ImageElement
from .block_elements import InputInteractiveElement
from .block_elements import InteractiveElement
from ...errors import SlackObjectFormationError


# -------------------------------------------------
Expand Down Expand Up @@ -216,7 +217,7 @@ def __init__(
*,
image_url: str,
alt_text: str,
title: Optional[Union[str, dict, TextObject]] = None,
title: Optional[Union[str, dict, PlainTextObject]] = None,
block_id: Optional[str] = None,
**others: dict,
):
Expand All @@ -240,7 +241,19 @@ def __init__(

self.image_url = image_url
self.alt_text = alt_text
self.title = TextObject.parse(title)
parsed_title = None
if title is not None:
if isinstance(title, str):
parsed_title = PlainTextObject(text=title)
elif isinstance(title, dict):
if title.get("type") != PlainTextObject.type:
raise SlackObjectFormationError(f"Unsupported type for title in an image block: {title.get('type')}")
parsed_title = PlainTextObject(text=title.get("text"), emoji=title.get("emoji"))
elif isinstance(title, PlainTextObject):
parsed_title = title
else:
raise SlackObjectFormationError(f"Unsupported type for title in an image block: {type(title)}")
self.title = parsed_title

@JsonValidator(f"image_url attribute cannot exceed {image_url_max_length} characters")
def _validate_image_url_length(self):
Expand Down
54 changes: 54 additions & 0 deletions tests/slack_sdk/models/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,60 @@ def test_document(self):
}
self.assertDictEqual(input, ImageBlock(**input).to_dict())

def test_issue_1369_title_type(self):
self.assertEqual(
"plain_text",
ImageBlock(
image_url="https://example.com/",
alt_text="example",
title="example",
).title.type,
)

self.assertEqual(
"plain_text",
ImageBlock(
image_url="https://example.com/",
alt_text="example",
title={
"type": "plain_text",
"text": "Please enjoy this photo of a kitten",
},
).title.type,
)

self.assertEqual(
"plain_text",
ImageBlock(
image_url="https://example.com/",
alt_text="example",
title=PlainTextObject(text="example"),
).title.type,
)

with self.assertRaises(SlackObjectFormationError):
self.assertEqual(
"plain_text",
ImageBlock(
image_url="https://example.com/",
alt_text="example",
title={
"type": "mrkdwn",
"text": "Please enjoy this photo of a kitten",
},
).title.type,
)

with self.assertRaises(SlackObjectFormationError):
self.assertEqual(
"plain_text",
ImageBlock(
image_url="https://example.com/",
alt_text="example",
title=MarkdownTextObject(text="example"),
).title.type,
)

def test_json(self):
self.assertDictEqual(
{
Expand Down

0 comments on commit 027a287

Please sign in to comment.