Skip to content

Commit

Permalink
Improve base plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatal1ty committed Jan 30, 2024
1 parent cdd69f0 commit c514196
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
15 changes: 8 additions & 7 deletions openapify/core/base_plugins.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import Any, ByteString, Dict, Optional, Union
from typing import Any, Dict, Optional, Union

from mashumaro.jsonschema import OPEN_API_3_1, JSONSchemaBuilder

from openapify.core.models import Body, Cookie, Header, QueryParam
from openapify.core.utils import get_value_type
from openapify.plugin import BasePlugin


Expand All @@ -13,10 +14,9 @@ def schema_helper(
name: Optional[str] = None,
) -> Optional[Dict[str, Any]]:
try:
if isinstance(obj, Body) and issubclass(
obj.value_type, ByteString # type: ignore
):
return {}
if isinstance(obj, Body):
if get_value_type(obj.value_type) in (bytes, bytearray):
return {}
else:
return None
except TypeError:
Expand All @@ -27,8 +27,9 @@ class GuessMediaTypePlugin(BasePlugin):
def media_type_helper(
self, body: Body, schema: Dict[str, Any]
) -> Optional[str]:
if not schema and issubclass(
body.value_type, ByteString # type: ignore
if not schema and get_value_type(body.value_type) in (
bytes,
bytearray,
):
return "application/octet-stream"
else:
Expand Down
13 changes: 13 additions & 0 deletions openapify/core/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from mashumaro.core.meta.helpers import get_type_origin

from openapify.core.models import TypeAnnotation


def get_value_type(value_type: TypeAnnotation) -> TypeAnnotation:
super_type = getattr(value_type, "__supertype__", None)
if super_type is not None:
return get_value_type(super_type)
origin_type = get_type_origin(value_type)
if origin_type is not value_type:
return get_value_type(origin_type)
return value_type

0 comments on commit c514196

Please sign in to comment.