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 f2cf3f4 commit ca928f6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "schema_validator"
version = "0.2.0"
version = "0.2.2"
description = "A flask/quart extension to provide schema validation with pydantic."
authors = ["hs <[email protected]>"]
classifiers = [
Expand Down
21 changes: 13 additions & 8 deletions schema_validator/core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import logging
from collections.abc import Mapping
from typing import Any, Dict, List, Optional, Tuple

Expand All @@ -14,19 +15,19 @@
from schema_validator.types import ServerObject
from schema_validator.flask.validation import DataSource


try:
from flask import current_app, render_template_string
from flask import Flask
from flask.json import JSONDecoder, JSONEncoder
IS_FLASK = True
except ImportError:
from quart import Quart
from quart import current_app, render_template_string
from quart.json import JSONDecoder, JSONEncoder
IS_FLASK = False
except ImportError:
from flask import current_app, render_template_string, Flask
from flask.json import JSONDecoder, JSONEncoder
IS_FLASK = True


PATH_RE = re.compile("<(?:[^:]*:)?([^>]+)>")
logger = logging.getLogger(__name__)


class PydanticJSONEncoder(JSONEncoder):
Expand Down Expand Up @@ -118,12 +119,16 @@ def init_app(self, app) -> None:
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
app_name = "QUART"

logger.info(f"start validator by {app_name}")

app.add_url_rule(
self.openapi_path, "openapi",
lambda: openapi(self)
lambda: openapi(validator=self)
)
app.add_url_rule(
self.openapi_tag_path, "openapi_tag",
Expand All @@ -133,7 +138,7 @@ def init_app(self, app) -> None:
if self.swagger_ui_path is not None:
app.add_url_rule(
self.swagger_ui_path, "swagger_ui",
lambda: swagger_ui(self)
lambda: swagger_ui(validator=self)
)
app.add_url_rule(
f"{self.swagger_ui_path}/<tag>", "swagger_ui_tag",
Expand Down
12 changes: 6 additions & 6 deletions schema_validator/quart/api.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from typing import Optional

from quart import render_template_string
from quart import current_app, render_template_string

from schema_validator.constants import SWAGGER_TEMPLATE


async def openapi(app, validator, tag: Optional[str] = None) -> dict:
async def openapi(validator, tag: Optional[str] = None) -> dict:
from ..core import _build_openapi_schema
return _build_openapi_schema(app, validator, tag)
return _build_openapi_schema(current_app, validator, tag)


async def swagger_ui(app, validator, tag: Optional[str] = None) -> str:
async def swagger_ui(validator, tag: Optional[str] = None) -> str:
path = f"/swagger/openapi-{tag}.json" if tag else validator.openapi_path
return await render_template_string(
SWAGGER_TEMPLATE,
title=validator.title,
openapi_path=path,
swagger_js_url=app.config["FLASK_SCHEMA_SWAGGER_JS_URL"],
swagger_css_url=app.config["FLASK_SCHEMA_SWAGGER_CSS_URL"],
swagger_js_url=current_app.config["FLASK_SCHEMA_SWAGGER_JS_URL"],
swagger_css_url=current_app.config["FLASK_SCHEMA_SWAGGER_CSS_URL"],
)

0 comments on commit ca928f6

Please sign in to comment.