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

Commit

Permalink
Added the ability to set different max_depths for validate_params dic…
Browse files Browse the repository at this point in the history
…t handling
  • Loading branch information
Seluj78 committed Sep 22, 2024
1 parent 47bf4a3 commit f1049aa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
8 changes: 3 additions & 5 deletions flask_utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@

from flask_utils.errors import BadRequestError

# TODO: Allow to set this value from the config/env
VALIDATE_PARAMS_MAX_DEPTH = 4


def _handle_bad_request(
use_error_handlers: bool,
Expand Down Expand Up @@ -131,9 +128,10 @@ def _check_type(value: Any, expected_type: Type, curr_depth: int = 0) -> bool:
.. versionadded:: 0.2.0
"""
max_depth = current_app.config.get("VALIDATE_PARAMS_MAX_DEPTH", 4)

if curr_depth >= VALIDATE_PARAMS_MAX_DEPTH:
warnings.warn(f"Maximum depth of {VALIDATE_PARAMS_MAX_DEPTH} reached.", SyntaxWarning, stacklevel=2)
if curr_depth >= max_depth:
warnings.warn(f"Maximum depth of {max_depth} reached.", SyntaxWarning, stacklevel=2)
return True
if expected_type is Any or _is_allow_empty(value, expected_type): # type: ignore
return True
Expand Down
16 changes: 16 additions & 0 deletions flask_utils/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ class FlaskUtils(object):
fu = FlaskUtils()
fu.init_app(app)
.. versionchanged:: 1.0.0
The :func:`~flask_utils.decorators.validate_params` decorator will now use the ``VALIDATE_PARAMS_MAX_DEPTH``
config variable to determine the maximum depth of the validation for dictionaries.
:Example:
.. code-block:: python
from flask import Flask
from flask_utils import FlaskUtils
app = Flask(__name__)
fu = FlaskUtils(app)
app.config["VALIDATE_PARAMS_MAX_DEPTH"] = 3
.. versionadded:: 0.5.0
"""

Expand Down Expand Up @@ -77,3 +92,4 @@ def init_app(self, app: Flask, register_error_handlers: bool = True) -> None:
self.has_error_handlers_registered = True

app.extensions["flask_utils"] = self
app.config.setdefault("VALIDATE_PARAMS_MAX_DEPTH", 4)
16 changes: 16 additions & 0 deletions tests/test_validate_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,22 @@ def test_max_depth(self, client):
assert issubclass(w[-1].category, SyntaxWarning)
assert "Maximum depth of 4 reached." in str(w[-1].message)

def test_change_max_depth(self, client, flask_client):
flask_client.config["VALIDATE_PARAMS_MAX_DEPTH"] = 1

with warnings.catch_warnings(record=True) as w:
client.post(
"/example",
json={
"user_info": {
"name": {"age": {"is_active": {"weight": {"hobbies": {"address": {"city": "New York City"}}}}}}
}
},
)
assert len(w) == 1
assert issubclass(w[-1].category, SyntaxWarning)
assert "Maximum depth of 1 reached." in str(w[-1].message)


class TestJSONOverridesRouteParams:
@pytest.fixture(autouse=True)
Expand Down

0 comments on commit f1049aa

Please sign in to comment.