Repository navigation
fix(client): treat empty 2xx body in http_put as success (#370) - #381
Alex Castillo González (pcbeingused33) wants to merge 2 commits into
Conversation
…tform#370) Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were identified for the change to handle empty successful PUT responses.
No open issues 🎉
What changed in this PR
Fixes #370 by accepting empty successful PUT responses, preventing false parsing errors and unnecessary fallback requests after bulk expectation updates.
Changes:
- Returns
{}for empty 2xx PUT responses whenraw=False, leaving raw-call behavior unchanged. - Adds regression tests for empty-body success and continued parsing errors on nonempty, non-JSON responses.
| File | Description |
|---|---|
test/apis/inject_expectation/test_inject_expectation.py |
Tests bulk updates with empty and invalid response bodies. |
pyoaev/client.py |
Skips JSON parsing for empty successful non-raw PUT responses. |
Review effort: Balanced
Mariot Tsitoara (mariot)
left a comment
There was a problem hiding this comment.
The fix works end to end. I ran the reproduction script from #370 against OpenAEV 3.261126.1 (with the contract looked up by textSearch on the payload name, since it wasn't in the first 500 contracts):
| pyoaev | bulk_update() |
pending after | update() |
|---|---|---|---|
| 3.261126.1 | OpenAEVParsingError (JSONDecodeError) |
[] |
parsed dict |
| this PR (bd2475b) | returns normally | [] |
parsed dict |
| this PR + the two suggestions below | returns normally | [] |
parsed dict |
Two inline suggestions: the raw check, and tests that assert what http_put returns. With both applied, the full test suite passes (378 tests) and black and isort are clean.
Out of scope here: http_patch also calls result.json() unconditionally, so a void PATCH endpoint would hit the same error.
| raw=raw, | ||
| **kwargs, | ||
| ) | ||
| if not raw and not result.content: |
There was a problem hiding this comment.
raw only changes how post_data is sent (as-is, as application/octet-stream, instead of JSON); http_put parses the response the same way either way. With not raw, a raw-body PUT to a void endpoint still raises OpenAEVParsingError. No caller passes raw=True to http_put today, so this keeps the check right rather than fixing a live bug.
| if not raw and not result.content: | |
| if not result.content: |
| class TestBulkUpdate(TestCase): | ||
| @mock.patch("requests.Session.request") | ||
| def test_empty_2xx_body_is_success(self, mock_request): | ||
| class EmptyResponse: | ||
| status_code = 200 | ||
| history = None | ||
| content = b"" | ||
| headers = {} | ||
|
|
||
| def json(self): | ||
| raise ValueError("Expecting value: line 1 column 1 (char 0)") | ||
|
|
||
| mock_request.return_value = EmptyResponse() | ||
| api_client = OpenAEV("url", "token") | ||
|
|
||
| result = api_client.inject_expectation.bulk_update( | ||
| {"exp-1": {"collector_id": "c-1", "result": "Detected", "is_success": True}} | ||
| ) | ||
|
|
||
| self.assertIsNone(result) | ||
| _, kwargs = mock_request.call_args | ||
| self.assertEqual(kwargs["url"], "url/api/injects/expectations/bulk") | ||
|
|
||
| @mock.patch("requests.Session.request") | ||
| def test_non_json_non_empty_body_still_raises(self, mock_request): | ||
| class HtmlResponse: | ||
| status_code = 200 | ||
| history = None | ||
| content = b"<html>" | ||
| headers = {} | ||
|
|
||
| def json(self): | ||
| raise ValueError("Expecting value") | ||
|
|
||
| mock_request.return_value = HtmlResponse() | ||
| api_client = OpenAEV("url", "token") | ||
|
|
||
| with self.assertRaises(OpenAEVParsingError): | ||
| api_client.inject_expectation.bulk_update({"exp-1": {}}) |
There was a problem hiding this comment.
bulk_update always returns None, so assertIsNone(result) passes whatever http_put returns. If it returned the Response or None instead of {}, this test would still pass, while inject_expectation.update() hands that value straight to its callers.
This suggestion asserts {} on http_put and update() directly, and adds cases for 204, for a raw-body PUT (it fails without the change on client.py), and for a JSON body that is still parsed.
| class TestBulkUpdate(TestCase): | |
| @mock.patch("requests.Session.request") | |
| def test_empty_2xx_body_is_success(self, mock_request): | |
| class EmptyResponse: | |
| status_code = 200 | |
| history = None | |
| content = b"" | |
| headers = {} | |
| def json(self): | |
| raise ValueError("Expecting value: line 1 column 1 (char 0)") | |
| mock_request.return_value = EmptyResponse() | |
| api_client = OpenAEV("url", "token") | |
| result = api_client.inject_expectation.bulk_update( | |
| {"exp-1": {"collector_id": "c-1", "result": "Detected", "is_success": True}} | |
| ) | |
| self.assertIsNone(result) | |
| _, kwargs = mock_request.call_args | |
| self.assertEqual(kwargs["url"], "url/api/injects/expectations/bulk") | |
| @mock.patch("requests.Session.request") | |
| def test_non_json_non_empty_body_still_raises(self, mock_request): | |
| class HtmlResponse: | |
| status_code = 200 | |
| history = None | |
| content = b"<html>" | |
| headers = {} | |
| def json(self): | |
| raise ValueError("Expecting value") | |
| mock_request.return_value = HtmlResponse() | |
| api_client = OpenAEV("url", "token") | |
| with self.assertRaises(OpenAEVParsingError): | |
| api_client.inject_expectation.bulk_update({"exp-1": {}}) | |
| def make_put_response(status_code=200, content=b"", payload=None): | |
| class PutResponse: | |
| def __init__(self): | |
| self.status_code = status_code | |
| self.history = None | |
| self.content = content | |
| self.headers = {"Content-Type": "application/json"} if content else {} | |
| def json(self): | |
| if payload is None: | |
| raise ValueError("Expecting value: line 1 column 1 (char 0)") | |
| return payload | |
| return PutResponse() | |
| class TestEmptyPutResponse(TestCase): | |
| @mock.patch("requests.Session.request") | |
| def test_bulk_update_empty_2xx_body_is_success(self, mock_request): | |
| api_client = OpenAEV("url", "token") | |
| for status_code in (200, 204): | |
| with self.subTest(status_code=status_code): | |
| mock_request.return_value = make_put_response(status_code) | |
| api_client.inject_expectation.bulk_update( | |
| { | |
| "exp-1": { | |
| "collector_id": "c-1", | |
| "result": "Detected", | |
| "is_success": True, | |
| } | |
| } | |
| ) | |
| _, kwargs = mock_request.call_args | |
| self.assertEqual(kwargs["method"], "put") | |
| self.assertEqual(kwargs["url"], "url/api/injects/expectations/bulk") | |
| @mock.patch("requests.Session.request") | |
| def test_http_put_empty_2xx_body_returns_empty_dict(self, mock_request): | |
| api_client = OpenAEV("url", "token") | |
| for status_code in (200, 204): | |
| with self.subTest(status_code=status_code): | |
| mock_request.return_value = make_put_response(status_code) | |
| result = api_client.http_put( | |
| "/injects/expectations/bulk", post_data={"inputs": {}} | |
| ) | |
| self.assertEqual({}, result) | |
| @mock.patch("requests.Session.request") | |
| def test_http_put_raw_empty_2xx_body_returns_empty_dict(self, mock_request): | |
| mock_request.return_value = make_put_response(200) | |
| api_client = OpenAEV("url", "token") | |
| result = api_client.http_put( | |
| "/injects/expectations/bulk", post_data=b'{"inputs": {}}', raw=True | |
| ) | |
| self.assertEqual({}, result) | |
| @mock.patch("requests.Session.request") | |
| def test_update_empty_2xx_body_returns_empty_dict(self, mock_request): | |
| mock_request.return_value = make_put_response(200) | |
| api_client = OpenAEV("url", "token") | |
| result = api_client.inject_expectation.update( | |
| "exp-1", {"collector_id": "c-1", "result": "Detected", "is_success": True} | |
| ) | |
| self.assertEqual({}, result) | |
| _, kwargs = mock_request.call_args | |
| self.assertEqual(kwargs["url"], "url/api/injects/expectations/exp-1") | |
| @mock.patch("requests.Session.request") | |
| def test_update_json_body_is_still_parsed(self, mock_request): | |
| payload = {"inject_expectation_id": "exp-1", "inject_expectation_score": 100} | |
| mock_request.return_value = make_put_response( | |
| 200, content=b'{"inject_expectation_id": "exp-1"}', payload=payload | |
| ) | |
| api_client = OpenAEV("url", "token") | |
| result = api_client.inject_expectation.update( | |
| "exp-1", {"collector_id": "c-1", "result": "Detected", "is_success": True} | |
| ) | |
| self.assertEqual(payload, result) | |
| @mock.patch("requests.Session.request") | |
| def test_non_json_non_empty_body_still_raises(self, mock_request): | |
| mock_request.return_value = make_put_response(200, content=b"<html>") | |
| api_client = OpenAEV("url", "token") | |
| with self.assertRaises(OpenAEVParsingError): | |
| api_client.inject_expectation.bulk_update({"exp-1": {}}) |
…enAEV-Platform#370) Apply review suggestions: http_put returns {} for an empty 2xx body regardless of raw, and the tests assert the value returned by http_put and inject_expectation.update() directly, covering 200/204, raw-body PUT and JSON bodies that are still parsed. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Signed-off-by: Alex <alex.castillog33@gmail.com>
|
Thanks, both suggestions applied as-is in 42b1854: the This comment was drafted with Claude Code. |
Mariot Tsitoara (mariot)
left a comment
There was a problem hiding this comment.
Thanks, LGTM!
Fixes #370
Proposed changes
OpenAEV.http_put()always calledresult.json()on a 2xx response.PUT /api/injects/expectations/bulkis avoidendpoint that answers 200 with an empty body, soJSONDecodeErrorwas re-raised asOpenAEVParsingErroreven though the platform had applied the update, and collectors fell back to one request per expectation.http_putnow returns{}when a non-raw response has an empty body; any other body is parsed as before, so a non-empty non-JSON body still raisesOpenAEVParsingError.Testing Instructions
python -m pytest test/apis/inject_expectation/test_inject_expectation.py -q: the newTestBulkUpdatecovers an empty 200 body (bulk_updatereturns normally; it raisedOpenAEVParsingErrorbefore this change) and a non-empty non-JSON body (still raises). Full suite: 374 passed.PUTwith200andContent-Length: 0, thenOpenAEV(url, token).inject_expectation.bulk_update({"<id>": {"collector_id": "<id>", "result": "Detected", "is_success": True}})(script in fix(inject_expectation): bulk_update always raises OpenAEVParsingError on OpenAEV's empty 200 response #370).Related issues
Checklist
Further comments
The empty-body check sits in
http_putrather than inbulk_updateso every voidPUTendpoint benefits;raw=Truecallers are unaffected.This change was prepared with the assistance of an AI coding tool (Claude); the submitter is responsible for the content (suggest the
vibe-codedlabel per CONTRIBUTING).