Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ponytailer committed Nov 11, 2021
1 parent ca928f6 commit 3aa40aa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
5 changes: 3 additions & 2 deletions schema_validator/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,16 @@ def init_app(self, app) -> None:
IS_FLASK = True
else:
IS_FLASK = False
except ModuleNotFoundError:
except BaseException:
IS_FLASK = False

if self.openapi_path is not None and app.config.get("SWAGGER_ROUTE"):
if IS_FLASK:
from .flask import openapi, swagger_ui
app_name = "FLASK"
else:
from .quart import openapi, swagger_ui
from .quart import openapi, swagger_ui, convert_model_result
app.make_response = convert_model_result(app.make_response)
app_name = "QUART"

logger.info(f"start validator by {app_name}")
Expand Down
4 changes: 2 additions & 2 deletions schema_validator/flask/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ def swagger_ui(validator, tag: Optional[str] = None) -> str:
SWAGGER_TEMPLATE,
title=validator.title,
openapi_path=path,
swagger_js_url=current_app.config["FLASK_SCHEMA_SWAGGER_JS_URL"],
swagger_css_url=current_app.config["FLASK_SCHEMA_SWAGGER_CSS_URL"],
swagger_js_url=current_app.config["SCHEMA_SWAGGER_JS_URL"],
swagger_css_url=current_app.config["SCHEMA_SWAGGER_CSS_URL"],
)
34 changes: 31 additions & 3 deletions schema_validator/quart/api.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
from typing import Optional
import inspect

from typing import Optional, Callable
from functools import wraps
from dataclasses import is_dataclass, asdict
from pydantic import BaseModel

from quart import current_app, render_template_string

from schema_validator.constants import SWAGGER_TEMPLATE


def convert_model_result(func: Callable) -> Callable:
@wraps(func)
async def decorator(result):
status_or_headers = None
headers = None
if isinstance(result, tuple):
value, status_or_headers, headers = result + (None,) * (3 - len(result))
else:
value = result

if is_dataclass(value):
dict_or_value = asdict(value)
elif isinstance(value, BaseModel):
dict_or_value = value.dict()
elif inspect.iscoroutine(value):
dict_or_value = await value
else:
dict_or_value = value
return await func((dict_or_value, status_or_headers, headers))

return decorator


async def openapi(validator, tag: Optional[str] = None) -> dict:
from ..core import _build_openapi_schema
return _build_openapi_schema(current_app, validator, tag)
Expand All @@ -16,6 +44,6 @@ async def swagger_ui(validator, tag: Optional[str] = None) -> str:
SWAGGER_TEMPLATE,
title=validator.title,
openapi_path=path,
swagger_js_url=current_app.config["FLASK_SCHEMA_SWAGGER_JS_URL"],
swagger_css_url=current_app.config["FLASK_SCHEMA_SWAGGER_CSS_URL"],
swagger_js_url=current_app.config["SCHEMA_SWAGGER_JS_URL"],
swagger_css_url=current_app.config["SCHEMA_SWAGGER_CSS_URL"],
)

0 comments on commit 3aa40aa

Please sign in to comment.