Skip to content

Commit 25c5e54

Browse files
committed
✨ Update DeprecatedEndpoint to include version information in string representation and enhance tests for route configuration
1 parent b253431 commit 25c5e54

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

packages/common-library/src/common_library/changelog.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
CHANGELOG formatted-messages for API routes
33
44
- Append at the bottom of the route's description
5-
- These are displayed in the swagger doc
5+
- These are displayed in the swagger/redoc doc
66
- These are displayed in client's doc as well (auto-generator)
77
- Inspired on this idea https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#describing-changes-between-versions
88
"""
@@ -81,8 +81,12 @@ def __init__(self, alternative_route: str, version: str | None = None):
8181
self.version = version
8282

8383
def to_string(self) -> str:
84+
base_message = "🚨 **Deprecated**"
85+
if self.version:
86+
base_message += f" in *version {self.version}*"
87+
8488
return (
85-
"🚨 **Deprecated**: This endpoint is deprecated and will be removed in a future release.\n"
89+
f"{base_message}: This endpoint is deprecated and will be removed in a future release.\n"
8690
f"Please use `{self.alternative_route}` instead.\n\n"
8791
)
8892

@@ -188,7 +192,7 @@ def create_route_config(
188192
Creates route configuration options including description based on changelog entries.
189193
190194
The function analyzes the changelog to determine if the endpoint:
191-
- Is not yet released (if the earliest entry version is in the future)
195+
- Is released and visible (if the earliest entry version is not in the future and not removed)
192196
- Is deprecated (if there's a DEPRECATED entry in the changelog)
193197
194198
Args:
@@ -209,23 +213,26 @@ def create_route_config(
209213

210214
# Determine endpoint state
211215
is_deprecated = False
212-
is_unreleased = False
216+
is_released = True # Assume released by default
217+
is_removed = False
213218

214-
# Get the first entry (NEW) to check if unreleased
219+
# Get the first entry (NEW) to check if released
215220
if changelog_list and changelog_list[0].entry_type == ChangelogType.NEW:
216221
first_entry = cast(NewEndpoint, changelog_list[0])
217222
first_version = first_entry.get_version()
218223
if first_version and first_version > current_version:
219-
is_unreleased = True
224+
is_released = False
220225

221-
# Check for deprecation
226+
# Check for deprecation and removal
222227
for entry in changelog_list:
223228
if entry.entry_type == ChangelogType.DEPRECATED:
224229
is_deprecated = True
225-
break
230+
elif entry.entry_type == ChangelogType.REMOVED:
231+
is_removed = True
226232

227233
# Set route options based on endpoint state
228-
route_options["include_in_schema"] = not is_unreleased
234+
# An endpoint is included in schema if it's released and not removed
235+
route_options["include_in_schema"] = is_released and not is_removed
229236
route_options["deprecated"] = is_deprecated
230237

231238
# Create description

packages/common-library/tests/test_changelog.py

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
@pytest.fixture
23-
def mock_api_version(mocker: MockerFixture) -> str:
23+
def current_api_version(mocker: MockerFixture) -> str:
2424
"""Fixture to mock the API_VERSION for testing purposes"""
2525
return "0.7.0"
2626

@@ -51,8 +51,14 @@ def test_changelog_entry_classes():
5151
assert deprecated_entry.entry_type == ChangelogType.DEPRECATED
5252
assert deprecated_entry.get_version() == Version("0.7.0")
5353
assert "Deprecated" in deprecated_entry.to_string()
54+
assert "in *version 0.7.0*" in deprecated_entry.to_string()
5455
assert "/v1/better-endpoint" in deprecated_entry.to_string()
5556

57+
# Test DeprecatedEndpoint without version
58+
deprecated_no_version = DeprecatedEndpoint("/v1/better-endpoint")
59+
assert "Deprecated" in deprecated_no_version.to_string()
60+
assert "in *version" not in deprecated_no_version.to_string()
61+
5662
# Test RemovedEndpoint
5763
removed_entry = RemovedEndpoint("0.9.0", "Use the new endpoint instead")
5864
assert removed_entry.entry_type == ChangelogType.REMOVED
@@ -114,7 +120,7 @@ def test_create_route_description():
114120
assert "Changed in *version 0.6.0*: Added authentication" in desc
115121

116122

117-
def test_create_route_config_for_deprecated_endpoints(mock_api_version: str) -> None:
123+
def test_create_route_config_for_deprecated_endpoints(current_api_version: str) -> None:
118124
"""Test route configuration for deprecated endpoints"""
119125
alternative_route = "/v1/new-endpoint"
120126
changelog = [
@@ -125,7 +131,7 @@ def test_create_route_config_for_deprecated_endpoints(mock_api_version: str) ->
125131
config = create_route_config(
126132
base_description="This is a deprecated endpoint",
127133
changelog=changelog,
128-
current_version=Version(mock_api_version),
134+
current_version=Version(current_api_version),
129135
)
130136

131137
expected_config = {
@@ -141,18 +147,18 @@ def test_create_route_config_for_deprecated_endpoints(mock_api_version: str) ->
141147

142148

143149
def test_create_route_config_for_to_be_released_endpoints(
144-
mock_api_version: str,
150+
current_api_version: str,
145151
) -> None:
146152
"""Test route configuration for endpoints that will be released in future versions"""
147-
future_version = f"{int(Version(mock_api_version).major) + 1}.0.0"
153+
future_version = f"{int(Version(current_api_version).major) + 1}.0.0"
148154
changelog = [
149155
NewEndpoint(future_version),
150156
]
151157

152158
config = create_route_config(
153159
base_description=f"This is a feature coming in version {future_version}",
154160
changelog=changelog,
155-
current_version=Version(mock_api_version),
161+
current_version=Version(current_api_version),
156162
)
157163

158164
expected_config = {
@@ -167,7 +173,7 @@ def test_create_route_config_for_to_be_released_endpoints(
167173
assert config == expected_config
168174

169175

170-
def test_create_route_config_with_removal_notice(mock_api_version: str) -> None:
176+
def test_create_route_config_with_removal_notice(current_api_version: str) -> None:
171177
"""Test route configuration with explicit removal notice in changelog"""
172178
removal_message = "Use the new endpoint instead"
173179
alternative_route = "/v1/better-endpoint"
@@ -181,12 +187,12 @@ def test_create_route_config_with_removal_notice(mock_api_version: str) -> None:
181187
config = create_route_config(
182188
base_description="This endpoint will be removed in version 0.9.0",
183189
changelog=changelog,
184-
current_version=Version(mock_api_version),
190+
current_version=current_api_version,
185191
)
186192

187193
expected_config = {
188194
"deprecated": True,
189-
"include_in_schema": True,
195+
"include_in_schema": False, # Changed from True to False due to REMOVED entry
190196
"description": create_route_description(
191197
base="This endpoint will be removed in version 0.9.0",
192198
changelog=changelog,
@@ -196,14 +202,14 @@ def test_create_route_config_with_removal_notice(mock_api_version: str) -> None:
196202
assert config == expected_config
197203

198204

199-
def test_create_route_config_with_regular_endpoint(mock_api_version: str) -> None:
205+
def test_create_route_config_with_regular_endpoint(current_api_version: str) -> None:
200206
"""Test route configuration for a standard endpoint (not deprecated, not upcoming)"""
201207
changelog = [NewEndpoint("0.5.0")]
202208

203209
config = create_route_config(
204210
base_description="This is a standard endpoint",
205211
changelog=changelog,
206-
current_version=mock_api_version,
212+
current_version=current_api_version,
207213
)
208214

209215
expected_config = {
@@ -218,7 +224,7 @@ def test_create_route_config_with_regular_endpoint(mock_api_version: str) -> Non
218224
assert config == expected_config
219225

220226

221-
def test_create_route_config_with_mixed_changelog(mock_api_version: str) -> None:
227+
def test_create_route_config_with_mixed_changelog(current_api_version: str) -> None:
222228

223229
alternative_route = "/v1/better-endpoint"
224230
changelog = [
@@ -232,12 +238,12 @@ def test_create_route_config_with_mixed_changelog(mock_api_version: str) -> None
232238
config = create_route_config(
233239
base_description="This endpoint has a complex history",
234240
changelog=changelog,
235-
current_version=mock_api_version,
241+
current_version=current_api_version,
236242
)
237243

238244
expected_config = {
239245
"deprecated": True,
240-
"include_in_schema": True,
246+
"include_in_schema": False, # Changed from True to False due to REMOVED entry
241247
"description": create_route_description(
242248
base="This endpoint has a complex history",
243249
changelog=changelog,
@@ -247,11 +253,11 @@ def test_create_route_config_with_mixed_changelog(mock_api_version: str) -> None
247253
assert config == expected_config
248254

249255

250-
def test_create_route_config_with_empty_changelog(mock_api_version: str) -> None:
256+
def test_create_route_config_with_empty_changelog(current_api_version: str) -> None:
251257

252258
config = create_route_config(
253259
base_description="This endpoint has no changelog",
254-
current_version=mock_api_version,
260+
current_version=current_api_version,
255261
)
256262

257263
expected_config = {
@@ -264,3 +270,15 @@ def test_create_route_config_with_empty_changelog(mock_api_version: str) -> None
264270
}
265271

266272
assert config == expected_config
273+
274+
275+
# Add a new test to explicitly verify the version display in deprecated endpoints
276+
def test_deprecated_endpoint_with_version():
277+
"""Test that DeprecatedEndpoint correctly displays the version information when available"""
278+
# With version
279+
deprecated_with_version = DeprecatedEndpoint("/new/endpoint", "0.8.0")
280+
assert "in *version 0.8.0*" in deprecated_with_version.to_string()
281+
282+
# Without version
283+
deprecated_without_version = DeprecatedEndpoint("/new/endpoint")
284+
assert "in *version" not in deprecated_without_version.to_string()

0 commit comments

Comments
 (0)