Skip to content
This repository was archived by the owner on Nov 19, 2023. It is now read-only.
Merged
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
11 changes: 9 additions & 2 deletions openapi_tester/schema_tester.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" Schema Tester """
from __future__ import annotations

import re
from itertools import chain
from typing import TYPE_CHECKING, Any, Callable, cast

Expand Down Expand Up @@ -89,11 +90,16 @@ def __init__(
raise ImproperlyConfigured(INIT_ERROR)

@staticmethod
def get_key_value(schema: dict[str, dict], key: str, error_addon: str = "") -> dict:
def get_key_value(schema: dict[str, dict], key: str, error_addon: str = "", use_regex=False) -> dict:
"""
Returns the value of a given key
"""
try:
if use_regex:
compiled_pattern = re.compile(key)
for key_ in schema.keys():
if compiled_pattern.match(key_):
return schema[key_]
Comment on lines +98 to +102

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we iterating over key in schema.keys() here instead of just doing this?

if compiled_pattern.match(key):
    return schema[key_]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to find a str match in the schema list.
For example schema.keys() in one instance has dict_keys(['application/xml', 'application/json']) and in this case key_ will return schema["application/json"] as opposed to schema["^application\/.*json$"] (which will break).

return schema[key]
except KeyError as e:
raise UndocumentedSchemaSectionError(
Expand Down Expand Up @@ -168,9 +174,10 @@ def get_response_schema_section(self, response: Response) -> dict[str, Any]:
)
json_object = self.get_key_value(
content_object,
"application/json",
r"^application\/.*json$",
"\n\nNo `application/json` responses documented for method: "
f"{response_method}, path: {parameterized_path}",
use_regex=True,
)
return self.get_key_value(json_object, "schema")

Expand Down
16 changes: 16 additions & 0 deletions test_project/api/views/pets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from rest_framework.response import Response
from rest_framework.status import HTTP_200_OK
from rest_framework.views import APIView

if TYPE_CHECKING:
from rest_framework.request import Request


class Pet(APIView):
def get(self, request: Request, petId: int) -> Response:
pet = {"name": "doggie", "category": {"id": 1, "name": "Dogs"}, "photoUrls": [], "status": "available"}
return Response(pet, HTTP_200_OK)
4 changes: 3 additions & 1 deletion test_project/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.conf.urls.i18n import i18n_patterns
from django.urls import include, path
from django.urls import include, path, re_path
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
from rest_framework import permissions, routers
Expand All @@ -11,6 +11,7 @@
from test_project.api.views.i18n import Languages
from test_project.api.views.items import Items
from test_project.api.views.names import EmptyNameViewSet, NamesRetrieveView, NameViewSet
from test_project.api.views.pets import Pet
from test_project.api.views.products import Products
from test_project.api.views.snake_cased_response import SnakeCasedResponse
from test_project.api.views.trucks import BadTrucks, GoodTrucks
Expand All @@ -34,6 +35,7 @@
path("api/<str:version>/snake-case/", SnakeCasedResponse.as_view()),
# ^trailing slash is here on purpose
path("api/<str:version>/router_generated/", include(router.urls)),
re_path(r"api/pet/(?P<petId>\d+)", Pet.as_view(), name="get-pet"),
]

internationalised_urlpatterns = i18n_patterns(
Expand Down
Loading