Skip to content
This repository was archived by the owner on Nov 19, 2023. It is now read-only.

Commit 1e1d0b5

Browse files
authored
Merge pull request #243 from radowit/allow-empty-response-content
Allow empty response content
2 parents 2a17633 + 60b901d commit 1e1d0b5

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

openapi_tester/schema_tester.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,28 @@ def get_response_schema_section(self, response: td.Response) -> Dict[str, Any]:
140140
if "openapi" not in schema: # pylint: disable=E1135
141141
# openapi 2.0, i.e. "swagger" has a different structure than openapi 3.0 status sub-schemas
142142
return self.get_key_value(status_code_object, "schema")
143-
content_object = self.get_key_value(
144-
status_code_object,
145-
"content",
146-
f"\n\nNo content documented for method: {response_method}, path: {parameterized_path}",
147-
)
148-
json_object = self.get_key_value(
149-
content_object,
150-
"application/json",
151-
f"\n\nNo `application/json` responses documented for method: {response_method}, path: {parameterized_path}",
152-
)
153-
return self.get_key_value(json_object, "schema")
143+
144+
if status_code_object.get("content"):
145+
content_object = self.get_key_value(
146+
status_code_object,
147+
"content",
148+
f"\n\nNo content documented for method: {response_method}, path: {parameterized_path}",
149+
)
150+
json_object = self.get_key_value(
151+
content_object,
152+
"application/json",
153+
f"\n\nNo `application/json` responses documented for method: {response_method}, path: {parameterized_path}",
154+
)
155+
return self.get_key_value(json_object, "schema")
156+
157+
if response.json():
158+
raise UndocumentedSchemaSectionError(
159+
UNDOCUMENTED_SCHEMA_SECTION_ERROR.format(
160+
key="content",
161+
error_addon=f"\n\nNo `content` defined for this response: {response_method}, path: {parameterized_path}",
162+
)
163+
)
164+
return {}
154165

155166
def handle_one_of(self, schema_section: dict, data: Any, reference: str, **kwargs: Any):
156167
matches = 0

tests/test_schema_tester.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,32 @@ def test_validate_response_failure_scenario_undocumented_status_code(monkeypatch
191191
tester.validate_response(response)
192192

193193

194+
def test_validate_response_failure_scenario_undocumented_content(client, monkeypatch):
195+
schema = deepcopy(tester.loader.get_schema())
196+
del schema["paths"][parameterized_path][method]["responses"][status]["content"]
197+
monkeypatch.setattr(tester.loader, "get_schema", mock_schema(schema))
198+
response = client.get(de_parameterized_path)
199+
with pytest.raises(
200+
UndocumentedSchemaSectionError,
201+
match=f"Error: Unsuccessfully tried to index the OpenAPI schema by `content`. \n\nNo `content` defined for this response: {method}, path: {parameterized_path}",
202+
):
203+
tester.validate_response(response)
204+
205+
194206
def test_validate_response_global_case_tester(client):
195207
response = client.get(de_parameterized_path)
196208
with pytest.raises(CaseError, match="is not properly PascalCased"):
197209
SchemaTester(case_tester=is_pascal_case).validate_response(response=response)
198210

199211

212+
def test_validate_response_empty_content(client, monkeypatch):
213+
schema = deepcopy(tester.loader.get_schema())
214+
del schema["paths"][parameterized_path][method]["responses"][status]["content"]
215+
monkeypatch.setattr(tester.loader, "get_schema", mock_schema(schema))
216+
response = response_factory({}, de_parameterized_path, method, status)
217+
tester.validate_response(response)
218+
219+
200220
def test_validate_response_global_ignored_case(client):
201221
response = client.get(de_parameterized_path)
202222
SchemaTester(

0 commit comments

Comments
 (0)