Skip to content

Commit

Permalink
Support jsonschema >= 3.0.0 (#1050)
Browse files Browse the repository at this point in the history
* 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: python-poetry/poetry#697

* 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.
  • Loading branch information
neverpanic authored and hjacobs committed Oct 18, 2019
1 parent 43003f8 commit 7a1ce8d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
23 changes: 19 additions & 4 deletions connexion/decorators/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand Down Expand Up @@ -261,10 +267,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)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 7a1ce8d

Please sign in to comment.