Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions openapi3/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions tests/path_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]