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

regex to support alt application/*json types #284

Merged
merged 3 commits into from
Nov 26, 2022
Merged
Changes from 1 commit
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
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
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