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

Commit 25a064f

Browse files
fix: fix exception when validation against empty list
1 parent 758f49b commit 25a064f

File tree

5 files changed

+20
-4
lines changed

5 files changed

+20
-4
lines changed

openapi_tester/loaders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def set_schema(self, schema: dict) -> None:
112112
self.schema = self.normalize_schema_paths(de_referenced_schema)
113113

114114
@cached_property
115-
def endpoints(self) -> list[str]: # pylint: disable=no-self-use
115+
def endpoints(self) -> list[str]:
116116
"""
117117
Returns a list of endpoint paths.
118118
"""

openapi_tester/schema_tester.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def get_response_schema_section(self, response: Response) -> dict[str, Any]:
143143
f"Documented status codes: {list(responses_object.keys())}. ",
144144
)
145145

146-
if "openapi" not in schema: # pylint: disable=E1135
146+
if "openapi" not in schema:
147147
# openapi 2.0, i.e. "swagger" has a different structure than openapi 3.0 status sub-schemas
148148
return self.get_key_value(status_code_object, "schema")
149149

@@ -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() if response.data else {},
386+
data=response.json() if response.data is not None else {},
387387
case_tester=case_tester or self.case_tester,
388388
ignore_case=ignore_case,
389389
validators=validators,

test_project/api/views/names.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ def get_object(self):
2323
class NameViewSet(ReadOnlyModelViewSet):
2424
serializer_class = NamesSerializer
2525
queryset = Names.objects.all()
26+
27+
28+
class EmptyNameViewSet(ReadOnlyModelViewSet):
29+
serializer_class = NamesSerializer
30+
queryset = Names.objects.all().none()

test_project/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from test_project.api.views.exempt_endpoint import Exempt
1111
from test_project.api.views.i18n import Languages
1212
from test_project.api.views.items import Items
13-
from test_project.api.views.names import NamesRetrieveView, NameViewSet
13+
from test_project.api.views.names import EmptyNameViewSet, NamesRetrieveView, NameViewSet
1414
from test_project.api.views.products import Products
1515
from test_project.api.views.snake_cased_response import SnakeCasedResponse
1616
from test_project.api.views.trucks import BadTrucks, GoodTrucks
@@ -29,6 +29,7 @@
2929
path("api/<str:version>/items", Items.as_view()),
3030
path("api/<str:version>/exempt-endpoint", Exempt.as_view()),
3131
path("api/<str:version>/<str:pk>/names", NamesRetrieveView.as_view()),
32+
path("api/<str:version>/empty-names", EmptyNameViewSet.as_view({"get": "list"})),
3233
path("api/<str:version>/categories/<int:category_pk>/subcategories/<int:subcategory_pk>/", Products.as_view()),
3334
path("api/<str:version>/snake-case/", SnakeCasedResponse.as_view()),
3435
# ^trailing slash is here on purpose

tests/test_clients.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ def test_request(openapi_client, generic_kwargs, expected_status_code):
5252
assert response.status_code == expected_status_code
5353

5454

55+
def test_request_on_empty_list(openapi_client):
56+
"""Ensure ``SchemaTester`` doesn't raise exception when response is empty list."""
57+
response = openapi_client.generic(
58+
method="GET",
59+
path="/api/v1/empty-names",
60+
content_type="application/json",
61+
)
62+
assert response.status_code == status.HTTP_200_OK, response.data
63+
64+
5565
@pytest.mark.parametrize(
5666
("generic_kwargs", "raises_kwargs"),
5767
[

0 commit comments

Comments
 (0)