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

Commit a1f810c

Browse files
committed
Allow empty null response
1 parent 53e6b88 commit a1f810c

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

openapi_tester/schema_tester.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def get_response_schema_section(self, response: Response) -> dict[str, Any]:
161161
)
162162
return self.get_key_value(json_object, "schema")
163163

164-
if response.json():
164+
if response.data and response.json():
165165
raise UndocumentedSchemaSectionError(
166166
UNDOCUMENTED_SCHEMA_SECTION_ERROR.format(
167167
key="content",
@@ -383,7 +383,7 @@ def validate_response(
383383
response_schema = self.get_response_schema_section(response)
384384
self.test_schema_section(
385385
schema_section=response_schema,
386-
data=response.json(),
386+
data=response.json() if response.data else {},
387387
case_tester=case_tester or self.case_tester,
388388
ignore_case=ignore_case,
389389
validators=validators,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "drf-openapi-tester"
3-
version = "2.0.0"
3+
version = "2.0.1"
44
description = "Test utility for validating OpenAPI response documentation"
55
authors = ["Sondre Lillebø Gundersen <sondrelg@live.no>", "Na'aman Hirschfeld <nhirschfeld@gmail.com>"]
66
maintainers = ["Na'aman Hirschfeld <nhirschfeld@gmail.com>"]

tests/test_schema_tester.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,12 @@ def test_validate_response_global_case_tester(client):
215215
SchemaTester(case_tester=is_pascal_case).validate_response(response=response)
216216

217217

218-
def test_validate_response_empty_content(client, monkeypatch):
218+
@pytest.mark.parametrize("empty_schema", [None, {}])
219+
def test_validate_response_empty_content(empty_schema, client, monkeypatch):
219220
schema = deepcopy(tester.loader.get_schema())
220221
del schema["paths"][parameterized_path][method]["responses"][status]["content"]
221222
monkeypatch.setattr(tester.loader, "get_schema", mock_schema(schema))
222-
response = response_factory({}, de_parameterized_path, method, status)
223+
response = response_factory(empty_schema, de_parameterized_path, method, status)
223224
tester.validate_response(response)
224225

225226

tests/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
TEST_ROOT = Path(__file__).resolve(strict=True).parent
1616

1717

18-
def response_factory(schema: dict, url_fragment: str, method: str, status_code: int | str = 200) -> Response:
19-
converted_schema = SchemaToPythonConverter(deepcopy(schema)).result
18+
def response_factory(schema: dict | None, url_fragment: str, method: str, status_code: int | str = 200) -> Response:
19+
converted_schema = None
20+
if schema:
21+
converted_schema = SchemaToPythonConverter(deepcopy(schema)).result
2022
response = Response(status=int(status_code), data=converted_schema)
2123
response.request = {"REQUEST_METHOD": method, "PATH_INFO": url_fragment}
22-
response.json = lambda: converted_schema # type: ignore
24+
if schema:
25+
response.json = lambda: converted_schema # type: ignore
2326
return response
2427

2528

0 commit comments

Comments
 (0)