diff --git a/openapi3/paths.py b/openapi3/paths.py index f8d0f2e..0499270 100644 --- a/openapi3/paths.py +++ b/openapi3/paths.py @@ -370,6 +370,12 @@ def request(self, base_url, security={}, data=None, parameters={}, verify=True, expected_media = expected_response.content.get(content_type, None) + # If raw_response is True, return the raw text or json based on content type + if raw_response: + if "application/json" in content_type: + return result.json() + return result.text + if expected_media is None and "/" in content_type: # accept media type ranges in the spec. the most specific matching # type should always be chosen, but if we do not have a match here diff --git a/tests/path_test.py b/tests/path_test.py index 1a62cdf..cdd818b 100644 --- a/tests/path_test.py +++ b/tests/path_test.py @@ -156,3 +156,61 @@ def test_securityparameters(with_securityparameters): with patch("requests.sessions.Session.send", return_value=resp) as r: api.call_api_v1_auth_login_create(data={}, parameters={}) api.call_api_v1_auth_login_create(data={}, parameters={}) + + +def test_raw_response_json(petstore_expanded_spec): + """ + Tests that raw_response=True returns unparsed JSON response + """ + mock_json_data = {"id": 1, "name": "doggie"} + resp = MagicMock(status_code=200,headers={"Content-Type": "application/json"},json=lambda: mock_json_data) + + with patch("requests.sessions.Session.send", return_value=resp) as mock_send: + # Get a specific pet by ID + result = petstore_expanded_spec.paths["/pets/{id}"].get.request( + base_url="http://example.com", + parameters={"id": "1"}, + raw_response=True + ) + # Verify we got back the raw JSON without model parsing + assert result == mock_json_data + +def test_raw_response_text(petstore_expanded_spec): + """ + Tests that raw_response=True returns unparsed text response + """ + mock_text = "Hello World" + resp = MagicMock(status_code=200, headers={"Content-Type": "text/plain"}, text=mock_text) + + with patch("requests.sessions.Session.send", return_value=resp) as mock_send: + # Get a specific pet by ID + result = petstore_expanded_spec.paths["/pets/{id}"].get.request( + base_url="http://example.com", + parameters={"id": "1"}, + raw_response=True + ) + # Verify we got back the raw text + assert result == mock_text + + +def test_normal_response_json(petstore_expanded_spec): + """ + Tests that raw_response=False (default) returns parsed model for JSON + """ + mock_json_data = {"id": 1, "name": "doggie"} + resp = MagicMock(status_code=200, headers={"Content-Type": "application/json"}, json=lambda: mock_json_data) + + with patch("requests.sessions.Session.send", return_value=resp) as mock_send: + # Get a specific pet by ID + result = petstore_expanded_spec.paths["/pets/{id}"].get.request( + base_url="http://example.com", + parameters={"id": "1"} + ) + + # Verify response was parsed into a Pet model + assert result.__class__.__name__ == "Pet" + # with the correct attributes + assert hasattr(result, 'id') + assert hasattr(result, 'name') + assert result.id == mock_json_data["id"] + assert result.name == mock_json_data["name"]