From e6949b01d6848697e0dfe1cd5510e795ad2d018e Mon Sep 17 00:00:00 2001 From: Espen Ogino Rathe Date: Mon, 20 Jun 2022 16:09:13 +0200 Subject: [PATCH 1/2] bugfix: fix a bug where custom serializer blocks can fail if after a list This commit fixes a bug where the rendering of a node can fail if it has fields not supported by Block and follows directly after a list item. The list item logic would pass in the first node after the list as context and cast it to a Block. This fails if the node has fields not supported by Block. Which is the case for custom serializer blocks. The context is actually not used, and removing it solves the bug. --- portabletext_html/renderer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/portabletext_html/renderer.py b/portabletext_html/renderer.py index e924c35..211125e 100644 --- a/portabletext_html/renderer.py +++ b/portabletext_html/renderer.py @@ -66,7 +66,7 @@ def render(self) -> str: if list_nodes and not is_list(node): tree = self._normalize_list_tree(list_nodes) - result += ''.join([self._render_node(n, Block(**node), list_item=True) for n in tree]) + result += ''.join([self._render_node(n, list_item=True) for n in tree]) list_nodes = [] # reset list_nodes if is_list(node): From c71c7dce174915af794c8c59b56bc9b68a3832ec Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Mon, 20 Jun 2022 14:18:54 +0000 Subject: [PATCH 2/2] 'Refactored by Sourcery' --- portabletext_html/renderer.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/portabletext_html/renderer.py b/portabletext_html/renderer.py index 211125e..2a5f02e 100644 --- a/portabletext_html/renderer.py +++ b/portabletext_html/renderer.py @@ -208,8 +208,11 @@ def _normalize_list_tree(self, nodes: list) -> list[dict]: continue if node.get('level') < current_list['level']: - parent = self._find_list(tree[-1], level=node.get('level'), list_item=node.get('listItem')) - if parent: + if parent := self._find_list( + tree[-1], + level=node.get('level'), + list_item=node.get('listItem'), + ): current_list = parent current_list['children'].append(node) continue @@ -240,8 +243,7 @@ def _find_list(self, root_node: dict, level: int, list_item: Optional[str] = Non ): return root_node - children = root_node.get('children') - if children: + if children := root_node.get('children'): return self._find_list(children[-1], level, list_item) return None