Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add check of field type before running json.loads #19

Merged
merged 3 commits into from
Nov 19, 2024
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
2 changes: 1 addition & 1 deletion flask_pydantic_spec/flask_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def request_validation(
) -> None:
raw_query = request.args or None
if raw_query is not None:
req_query = parse_multi_dict(raw_query)
req_query = parse_multi_dict(raw_query, query)
else:
req_query = {}
if request.content_type and "application/json" in request.content_type:
Expand Down
6 changes: 4 additions & 2 deletions flask_pydantic_spec/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ def sync_validate(*args: Any, **kwargs: Any) -> FlaskResponse:
"200": {"description": "ok"}
}
self.class_view_api_info[view_name][method]["no_api_key"] = no_api_key
self.class_view_api_info[view_name][method]["is_token_route"] = is_token_route
self.class_view_api_info[view_name][method][
"is_token_route"
] = is_token_route

# register
for name, model in zip(
Expand Down Expand Up @@ -428,7 +430,7 @@ def _generate_spec(self) -> Mapping[str, Any]:

return self._generate_spec_common(routes)

def _get_route_security(self, no_api_key, is_token_route):
def _get_route_security(self, no_api_key: bool, is_token_route: bool) -> dict:
if no_api_key:
return {"security": []}
elif is_token_route:
Expand Down
19 changes: 14 additions & 5 deletions flask_pydantic_spec/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
List,
Dict,
Iterable,
Type,
)

from werkzeug.datastructures import MultiDict
from pydantic import BaseModel
from pydantic.fields import SHAPE_LIST, SHAPE_DICT
from werkzeug.routing import Rule

from .types import Response, RequestBase, Request
Expand Down Expand Up @@ -197,14 +199,21 @@ def default_after_handler(
)


def parse_multi_dict(input: MultiDict) -> Dict[str, Any]:
def parse_multi_dict(
input: MultiDict, schema: Optional[Type[BaseModel]] = None
) -> Dict[str, Any]:
result = {}
for key, value in input.to_dict(flat=False).items():
if len(value) == 1:
try:
value_to_use = json.loads(value[0])
except (TypeError, JSONDecodeError):
value_to_use = value[0]
value_to_use = value[0]
if not schema or any(
shape == schema.__fields__[key].shape
for shape in [SHAPE_LIST, SHAPE_DICT]
):
try:
value_to_use = json.loads(value[0])
except (TypeError, JSONDecodeError):
pass
else:
value_to_use = value
result[key] = value_to_use
Expand Down
Loading