Skip to content

Commit d1597d3

Browse files
committed
fix(pagination): Handle empty next_page_token
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 2739924 commit d1597d3

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/frequenz/client/common/pagination/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,15 @@ def from_proto(
124124
Returns:
125125
Info object corresponding to the protobuf message.
126126
"""
127+
# We check for truthiness here to handle both cases where the token is
128+
# not set (defaults to "") or is explicitly set to "". In both
129+
# situations, we want to return `None`. Using `HasField("next_page_token")`
130+
# would not handle the case where the token is explicitly set to "".
127131
return cls(
128132
total_items=pagination_info.total_items,
129-
next_page_token=pagination_info.next_page_token,
133+
next_page_token=pagination_info.next_page_token
134+
if pagination_info.next_page_token
135+
else None,
130136
)
131137

132138
def to_proto_v1alpha8(self) -> PBPaginationInfoAlpha8:

tests/test_pagination.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,38 @@ def test_pagination_info_from_proto_v1alpha8() -> None:
3030
assert info.next_page_token == "token"
3131

3232

33+
def test_pagination_info_from_proto_v1_no_token() -> None:
34+
"""Test the PaginationInfo from_proto method with v1 proto and no token."""
35+
proto = PBPaginationInfo(total_items=100)
36+
info = PaginationInfo.from_proto(proto)
37+
assert info.total_items == 100
38+
assert info.next_page_token is None
39+
40+
41+
def test_pagination_info_from_proto_v1alpha8_no_token() -> None:
42+
"""Test the PaginationInfo from_proto method with v1alpha8 proto and no token."""
43+
proto = PBPaginationInfoAlpha8(total_items=100)
44+
info = PaginationInfo.from_proto(proto)
45+
assert info.total_items == 100
46+
assert info.next_page_token is None
47+
48+
49+
def test_pagination_info_from_proto_v1_empty_token() -> None:
50+
"""Test the PaginationInfo from_proto method with v1 proto and an empty token."""
51+
proto = PBPaginationInfo(total_items=100, next_page_token="")
52+
info = PaginationInfo.from_proto(proto)
53+
assert info.total_items == 100
54+
assert info.next_page_token is None
55+
56+
57+
def test_pagination_info_from_proto_v1alpha8_empty_token() -> None:
58+
"""Test the PaginationInfo from_proto method with v1alpha8 proto and an empty token."""
59+
proto = PBPaginationInfoAlpha8(total_items=100, next_page_token="")
60+
info = PaginationInfo.from_proto(proto)
61+
assert info.total_items == 100
62+
assert info.next_page_token is None
63+
64+
3365
def test_pagination_info_to_proto_v1() -> None:
3466
"""Test the PaginationInfo to_proto method."""
3567
info = PaginationInfo(total_items=100, next_page_token="token")

0 commit comments

Comments
 (0)