From cb9f386fb9ad12a701d0d13408e86da478197239 Mon Sep 17 00:00:00 2001 From: Clemens Lang Date: Tue, 8 Oct 2019 14:39:51 +0200 Subject: [PATCH 1/2] setup.py: Support jsonschema >= 3.0.0 Other projects have started using jsonschema >= 3.0.0, and modern python packaging tools such as poetry now fail to install projects using both connexion and a different dependency if they depend on different versions of jsonschema. Allow jsonschema versions >= 3.0.0 to avoid this problem. See: https://github.com/sdispater/poetry/issues/697 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index ed14c0ffd..a9f69352b 100755 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ def read_version(package): install_requires = [ 'clickclick>=1.2', - 'jsonschema>=2.5.1,<3.0.0', + 'jsonschema>=2.5.1', 'PyYAML>=5.1', 'requests>=2.9.1', 'six>=1.9', From 2f0d92b5765196f7229d1b7ef4db9d97d8476676 Mon Sep 17 00:00:00 2001 From: Clemens Lang Date: Tue, 8 Oct 2019 14:42:22 +0200 Subject: [PATCH 2/2] Avoid warning when using jsonschema >= 3.0 jsonschema 3.0.0 changed the API to deprecate the types parameter of the jsonschema.IValidator constructor and now raises a warning when using it. Avoid the warning by checking the jsonschema version and switching to the new way to implement this when it is >= 3.0. Note that while jsonschema 2.x did have jsonschema.validators.extend, it did not support the type_checker argument, so the same code cannot be used with jsonschema 2.x. --- connexion/decorators/validation.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/connexion/decorators/validation.py b/connexion/decorators/validation.py index ab0c91345..d9016ad3f 100644 --- a/connexion/decorators/validation.py +++ b/connexion/decorators/validation.py @@ -4,8 +4,10 @@ import logging import sys +import pkg_resources import six from jsonschema import Draft4Validator, ValidationError, draft4_format_checker +from jsonschema.validators import extend from werkzeug.datastructures import FileStorage from ..exceptions import ExtraParameterProblem @@ -14,6 +16,10 @@ from ..problem import problem from ..utils import all_json, boolean, is_json_mimetype, is_null, is_nullable +_jsonschema_3_or_newer = pkg_resources.parse_version( + pkg_resources.get_distribution("jsonschema").version) >= \ + pkg_resources.parse_version("3.0.0") + logger = logging.getLogger('connexion.decorators.validation') TYPE_MAP = { @@ -245,10 +251,19 @@ def validate_parameter(parameter_type, value, param, param_name=None): del param['required'] try: if parameter_type == 'formdata' and param.get('type') == 'file': - Draft4Validator( - param, - format_checker=draft4_format_checker, - types={'file': FileStorage}).validate(converted_value) + if _jsonschema_3_or_newer: + extend( + Draft4Validator, + type_checker=Draft4Validator.TYPE_CHECKER.redefine( + "file", + lambda checker, instance: isinstance(instance, FileStorage) + ) + )(param, format_checker=draft4_format_checker).validate(converted_value) + else: + Draft4Validator( + param, + format_checker=draft4_format_checker, + types={'file': FileStorage}).validate(converted_value) else: Draft4Validator( param, format_checker=draft4_format_checker).validate(converted_value)