Project

Profile

Help

Issue #312

closed

It is impossible to use filters with GET search requests, which makes the name of the REST view a bit of a lie

Added by rbarlow almost 10 years ago. Updated over 4 years ago.

Status:
CLOSED - CURRENTRELEASE
Priority:
Normal
Category:
-
Sprint/Milestone:
-
Start date:
Due date:
Estimated time:
Severity:
3. High
Version:
Platform Release:
2.7.2
OS:
Triaged:
Yes
Groomed:
No
Sprint Candidate:
No
Tags:
Easy Fix, Pulp 2
Sprint:
Quarter:

Description

In converting our search API to Django, I found that it is impossible to search with filters in a GET request, despite the documentation saying it is possible[0].

$ http --json -a admin:admin --verify=no GET https://localhost/pulp/api/v2/tasks/search/?filters=\{\"state\"\:\"running\"\}\&field=task_id\&limit=1 WebFrameworkSwitch:webpy
/usr/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py:730: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html (This warning will only appear once by default.)
  InsecureRequestWarning)
HTTP/1.1 400 Bad Request
Connection: close
Content-Encoding: utf-8
Content-Length: 378
Content-Type: application/json
Date: Wed, 25 Feb 2015 16:46:32 GMT
Server: Apache/2.4.6 (Red Hat) OpenSSL/1.0.1e-fips mod_wsgi/3.4 Python/2.7.5

{
    "_href": "/pulp/api/v2/tasks/search/", 
    "error": {
        "code": "PLP0015", 
        "data": {
            "properties": "['filters']", 
            "property_names": [
                "filters"
            ]
        }, 
        "description": "Invalid properties: ['filters']", 
        "sub_errors": []
    }, 
    "error_message": "Invalid properties: ['filters']", 
    "exception": null, 
    "http_request_method": "GET", 
    "http_status": 400, 
    "property_names": [
        "filters"
    ], 
    "traceback": null
}

I expect this behavior instead:

$ http --json -a admin:admin --verify=no GET https://localhost/pulp/api/v2/tasks/search/?filters=\{\"task_id\"\:\"e29c1188-892c-44d6-ba3a-2742beb40423\"\}\&field=task_id WebFrameworkSwitch:webpy
/usr/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py:730: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html (This warning will only appear once by default.)
  InsecureRequestWarning)
HTTP/1.1 200 OK
Connection: Keep-Alive
Content-Length: 449
Content-Type: application/json
Date: Wed, 25 Feb 2015 17:02:09 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.6 (Red Hat) OpenSSL/1.0.1e-fips mod_wsgi/3.4 Python/2.7.5

[
    {
        "_href": "/pulp/api/v2/tasks/e29c1188-892c-44d6-ba3a-2742beb40423/", 
        "_id": {
            "$oid": "54ed4356a568cca370f63bdc"
        }, 
        "_ns": "task_status", 
        "error": null, 
        "exception": null, 
        "finish_time": null, 
        "id": "54ed4356a568cca370f63bdc", 
        "progress_report": {}, 
        "result": null, 
        "spawned_tasks": [], 
        "start_time": null, 
        "state": "waiting", 
        "tags": [], 
        "task_id": "e29c1188-892c-44d6-ba3a-2742beb40423", 
        "task_type": null, 
        "traceback": null, 
        "worker_name": null
    }
]

I dug into the code for a bit and determined that the get handler was passing the "filters" parameter as a string instead of as a dictionary to the Criteria object, which is not valid. It is the Criteria object that raises the error message "Invalid properties: ['filters']". The fix is a one liner! All we need to do is to insert this into the SearchView's get() handler before it hands the query to the Criteria.from_client_input() method:

query['filters'] = json.loads(query['filters'])

I considered fixing this along with my conversion to Django, but in the interest of making a pure conversion, I've decided to handle this with a bug report and separate pull request instead.

In my opinion, this is important to fix. It is not very RESTful to search via a POST method, but currently it is impossible to search via a GET method, which is the intended method for read operations.

[0] http://pulp.readthedocs.org/en/latest/dev-guide/conventions/criteria.html#search-api


Related issues

Related to Pulp - Issue #1040: Search with Get does not workCLOSED - CURRENTRELEASEamacdona@redhat.comActions
Blocks Pulp - Issue #1332: in Search API, restritcting fields does not work with MongoengineCLOSED - CURRENTRELEASEamacdona@redhat.comActions

Also available in: Atom PDF