prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  500 error when json validation fails with additional properties
  `<ValidationError: "Additional properties are not allowed ('blockID', 'id' were unexpected)">`
  
  ```
    File "/home/mathieu/Code/Mozilla/cliquet/cliquet/resource/__init__.py", line 395, in put
      new_record = self.process_record(post_record, old=existing)
    File "/home/mathieu/Code/Mozilla/kinto/kinto/views/records.py", line 71, in process_record
      field = e.path.pop() if e.path else e.validator_value.pop()
  AttributeError: 'bool' object has no attribute 'pop' lang=None uid=67f1f8fc8d3dd228096950c20d6515c4786a675264741bec2995a560519aad87
  
  ```
  
  Possible fix:
  
  ``` diff
           except jsonschema_exceptions.ValidationError as e:
  -            field = e.path.pop() if e.path else e.validator_value.pop()
  +            try:
  +                field = e.path.pop() if e.path else e.validator_value.pop()
  +            except AttributeError:
  +                field = None
  ```
  
  500 error when json validation fails with additional properties
  `<ValidationError: "Additional properties are not allowed ('blockID', 'id' were unexpected)">`
  
  ```
    File "/home/mathieu/Code/Mozilla/cliquet/cliquet/resource/__init__.py", line 395, in put
      new_record = self.process_record(post_record, old=existing)
    File "/home/mathieu/Code/Mozilla/kinto/kinto/views/records.py", line 71, in process_record
      field = e.path.pop() if e.path else e.validator_value.pop()
  AttributeError: 'bool' object has no attribute 'pop' lang=None uid=67f1f8fc8d3dd228096950c20d6515c4786a675264741bec2995a560519aad87
  
  ```
  
  Possible fix:
  
  ``` diff
           except jsonschema_exceptions.ValidationError as e:
  -            field = e.path.pop() if e.path else e.validator_value.pop()
  +            try:
  +                field = e.path.pop() if e.path else e.validator_value.pop()
  +            except AttributeError:
  +                field = None
  ```
  
  
  --- 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/views/records.py`
  Content:
  ```
  1 import copy
  2 
  3 import jsonschema
  4 from cliquet import resource
  5 from cliquet.errors import raise_invalid
  6 from jsonschema import exceptions as jsonschema_exceptions
  7 from pyramid.security import Authenticated
  8 from pyramid.settings import asbool
  9 
  10 from kinto.views import object_exists_or_404
  11 
  12 
  13 class RecordSchema(resource.ResourceSchema):
  14     class Options:
  15         preserve_unknown = True
  16 
  17 
  18 _parent_path = '/buckets/{{bucket_id}}/collections/{{collection_id}}'
  19 
  20 
  21 @resource.register(name='record',
  22                    collection_path=_parent_path + '/records',
  23                    record_path=_parent_path + '/records/{{id}}')
  24 class Record(resource.ShareableResource):
  25 
  26     mapping = RecordSchema()
  27     schema_field = 'schema'
  28 
  29     def __init__(self, *args, **kwargs):
  30         super(Record, self).__init__(*args, **kwargs)
  31 
  32         # Check if already fetched before (in batch).
  33         collections = self.request.bound_data.setdefault('collections', {})
  34         collection_uri = self.get_parent_id(self.request)
  35         if collection_uri not in collections:
  36             # Unknown yet, fetch from storage.
  37             collection_parent_id = '/buckets/%s' % self.bucket_id
  38             collection = object_exists_or_404(self.request,
  39                                               collection_id='collection',
  40                                               parent_id=collection_parent_id,
  41                                               object_id=self.collection_id)
  42             collections[collection_uri] = collection
  43 
  44         self._collection = collections[collection_uri]
  45 
  46     def get_parent_id(self, request):
  47         self.bucket_id = request.matchdict['bucket_id']
  48         self.collection_id = request.matchdict['collection_id']
  49         return '/buckets/%s/collections/%s' % (self.bucket_id,
  50                                                self.collection_id)
  51 
  52     def is_known_field(self, field_name):
  53         """Without schema, any field is considered as known."""
  54         return True
  55 
  56     def process_record(self, new, old=None):
  57         """Validate records against collection schema, if any."""
  58         new = super(Record, self).process_record(new, old)
  59 
  60         schema = self._collection.get('schema')
  61         settings = self.request.registry.settings
  62         schema_validation = 'experimental_collection_schema_validation'
  63         if not schema or not asbool(settings.get(schema_validation)):
  64             return new
  65 
  66         collection_timestamp = self._collection[self.model.modified_field]
  67 
  68         try:
  69             stripped = copy.deepcopy(new)
  70             stripped.pop(self.model.id_field, None)
  71             stripped.pop(self.model.modified_field, None)
  72             stripped.pop(self.model.permissions_field, None)
  73             stripped.pop(self.schema_field, None)
  74             jsonschema.validate(stripped, schema)
  75         except jsonschema_exceptions.ValidationError as e:
  76             field = e.path.pop() if e.path else e.validator_value.pop()
  77             raise_invalid(self.request, name=field, description=e.message)
  78 
  79         new[self.schema_field] = collection_timestamp
  80         return new
  81 
  82     def collection_get(self):
  83         result = super(Record, self).collection_get()
  84         self._handle_cache_expires(self.request.response)
  85         return result
  86 
  87     def get(self):
  88         result = super(Record, self).get()
  89         self._handle_cache_expires(self.request.response)
  90         return result
  91 
  92     def _handle_cache_expires(self, response):
  93         """If the parent collection defines a ``cache_expires`` attribute,
  94         then cache-control response headers are sent.
  95 
  96         .. note::
  97 
  98             Those headers are also sent if the
  99             ``kinto.record_cache_expires_seconds`` setting is defined.
  100         """
  101         is_anonymous = Authenticated not in self.request.effective_principals
  102         if not is_anonymous:
  103             return
  104 
  105         cache_expires = self._collection.get('cache_expires')
  106         if cache_expires is None:
  107             by_bucket = 'kinto.%s_record_cache_expires_seconds' % (
  108                 self.bucket_id)
  109             by_collection = '%s_%s_record_cache_expires_seconds' % (
  110                 self.bucket_id, self.collection_id)
  111             settings = self.request.registry.settings
  112             cache_expires = settings.get(by_collection,
  113                                          settings.get(by_bucket))
  114 
  115         if cache_expires is not None:
  116             response.cache_expires(seconds=cache_expires)
  117
  ```
  --- 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/views/records.py b/kinto/views/records.py
  --- a/kinto/views/records.py
  +++ b/kinto/views/records.py
  @@ -73,7 +73,10 @@
               stripped.pop(self.schema_field, None)
               jsonschema.validate(stripped, schema)
           except jsonschema_exceptions.ValidationError as e:
  -            field = e.path.pop() if e.path else e.validator_value.pop()
  +            try:
  +                field = e.path.pop() if e.path else e.validator_value.pop()
  +            except AttributeError:
  +                field = None
               raise_invalid(self.request, name=field, description=e.message)
   
           new[self.schema_field] = collection_timestamp
  
verification_info: {"golden_diff": "diff --git a/kinto/views/records.py b/kinto/views/records.py\n--- a/kinto/views/records.py\n+++ b/kinto/views/records.py\n@@ -73,7 +73,10 @@\n             stripped.pop(self.schema_field, None)\n             jsonschema.validate(stripped, schema)\n         except jsonschema_exceptions.ValidationError as e:\n-            field = e.path.pop() if e.path else e.validator_value.pop()\n+            try:\n+                field = e.path.pop() if e.path else e.validator_value.pop()\n+            except AttributeError:\n+                field = None\n             raise_invalid(self.request, name=field, description=e.message)\n \n         new[self.schema_field] = collection_timestamp\n", "issue": "500 error when json validation fails with additional properties\n`<ValidationError: \"Additional properties are not allowed ('blockID', 'id' were unexpected)\">`\n\n```\n  File \"/home/mathieu/Code/Mozilla/cliquet/cliquet/resource/__init__.py\", line 395, in put\n    new_record = self.process_record(post_record, old=existing)\n  File \"/home/mathieu/Code/Mozilla/kinto/kinto/views/records.py\", line 71, in process_record\n    field = e.path.pop() if e.path else e.validator_value.pop()\nAttributeError: 'bool' object has no attribute 'pop' lang=None uid=67f1f8fc8d3dd228096950c20d6515c4786a675264741bec2995a560519aad87\n\n```\n\nPossible fix:\n\n``` diff\n         except jsonschema_exceptions.ValidationError as e:\n-            field = e.path.pop() if e.path else e.validator_value.pop()\n+            try:\n+                field = e.path.pop() if e.path else e.validator_value.pop()\n+            except AttributeError:\n+                field = None\n```\n\n500 error when json validation fails with additional properties\n`<ValidationError: \"Additional properties are not allowed ('blockID', 'id' were unexpected)\">`\n\n```\n  File \"/home/mathieu/Code/Mozilla/cliquet/cliquet/resource/__init__.py\", line 395, in put\n    new_record = self.process_record(post_record, old=existing)\n  File \"/home/mathieu/Code/Mozilla/kinto/kinto/views/records.py\", line 71, in process_record\n    field = e.path.pop() if e.path else e.validator_value.pop()\nAttributeError: 'bool' object has no attribute 'pop' lang=None uid=67f1f8fc8d3dd228096950c20d6515c4786a675264741bec2995a560519aad87\n\n```\n\nPossible fix:\n\n``` diff\n         except jsonschema_exceptions.ValidationError as e:\n-            field = e.path.pop() if e.path else e.validator_value.pop()\n+            try:\n+                field = e.path.pop() if e.path else e.validator_value.pop()\n+            except AttributeError:\n+                field = None\n```\n\n", "before_files": [{"content": "import copy\n\nimport jsonschema\nfrom cliquet import resource\nfrom cliquet.errors import raise_invalid\nfrom jsonschema import exceptions as jsonschema_exceptions\nfrom pyramid.security import Authenticated\nfrom pyramid.settings import asbool\n\nfrom kinto.views import object_exists_or_404\n\n\nclass RecordSchema(resource.ResourceSchema):\n    class Options:\n        preserve_unknown = True\n\n\n_parent_path = '/buckets/{{bucket_id}}/collections/{{collection_id}}'\n\n\n@resource.register(name='record',\n                   collection_path=_parent_path + '/records',\n                   record_path=_parent_path + '/records/{{id}}')\nclass Record(resource.ShareableResource):\n\n    mapping = RecordSchema()\n    schema_field = 'schema'\n\n    def __init__(self, *args, **kwargs):\n        super(Record, self).__init__(*args, **kwargs)\n\n        # Check if already fetched before (in batch).\n        collections = self.request.bound_data.setdefault('collections', {})\n        collection_uri = self.get_parent_id(self.request)\n        if collection_uri not in collections:\n            # Unknown yet, fetch from storage.\n            collection_parent_id = '/buckets/%s' % self.bucket_id\n            collection = object_exists_or_404(self.request,\n                                              collection_id='collection',\n                                              parent_id=collection_parent_id,\n                                              object_id=self.collection_id)\n            collections[collection_uri] = collection\n\n        self._collection = collections[collection_uri]\n\n    def get_parent_id(self, request):\n        self.bucket_id = request.matchdict['bucket_id']\n        self.collection_id = request.matchdict['collection_id']\n        return '/buckets/%s/collections/%s' % (self.bucket_id,\n                                               self.collection_id)\n\n    def is_known_field(self, field_name):\n        \"\"\"Without schema, any field is considered as known.\"\"\"\n        return True\n\n    def process_record(self, new, old=None):\n        \"\"\"Validate records against collection schema, if any.\"\"\"\n        new = super(Record, self).process_record(new, old)\n\n        schema = self._collection.get('schema')\n        settings = self.request.registry.settings\n        schema_validation = 'experimental_collection_schema_validation'\n        if not schema or not asbool(settings.get(schema_validation)):\n            return new\n\n        collection_timestamp = self._collection[self.model.modified_field]\n\n        try:\n            stripped = copy.deepcopy(new)\n            stripped.pop(self.model.id_field, None)\n            stripped.pop(self.model.modified_field, None)\n            stripped.pop(self.model.permissions_field, None)\n            stripped.pop(self.schema_field, None)\n            jsonschema.validate(stripped, schema)\n        except jsonschema_exceptions.ValidationError as e:\n            field = e.path.pop() if e.path else e.validator_value.pop()\n            raise_invalid(self.request, name=field, description=e.message)\n\n        new[self.schema_field] = collection_timestamp\n        return new\n\n    def collection_get(self):\n        result = super(Record, self).collection_get()\n        self._handle_cache_expires(self.request.response)\n        return result\n\n    def get(self):\n        result = super(Record, self).get()\n        self._handle_cache_expires(self.request.response)\n        return result\n\n    def _handle_cache_expires(self, response):\n        \"\"\"If the parent collection defines a ``cache_expires`` attribute,\n        then cache-control response headers are sent.\n\n        .. note::\n\n            Those headers are also sent if the\n            ``kinto.record_cache_expires_seconds`` setting is defined.\n        \"\"\"\n        is_anonymous = Authenticated not in self.request.effective_principals\n        if not is_anonymous:\n            return\n\n        cache_expires = self._collection.get('cache_expires')\n        if cache_expires is None:\n            by_bucket = 'kinto.%s_record_cache_expires_seconds' % (\n                self.bucket_id)\n            by_collection = '%s_%s_record_cache_expires_seconds' % (\n                self.bucket_id, self.collection_id)\n            settings = self.request.registry.settings\n            cache_expires = settings.get(by_collection,\n                                         settings.get(by_bucket))\n\n        if cache_expires is not None:\n            response.cache_expires(seconds=cache_expires)\n", "path": "kinto/views/records.py"}], "after_files": [{"content": "import copy\n\nimport jsonschema\nfrom cliquet import resource\nfrom cliquet.errors import raise_invalid\nfrom jsonschema import exceptions as jsonschema_exceptions\nfrom pyramid.security import Authenticated\nfrom pyramid.settings import asbool\n\nfrom kinto.views import object_exists_or_404\n\n\nclass RecordSchema(resource.ResourceSchema):\n    class Options:\n        preserve_unknown = True\n\n\n_parent_path = '/buckets/{{bucket_id}}/collections/{{collection_id}}'\n\n\n@resource.register(name='record',\n                   collection_path=_parent_path + '/records',\n                   record_path=_parent_path + '/records/{{id}}')\nclass Record(resource.ShareableResource):\n\n    mapping = RecordSchema()\n    schema_field = 'schema'\n\n    def __init__(self, *args, **kwargs):\n        super(Record, self).__init__(*args, **kwargs)\n\n        # Check if already fetched before (in batch).\n        collections = self.request.bound_data.setdefault('collections', {})\n        collection_uri = self.get_parent_id(self.request)\n        if collection_uri not in collections:\n            # Unknown yet, fetch from storage.\n            collection_parent_id = '/buckets/%s' % self.bucket_id\n            collection = object_exists_or_404(self.request,\n                                              collection_id='collection',\n                                              parent_id=collection_parent_id,\n                                              object_id=self.collection_id)\n            collections[collection_uri] = collection\n\n        self._collection = collections[collection_uri]\n\n    def get_parent_id(self, request):\n        self.bucket_id = request.matchdict['bucket_id']\n        self.collection_id = request.matchdict['collection_id']\n        return '/buckets/%s/collections/%s' % (self.bucket_id,\n                                               self.collection_id)\n\n    def is_known_field(self, field_name):\n        \"\"\"Without schema, any field is considered as known.\"\"\"\n        return True\n\n    def process_record(self, new, old=None):\n        \"\"\"Validate records against collection schema, if any.\"\"\"\n        new = super(Record, self).process_record(new, old)\n\n        schema = self._collection.get('schema')\n        settings = self.request.registry.settings\n        schema_validation = 'experimental_collection_schema_validation'\n        if not schema or not asbool(settings.get(schema_validation)):\n            return new\n\n        collection_timestamp = self._collection[self.model.modified_field]\n\n        try:\n            stripped = copy.deepcopy(new)\n            stripped.pop(self.model.id_field, None)\n            stripped.pop(self.model.modified_field, None)\n            stripped.pop(self.model.permissions_field, None)\n            stripped.pop(self.schema_field, None)\n            jsonschema.validate(stripped, schema)\n        except jsonschema_exceptions.ValidationError as e:\n            try:\n                field = e.path.pop() if e.path else e.validator_value.pop()\n            except AttributeError:\n                field = None\n            raise_invalid(self.request, name=field, description=e.message)\n\n        new[self.schema_field] = collection_timestamp\n        return new\n\n    def collection_get(self):\n        result = super(Record, self).collection_get()\n        self._handle_cache_expires(self.request.response)\n        return result\n\n    def get(self):\n        result = super(Record, self).get()\n        self._handle_cache_expires(self.request.response)\n        return result\n\n    def _handle_cache_expires(self, response):\n        \"\"\"If the parent collection defines a ``cache_expires`` attribute,\n        then cache-control response headers are sent.\n\n        .. note::\n\n            Those headers are also sent if the\n            ``kinto.record_cache_expires_seconds`` setting is defined.\n        \"\"\"\n        is_anonymous = Authenticated not in self.request.effective_principals\n        if not is_anonymous:\n            return\n\n        cache_expires = self._collection.get('cache_expires')\n        if cache_expires is None:\n            by_bucket = 'kinto.%s_record_cache_expires_seconds' % (\n                self.bucket_id)\n            by_collection = '%s_%s_record_cache_expires_seconds' % (\n                self.bucket_id, self.collection_id)\n            settings = self.request.registry.settings\n            cache_expires = settings.get(by_collection,\n                                         settings.get(by_bucket))\n\n        if cache_expires is not None:\n            response.cache_expires(seconds=cache_expires)\n", "path": "kinto/views/records.py"}]}