inveniosoftware / invenio-records-rest

Invenio records REST API module.
https://invenio-records-rest.readthedocs.io
MIT License
4 stars 62 forks source link

PUT opration control over "control_number" field #195

Closed ppanero closed 5 years ago

ppanero commented 6 years ago

When performing a PUT opration e.g.

curl -X PUT -H 'Content-Type: application/json' -H 'Accept: application/json' -i 'http://<host>:5000/api/record/5' --data '
       {
        "description": "This is an awesome updated description",
        "title": "Update Test 1"
       }
       '

If the new body does not contain the _controlnumber field the record is updated correctly, failing afterwards in the search phase due to the lack of control_number in the body:

Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/lib/python2.7/site-packages/werkzeug/wsgi.py", line 826, in __call__
    return app(environ, start_response)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/lib/python2.7/site-packages/flask/views.py", line 84, in view
    return self.dispatch_request(*args, **kwargs)
  File "/usr/lib/python2.7/site-packages/invenio_rest/views.py", line 253, in dispatch_request
    *args, **kwargs
  File "/usr/lib/python2.7/site-packages/flask/views.py", line 149, in dispatch_request
    return meth(*args, **kwargs)
  File "/usr/lib/python2.7/site-packages/invenio_records_rest/views.py", line 540, in get
    item_links_factory=self.item_links_factory,
  File "/usr/lib/python2.7/site-packages/invenio_rest/views.py", line 234, in make_response
    return serializer(*args, **kwargs)
  File "/usr/lib/python2.7/site-packages/invenio_records_rest/serializers/response.py", line 72, in view
    item_links_factory=item_links_factory),
  File "/usr/lib/python2.7/site-packages/invenio_records_rest/serializers/json.py", line 86, in serialize_search
    ) for hit in search_result['hits']['hits']],
  File "/usr/lib/python2.7/site-packages/invenio_pidstore/fetchers.py", line 64, in recid_fetcher
    pid_value=str(data['control_number']),
KeyError: 'control_number'

I see three possible solutions:

1- State this fact in the documentation so users are aware 2- Control the existence of this field in the PUT operation and return a 400 if this field does not exist (rejecting the update) 3- Control the existence of this field in the PUT operation and add it with the id coming in the request URL if it does not exist

In addition a check to match the URL and data control_number could be performed to avoid inconsistencies.

I can work in a PR for this, just let me know which is the preferred solution conceptual/framework wise.

ppanero commented 5 years ago

Fix by https://github.com/inveniosoftware/invenio-records-rest/pull/234