Skip to content

Commit fe248a4

Browse files
athul-rsclaude
andcommitted
UN-2190 Address review: guard non-JSON responses, scope variable to API deployments
- Wrap pm.response.json() in try/catch so error pages (non-JSON) don't surface a Postman test error - Move collection variables behind APIBase.get_collection_variables() so Pipeline collections (no status request) stay variable-free Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 0735fc8 commit fe248a4

1 file changed

Lines changed: 25 additions & 8 deletions

File tree

  • backend/api_v2/postman_collection

backend/api_v2/postman_collection/dto.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ class APIBase(ABC):
8888
def get_form_data_items(self) -> list[FormDataItem]:
8989
pass
9090

91+
def get_collection_variables(self) -> list["VariableItem"]:
92+
"""Collection-level variables; only needed when a request
93+
references them.
94+
"""
95+
return []
96+
9197
@abstractmethod
9298
def get_api_endpoint(self) -> str:
9399
pass
@@ -166,6 +172,14 @@ def _get_status_api_request(self) -> RequestItem:
166172
status_url = urljoin(abs_api_endpoint, "?" + status_query_str)
167173
return RequestItem(method=HTTPMethod.GET, header=header_list, url=status_url)
168174

175+
def get_collection_variables(self) -> list[VariableItem]:
176+
return [
177+
VariableItem(
178+
key=CollectionKey.EXEC_ID_VARIABLE_NAME,
179+
value=CollectionKey.STATUS_EXEC_ID_DEFAULT,
180+
)
181+
]
182+
169183
def _get_execute_capture_event(self) -> EventItem:
170184
"""Post-response script that stores the execution_id from the execute
171185
response into a collection variable, so the status request can use it
@@ -175,7 +189,12 @@ def _get_execute_capture_event(self) -> EventItem:
175189
listen="test",
176190
script=ScriptItem(
177191
exec=[
178-
"const response = pm.response.json();",
192+
"let response = null;",
193+
"try {",
194+
" response = pm.response.json();",
195+
"} catch (error) {",
196+
" // Non-JSON response (e.g. gateway error); nothing to capture",
197+
"}",
179198
"if (response && response.message && response.message.execution_id) {", # noqa: E501
180199
f' pm.collectionVariables.set("{CollectionKey.EXEC_ID_VARIABLE_NAME}", response.message.execution_id);', # noqa: E501
181200
"}",
@@ -267,13 +286,11 @@ def create(
267286
)
268287
postman_info: PostmanInfo = data_object.get_postman_info()
269288
postman_item_list = data_object.get_postman_items()
270-
variables = [
271-
VariableItem(
272-
key=CollectionKey.EXEC_ID_VARIABLE_NAME,
273-
value=CollectionKey.STATUS_EXEC_ID_DEFAULT,
274-
)
275-
]
276-
return cls(info=postman_info, item=postman_item_list, variable=variables)
289+
return cls(
290+
info=postman_info,
291+
item=postman_item_list,
292+
variable=data_object.get_collection_variables(),
293+
)
277294

278295
def to_dict(self) -> dict[str, Any]:
279296
"""Convert PostmanCollection instance to a dict.

0 commit comments

Comments
 (0)