prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Remove pinning and pip_pre config once jsonschema 3 is released properly
  Refs https://github.com/Kinto/kinto/pull/1898#pullrequestreview-180204186
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `kinto/schema_validation.py`
  Content:
  ```
  1 import colander
  2 from jsonschema import ValidationError, SchemaError, RefResolutionError
  3 from jsonschema.validators import validator_for
  4 
  5 try:  # pragma: no cover
  6     from jsonschema import Draft7Validator as DraftValidator
  7 except ImportError:
  8     from jsonschema import Draft4Validator as DraftValidator
  9 
  10 from pyramid.settings import asbool
  11 
  12 from kinto.core import utils
  13 from kinto.core.errors import raise_invalid
  14 from kinto.views import object_exists_or_404
  15 
  16 
  17 class JSONSchemaMapping(colander.SchemaNode):
  18     def schema_type(self, **kw):
  19         return colander.Mapping(unknown="preserve")
  20 
  21     def deserialize(self, cstruct=colander.null):
  22         # Start by deserializing a simple mapping.
  23         validated = super().deserialize(cstruct)
  24 
  25         # In case it is optional in parent schema.
  26         if not validated or validated in (colander.null, colander.drop):
  27             return validated
  28         try:
  29             check_schema(validated)
  30         except ValidationError as e:
  31             self.raise_invalid(e.message)
  32         return validated
  33 
  34 
  35 def check_schema(data):
  36     try:
  37         DraftValidator.check_schema(data)
  38     except SchemaError as e:
  39         message = e.path.pop() + e.message
  40         raise ValidationError(message)
  41 
  42 
  43 # Module level global that stores a version of every possible schema (as a <class 'dict'>)
  44 # turned into a jsonschema instance (as <class 'jsonschema.validators.Validator'>).
  45 _schema_cache = {}
  46 
  47 
  48 def validate(data, schema):
  49     """Raise a ValidationError or a RefResolutionError if the data doesn't validate
  50     with the given schema.
  51 
  52     Note that this function is just a "wrapper" on `jsonschema.validate()` but with
  53     some memoization based on the schema for better repeat performance.
  54     """
  55     # Because the schema is a dict, it can't be used as a hash key so it needs to be
  56     # "transformed" to something that is hashable. The quickest solution is to convert
  57     # it to a string.
  58     # Note that the order of the dict will determine the string it becomes. The solution
  59     # to that would a canonical serializer like `json.dumps(..., sort_keys=True)` but it's
  60     # overkill since the assumption is that the schema is very unlikely to be exactly
  61     # the same but different order.
  62     cache_key = str(schema)
  63     if cache_key not in _schema_cache:
  64         # This is essentially what the `jsonschema.validate()` shortcut function does.
  65         cls = validator_for(schema)
  66         cls.check_schema(schema)
  67         _schema_cache[cache_key] = cls(schema)
  68     return _schema_cache[cache_key].validate(data)
  69 
  70 
  71 def validate_schema(data, schema, id_field, ignore_fields=None):
  72     if ignore_fields is None:
  73         ignore_fields = []
  74     # Only ignore the `id` field if the schema does not explicitly mention it.
  75     if id_field not in schema.get("properties", {}):
  76         ignore_fields += (id_field,)
  77 
  78     required_fields = [f for f in schema.get("required", []) if f not in ignore_fields]
  79     # jsonschema doesn't accept 'required': [] yet.
  80     # See https://github.com/Julian/jsonschema/issues/337.
  81     # In the meantime, strip out 'required' if no other fields are required.
  82     if required_fields:
  83         schema = {**schema, "required": required_fields}
  84     else:
  85         schema = {f: v for f, v in schema.items() if f != "required"}
  86 
  87     data = {f: v for f, v in data.items() if f not in ignore_fields}
  88 
  89     try:
  90         validate(data, schema)
  91     except ValidationError as e:
  92         if e.path:
  93             field = e.path[-1]
  94         elif e.validator_value:
  95             field = e.validator_value[-1]
  96         else:
  97             field = e.schema_path[-1]
  98         e.field = field
  99         raise e
  100     # Raise an error here if a reference in the schema doesn't resolve.
  101     # jsonschema doesn't provide schema validation checking upon creation yet,
  102     # it must be validated against data.
  103     # See https://github.com/Julian/jsonschema/issues/399
  104     # For future support https://github.com/Julian/jsonschema/issues/346.
  105     except RefResolutionError as e:
  106         raise e
  107 
  108 
  109 def validate_from_bucket_schema_or_400(data, resource_name, request, id_field, ignore_fields):
  110     """Lookup in the parent objects if a schema was defined for this resource.
  111 
  112     If the schema validation feature is enabled, if a schema is/are defined, and if the
  113     data does not validate it/them, then it raises a 400 exception.
  114     """
  115     settings = request.registry.settings
  116     schema_validation = "experimental_collection_schema_validation"
  117     # If disabled from settings, do nothing.
  118     if not asbool(settings.get(schema_validation)):
  119         return
  120 
  121     bucket_id = request.matchdict["bucket_id"]
  122     bucket_uri = utils.instance_uri(request, "bucket", id=bucket_id)
  123     buckets = request.bound_data.setdefault("buckets", {})
  124     if bucket_uri not in buckets:
  125         # Unknown yet, fetch from storage.
  126         bucket = object_exists_or_404(
  127             request, resource_name="bucket", parent_id="", object_id=bucket_id
  128         )
  129         buckets[bucket_uri] = bucket
  130 
  131     # Let's see if the bucket defines a schema for this resource.
  132     metadata_field = f"{resource_name}:schema"
  133     bucket = buckets[bucket_uri]
  134     if metadata_field not in bucket:
  135         return
  136 
  137     # Validate or fail with 400.
  138     schema = bucket[metadata_field]
  139     try:
  140         validate_schema(data, schema, ignore_fields=ignore_fields, id_field=id_field)
  141     except ValidationError as e:
  142         raise_invalid(request, name=e.field, description=e.message)
  143     except RefResolutionError as e:
  144         raise_invalid(request, name="schema", description=str(e))
  145
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/kinto/schema_validation.py b/kinto/schema_validation.py
  --- a/kinto/schema_validation.py
  +++ b/kinto/schema_validation.py
  @@ -1,12 +1,12 @@
   import colander
  -from jsonschema import ValidationError, SchemaError, RefResolutionError
  +from jsonschema import (
  +    Draft7Validator as DraftValidator,
  +    ValidationError,
  +    SchemaError,
  +    RefResolutionError,
  +)
   from jsonschema.validators import validator_for
   
  -try:  # pragma: no cover
  -    from jsonschema import Draft7Validator as DraftValidator
  -except ImportError:
  -    from jsonschema import Draft4Validator as DraftValidator
  -
   from pyramid.settings import asbool
   
   from kinto.core import utils
  
verification_info: {"golden_diff": "diff --git a/kinto/schema_validation.py b/kinto/schema_validation.py\n--- a/kinto/schema_validation.py\n+++ b/kinto/schema_validation.py\n@@ -1,12 +1,12 @@\n import colander\n-from jsonschema import ValidationError, SchemaError, RefResolutionError\n+from jsonschema import (\n+    Draft7Validator as DraftValidator,\n+    ValidationError,\n+    SchemaError,\n+    RefResolutionError,\n+)\n from jsonschema.validators import validator_for\n \n-try:  # pragma: no cover\n-    from jsonschema import Draft7Validator as DraftValidator\n-except ImportError:\n-    from jsonschema import Draft4Validator as DraftValidator\n-\n from pyramid.settings import asbool\n \n from kinto.core import utils\n", "issue": "Remove pinning and pip_pre config once jsonschema 3 is released properly\nRefs https://github.com/Kinto/kinto/pull/1898#pullrequestreview-180204186\n", "before_files": [{"content": "import colander\nfrom jsonschema import ValidationError, SchemaError, RefResolutionError\nfrom jsonschema.validators import validator_for\n\ntry:  # pragma: no cover\n    from jsonschema import Draft7Validator as DraftValidator\nexcept ImportError:\n    from jsonschema import Draft4Validator as DraftValidator\n\nfrom pyramid.settings import asbool\n\nfrom kinto.core import utils\nfrom kinto.core.errors import raise_invalid\nfrom kinto.views import object_exists_or_404\n\n\nclass JSONSchemaMapping(colander.SchemaNode):\n    def schema_type(self, **kw):\n        return colander.Mapping(unknown=\"preserve\")\n\n    def deserialize(self, cstruct=colander.null):\n        # Start by deserializing a simple mapping.\n        validated = super().deserialize(cstruct)\n\n        # In case it is optional in parent schema.\n        if not validated or validated in (colander.null, colander.drop):\n            return validated\n        try:\n            check_schema(validated)\n        except ValidationError as e:\n            self.raise_invalid(e.message)\n        return validated\n\n\ndef check_schema(data):\n    try:\n        DraftValidator.check_schema(data)\n    except SchemaError as e:\n        message = e.path.pop() + e.message\n        raise ValidationError(message)\n\n\n# Module level global that stores a version of every possible schema (as a <class 'dict'>)\n# turned into a jsonschema instance (as <class 'jsonschema.validators.Validator'>).\n_schema_cache = {}\n\n\ndef validate(data, schema):\n    \"\"\"Raise a ValidationError or a RefResolutionError if the data doesn't validate\n    with the given schema.\n\n    Note that this function is just a \"wrapper\" on `jsonschema.validate()` but with\n    some memoization based on the schema for better repeat performance.\n    \"\"\"\n    # Because the schema is a dict, it can't be used as a hash key so it needs to be\n    # \"transformed\" to something that is hashable. The quickest solution is to convert\n    # it to a string.\n    # Note that the order of the dict will determine the string it becomes. The solution\n    # to that would a canonical serializer like `json.dumps(..., sort_keys=True)` but it's\n    # overkill since the assumption is that the schema is very unlikely to be exactly\n    # the same but different order.\n    cache_key = str(schema)\n    if cache_key not in _schema_cache:\n        # This is essentially what the `jsonschema.validate()` shortcut function does.\n        cls = validator_for(schema)\n        cls.check_schema(schema)\n        _schema_cache[cache_key] = cls(schema)\n    return _schema_cache[cache_key].validate(data)\n\n\ndef validate_schema(data, schema, id_field, ignore_fields=None):\n    if ignore_fields is None:\n        ignore_fields = []\n    # Only ignore the `id` field if the schema does not explicitly mention it.\n    if id_field not in schema.get(\"properties\", {}):\n        ignore_fields += (id_field,)\n\n    required_fields = [f for f in schema.get(\"required\", []) if f not in ignore_fields]\n    # jsonschema doesn't accept 'required': [] yet.\n    # See https://github.com/Julian/jsonschema/issues/337.\n    # In the meantime, strip out 'required' if no other fields are required.\n    if required_fields:\n        schema = {**schema, \"required\": required_fields}\n    else:\n        schema = {f: v for f, v in schema.items() if f != \"required\"}\n\n    data = {f: v for f, v in data.items() if f not in ignore_fields}\n\n    try:\n        validate(data, schema)\n    except ValidationError as e:\n        if e.path:\n            field = e.path[-1]\n        elif e.validator_value:\n            field = e.validator_value[-1]\n        else:\n            field = e.schema_path[-1]\n        e.field = field\n        raise e\n    # Raise an error here if a reference in the schema doesn't resolve.\n    # jsonschema doesn't provide schema validation checking upon creation yet,\n    # it must be validated against data.\n    # See https://github.com/Julian/jsonschema/issues/399\n    # For future support https://github.com/Julian/jsonschema/issues/346.\n    except RefResolutionError as e:\n        raise e\n\n\ndef validate_from_bucket_schema_or_400(data, resource_name, request, id_field, ignore_fields):\n    \"\"\"Lookup in the parent objects if a schema was defined for this resource.\n\n    If the schema validation feature is enabled, if a schema is/are defined, and if the\n    data does not validate it/them, then it raises a 400 exception.\n    \"\"\"\n    settings = request.registry.settings\n    schema_validation = \"experimental_collection_schema_validation\"\n    # If disabled from settings, do nothing.\n    if not asbool(settings.get(schema_validation)):\n        return\n\n    bucket_id = request.matchdict[\"bucket_id\"]\n    bucket_uri = utils.instance_uri(request, \"bucket\", id=bucket_id)\n    buckets = request.bound_data.setdefault(\"buckets\", {})\n    if bucket_uri not in buckets:\n        # Unknown yet, fetch from storage.\n        bucket = object_exists_or_404(\n            request, resource_name=\"bucket\", parent_id=\"\", object_id=bucket_id\n        )\n        buckets[bucket_uri] = bucket\n\n    # Let's see if the bucket defines a schema for this resource.\n    metadata_field = f\"{resource_name}:schema\"\n    bucket = buckets[bucket_uri]\n    if metadata_field not in bucket:\n        return\n\n    # Validate or fail with 400.\n    schema = bucket[metadata_field]\n    try:\n        validate_schema(data, schema, ignore_fields=ignore_fields, id_field=id_field)\n    except ValidationError as e:\n        raise_invalid(request, name=e.field, description=e.message)\n    except RefResolutionError as e:\n        raise_invalid(request, name=\"schema\", description=str(e))\n", "path": "kinto/schema_validation.py"}], "after_files": [{"content": "import colander\nfrom jsonschema import (\n    Draft7Validator as DraftValidator,\n    ValidationError,\n    SchemaError,\n    RefResolutionError,\n)\nfrom jsonschema.validators import validator_for\n\nfrom pyramid.settings import asbool\n\nfrom kinto.core import utils\nfrom kinto.core.errors import raise_invalid\nfrom kinto.views import object_exists_or_404\n\n\nclass JSONSchemaMapping(colander.SchemaNode):\n    def schema_type(self, **kw):\n        return colander.Mapping(unknown=\"preserve\")\n\n    def deserialize(self, cstruct=colander.null):\n        # Start by deserializing a simple mapping.\n        validated = super().deserialize(cstruct)\n\n        # In case it is optional in parent schema.\n        if not validated or validated in (colander.null, colander.drop):\n            return validated\n        try:\n            check_schema(validated)\n        except ValidationError as e:\n            self.raise_invalid(e.message)\n        return validated\n\n\ndef check_schema(data):\n    try:\n        DraftValidator.check_schema(data)\n    except SchemaError as e:\n        message = e.path.pop() + e.message\n        raise ValidationError(message)\n\n\n# Module level global that stores a version of every possible schema (as a <class 'dict'>)\n# turned into a jsonschema instance (as <class 'jsonschema.validators.Validator'>).\n_schema_cache = {}\n\n\ndef validate(data, schema):\n    \"\"\"Raise a ValidationError or a RefResolutionError if the data doesn't validate\n    with the given schema.\n\n    Note that this function is just a \"wrapper\" on `jsonschema.validate()` but with\n    some memoization based on the schema for better repeat performance.\n    \"\"\"\n    # Because the schema is a dict, it can't be used as a hash key so it needs to be\n    # \"transformed\" to something that is hashable. The quickest solution is to convert\n    # it to a string.\n    # Note that the order of the dict will determine the string it becomes. The solution\n    # to that would a canonical serializer like `json.dumps(..., sort_keys=True)` but it's\n    # overkill since the assumption is that the schema is very unlikely to be exactly\n    # the same but different order.\n    cache_key = str(schema)\n    if cache_key not in _schema_cache:\n        # This is essentially what the `jsonschema.validate()` shortcut function does.\n        cls = validator_for(schema)\n        cls.check_schema(schema)\n        _schema_cache[cache_key] = cls(schema)\n    return _schema_cache[cache_key].validate(data)\n\n\ndef validate_schema(data, schema, id_field, ignore_fields=None):\n    if ignore_fields is None:\n        ignore_fields = []\n    # Only ignore the `id` field if the schema does not explicitly mention it.\n    if id_field not in schema.get(\"properties\", {}):\n        ignore_fields += (id_field,)\n\n    required_fields = [f for f in schema.get(\"required\", []) if f not in ignore_fields]\n    # jsonschema doesn't accept 'required': [] yet.\n    # See https://github.com/Julian/jsonschema/issues/337.\n    # In the meantime, strip out 'required' if no other fields are required.\n    if required_fields:\n        schema = {**schema, \"required\": required_fields}\n    else:\n        schema = {f: v for f, v in schema.items() if f != \"required\"}\n\n    data = {f: v for f, v in data.items() if f not in ignore_fields}\n\n    try:\n        validate(data, schema)\n    except ValidationError as e:\n        if e.path:\n            field = e.path[-1]\n        elif e.validator_value:\n            field = e.validator_value[-1]\n        else:\n            field = e.schema_path[-1]\n        e.field = field\n        raise e\n    # Raise an error here if a reference in the schema doesn't resolve.\n    # jsonschema doesn't provide schema validation checking upon creation yet,\n    # it must be validated against data.\n    # See https://github.com/Julian/jsonschema/issues/399\n    # For future support https://github.com/Julian/jsonschema/issues/346.\n    except RefResolutionError as e:\n        raise e\n\n\ndef validate_from_bucket_schema_or_400(data, resource_name, request, id_field, ignore_fields):\n    \"\"\"Lookup in the parent objects if a schema was defined for this resource.\n\n    If the schema validation feature is enabled, if a schema is/are defined, and if the\n    data does not validate it/them, then it raises a 400 exception.\n    \"\"\"\n    settings = request.registry.settings\n    schema_validation = \"experimental_collection_schema_validation\"\n    # If disabled from settings, do nothing.\n    if not asbool(settings.get(schema_validation)):\n        return\n\n    bucket_id = request.matchdict[\"bucket_id\"]\n    bucket_uri = utils.instance_uri(request, \"bucket\", id=bucket_id)\n    buckets = request.bound_data.setdefault(\"buckets\", {})\n    if bucket_uri not in buckets:\n        # Unknown yet, fetch from storage.\n        bucket = object_exists_or_404(\n            request, resource_name=\"bucket\", parent_id=\"\", object_id=bucket_id\n        )\n        buckets[bucket_uri] = bucket\n\n    # Let's see if the bucket defines a schema for this resource.\n    metadata_field = f\"{resource_name}:schema\"\n    bucket = buckets[bucket_uri]\n    if metadata_field not in bucket:\n        return\n\n    # Validate or fail with 400.\n    schema = bucket[metadata_field]\n    try:\n        validate_schema(data, schema, ignore_fields=ignore_fields, id_field=id_field)\n    except ValidationError as e:\n        raise_invalid(request, name=e.field, description=e.message)\n    except RefResolutionError as e:\n        raise_invalid(request, name=\"schema\", description=str(e))\n", "path": "kinto/schema_validation.py"}]}