|
| 1 | +"""Reproduction test for GitHub issue #564. |
| 2 | +
|
| 3 | +The async delete path fails when the server returns an empty response body |
| 4 | +for the delete endpoint. In the user's report (v8.0.0), this manifested as: |
| 5 | + AttributeError: 'str' object has no attribute '_response_info' |
| 6 | +
|
| 7 | +The root cause was that the deserializer returned a raw empty string when |
| 8 | +the response body couldn't be parsed as JSON, and downstream code tried |
| 9 | +to set attributes on that string. |
| 10 | +
|
| 11 | +The fix returns None early from the deserializer when response data is |
| 12 | +empty or whitespace-only. |
| 13 | +""" |
| 14 | + |
| 15 | +import pytest |
| 16 | +from unittest.mock import AsyncMock, patch |
| 17 | + |
| 18 | +from pinecone.openapi_support.rest_utils import RESTResponse |
| 19 | +from pinecone.openapi_support.asyncio_api_client import AsyncioApiClient |
| 20 | +from pinecone.openapi_support.api_client import ApiClient |
| 21 | +from pinecone.config.openapi_configuration import Configuration |
| 22 | + |
| 23 | + |
| 24 | +def _make_config(): |
| 25 | + return Configuration(host="https://test.pinecone.io", api_key={"ApiKeyAuth": "test-key"}) |
| 26 | + |
| 27 | + |
| 28 | +def _make_response(body: bytes): |
| 29 | + return RESTResponse( |
| 30 | + status=200, data=body, headers={"content-type": "application/json"}, reason="OK" |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +_CALL_API_KWARGS = dict( |
| 35 | + resource_path="/vectors/delete", |
| 36 | + method="POST", |
| 37 | + path_params={}, |
| 38 | + query_params=[], |
| 39 | + header_params={"Content-Type": "application/json"}, |
| 40 | + body={"deleteAll": True, "namespace": "test-namespace"}, |
| 41 | + response_type=(dict,), |
| 42 | + auth_settings=["ApiKeyAuth"], |
| 43 | + _return_http_data_only=True, |
| 44 | + _preload_content=True, |
| 45 | + _request_timeout=None, |
| 46 | + _host=None, |
| 47 | + _check_type=True, |
| 48 | + collection_formats={}, |
| 49 | +) |
| 50 | + |
| 51 | + |
| 52 | +class TestAsyncioDeleteEmptyBody: |
| 53 | + """Async client: empty/whitespace response bodies should return None.""" |
| 54 | + |
| 55 | + @pytest.mark.asyncio |
| 56 | + @pytest.mark.parametrize("body", [b"", b" ", b"\n", b" \n "]) |
| 57 | + async def test_empty_or_whitespace_body_returns_none(self, body): |
| 58 | + client = AsyncioApiClient(configuration=_make_config()) |
| 59 | + try: |
| 60 | + with patch.object( |
| 61 | + client, "request", new_callable=AsyncMock, return_value=_make_response(body) |
| 62 | + ): |
| 63 | + result = await client.call_api(**_CALL_API_KWARGS) |
| 64 | + assert result is None |
| 65 | + finally: |
| 66 | + await client.close() |
| 67 | + |
| 68 | + @pytest.mark.asyncio |
| 69 | + async def test_empty_json_object_succeeds(self): |
| 70 | + client = AsyncioApiClient(configuration=_make_config()) |
| 71 | + try: |
| 72 | + with patch.object( |
| 73 | + client, "request", new_callable=AsyncMock, return_value=_make_response(b"{}") |
| 74 | + ): |
| 75 | + result = await client.call_api(**_CALL_API_KWARGS) |
| 76 | + assert isinstance(result, dict) |
| 77 | + finally: |
| 78 | + await client.close() |
| 79 | + |
| 80 | + |
| 81 | +class TestSyncDeleteEmptyBody: |
| 82 | + """Sync client: empty/whitespace response bodies should return None.""" |
| 83 | + |
| 84 | + @pytest.mark.parametrize("body", [b"", b" ", b"\n", b" \n "]) |
| 85 | + def test_empty_or_whitespace_body_returns_none(self, body): |
| 86 | + client = ApiClient(configuration=_make_config()) |
| 87 | + with patch.object(client, "request", return_value=_make_response(body)): |
| 88 | + result = client.call_api(**_CALL_API_KWARGS) |
| 89 | + assert result is None |
| 90 | + |
| 91 | + def test_empty_json_object_succeeds(self): |
| 92 | + client = ApiClient(configuration=_make_config()) |
| 93 | + with patch.object(client, "request", return_value=_make_response(b"{}")): |
| 94 | + result = client.call_api(**_CALL_API_KWARGS) |
| 95 | + assert isinstance(result, dict) |
0 commit comments