Skip to content

fix(client): treat empty 2xx body in http_put as success (#370) - #381

Open
Alex Castillo González (pcbeingused33) wants to merge 2 commits into
OpenAEV-Platform:mainfrom
pcbeingused33:fix/370-empty-put-response
Open

Alex Castillo González (pcbeingused33) wants to merge 2 commits into
OpenAEV-Platform:mainfrom
pcbeingused33:fix/370-empty-put-response

Conversation

@pcbeingused33

Copy link
Copy Markdown

Fixes #370

Proposed changes

  • OpenAEV.http_put() always called result.json() on a 2xx response. PUT /api/injects/expectations/bulk is a void endpoint that answers 200 with an empty body, so JSONDecodeError was re-raised as OpenAEVParsingError even though the platform had applied the update, and collectors fell back to one request per expectation.
  • http_put now 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 raises OpenAEVParsingError.

Testing Instructions

  1. python -m pytest test/apis/inject_expectation/test_inject_expectation.py -q: the new TestBulkUpdate covers an empty 200 body (bulk_update returns normally; it raised OpenAEVParsingError before this change) and a non-empty non-JSON body (still raises). Full suite: 374 passed.
  2. Manual repro: a stub server answering PUT with 200 and Content-Length: 0, then OpenAEV(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

  • I consider the submitted work as finished
  • I tested the code for its functionality
  • I wrote test cases for the relevant uses case
  • I added/update the relevant documentation (either on github or on notion)
  • Where necessary I refactored code to improve the overall quality
  • For bug fix -> I implemented a test that covers the bug

Further comments

The empty-body check sits in http_put rather than in bulk_update so every void PUT endpoint benefits; raw=True callers 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-coded label per CONTRIBUTING).

…tform#370)

Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 13, 2026 06:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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 when raw=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 Mariot Tsitoara (mariot) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pyoaev/client.py Outdated
raw=raw,
**kwargs,
)
if not raw and not result.content:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
if not raw and not result.content:
if not result.content:

Comment on lines +373 to +411
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": {}})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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>
@pcbeingused33

Copy link
Copy Markdown
Author

Thanks, both suggestions applied as-is in 42b1854: the raw guard is gone, so http_put returns {} for any empty 2xx body, and the tests now assert {} on http_put and update() directly (200/204, raw-body PUT, JSON body still parsed). The raw-body case fails against the previous client.py and passes now; the inject_expectation tests pass (27 passed).

This comment was drafted with Claude Code.

@mariot Mariot Tsitoara (mariot) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(inject_expectation): bulk_update always raises OpenAEVParsingError on OpenAEV's empty 200 response

3 participants