Skip to content

Commit

Permalink
Add the OpenAPI definitions to the validation schema for resolving JS…
Browse files Browse the repository at this point in the history
…ON pointers
  • Loading branch information
foarsitter committed Feb 1, 2024
1 parent b38a7d9 commit 9eaee43
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
9 changes: 0 additions & 9 deletions flask_restx/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

from flask.signals import got_request_exception

from jsonschema import RefResolver

from werkzeug.utils import cached_property
from werkzeug.datastructures import Headers
from werkzeug.exceptions import (
Expand Down Expand Up @@ -162,7 +160,6 @@ def __init__(
)
self._schema = None
self.models = {}
self._refresolver = None
self.format_checker = format_checker
self.namespaces = []
self.default_swagger_filename = default_swagger_filename
Expand Down Expand Up @@ -822,12 +819,6 @@ def payload(self):
"""Store the input payload in the current request context"""
return request.get_json()

@property
def refresolver(self):
if not self._refresolver:
self._refresolver = RefResolver.from_schema(self.__schema__)
return self._refresolver

@staticmethod
def _blueprint_setup_add_url_rule_patch(
blueprint_setup, rule, endpoint=None, view_func=None, **options
Expand Down
9 changes: 4 additions & 5 deletions flask_restx/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from .utils import not_none
from ._http import HTTPStatus


RE_REQUIRED = re.compile(r"u?\'(?P<name>.*)\' is a required property", re.I | re.U)


Expand Down Expand Up @@ -88,10 +87,10 @@ def inherit(cls, name, *parents):
model.__parents__ = parents[:-1]
return model

def validate(self, data, resolver=None, format_checker=None):
validator = Draft4Validator(
self.__schema__, resolver=resolver, format_checker=format_checker
)
def validate(self, data, format_checker=None, definitions=None):
schema = self.__schema__
schema["definitions"] = definitions or {}
validator = Draft4Validator(schema, format_checker=format_checker)
try:
validator.validate(data)
except ValidationError:
Expand Down
12 changes: 10 additions & 2 deletions flask_restx/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,17 @@ def __validate_payload(self, expect, collection=False):
if collection:
data = data if isinstance(data, list) else [data]
for obj in data:
expect.validate(obj, self.api.refresolver, self.api.format_checker)
expect.validate(
obj,
self.api.format_checker,
definitions=self.api.__schema__["definitions"],
)
else:
expect.validate(data, self.api.refresolver, self.api.format_checker)
expect.validate(
data,
self.api.format_checker,
definitions=self.api.__schema__["definitions"],
)

def validate_payload(self, func):
"""Perform a payload validation on expected model if necessary"""
Expand Down

0 comments on commit 9eaee43

Please sign in to comment.