summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.opendev.org>2020-02-26 14:08:37 +0000
committerGerrit Code Review <review@openstack.org>2020-02-26 14:08:37 +0000
commit7aa69ff319875c7e97b49b8c2e139316c1c43c28 (patch)
treee9ab4c115fe7d91365e44ceed07454513a120c4c
parent7644237d1a5af01a479638a672ebf6b82a8bd8c1 (diff)
parent3750587833310aafdc0f6948a630ca3afe6bea0a (diff)
downloadneutron-7aa69ff319875c7e97b49b8c2e139316c1c43c28.tar.gz
Merge "Fix bug: AttributeError arises while sorting with standard attributes" into stable/rockyrocky-em13.0.7
-rw-r--r--neutron/api/api_common.py4
-rw-r--r--neutron/tests/unit/api/v2/test_base.py19
-rw-r--r--releasenotes/notes/add-sort-keys-check-for-get-sorts-b9e3e86ddcb3bc3a.yaml6
3 files changed, 26 insertions, 3 deletions
diff --git a/neutron/api/api_common.py b/neutron/api/api_common.py
index 1092601c26..7daa8f815c 100644
--- a/neutron/api/api_common.py
+++ b/neutron/api/api_common.py
@@ -216,7 +216,9 @@ def get_sorts(request, attr_info):
msg = _("The number of sort_keys and sort_dirs must be same")
raise exc.HTTPBadRequest(explanation=msg)
valid_dirs = [constants.SORT_DIRECTION_ASC, constants.SORT_DIRECTION_DESC]
- absent_keys = [x for x in sort_keys if x not in attr_info]
+ valid_sort_keys = set(attr for attr, schema in attr_info.items()
+ if schema.get('is_sort_key', False))
+ absent_keys = [x for x in sort_keys if x not in valid_sort_keys]
if absent_keys:
msg = _("%s is invalid attribute for sort_keys") % absent_keys
raise exc.HTTPBadRequest(explanation=msg)
diff --git a/neutron/tests/unit/api/v2/test_base.py b/neutron/tests/unit/api/v2/test_base.py
index 902733b36f..41874c219c 100644
--- a/neutron/tests/unit/api/v2/test_base.py
+++ b/neutron/tests/unit/api/v2/test_base.py
@@ -1560,7 +1560,10 @@ class SortingTestCase(base.BaseTestCase):
def test_get_sorts(self):
path = '/?sort_key=foo&sort_dir=desc&sort_key=bar&sort_dir=asc'
request = webob.Request.blank(path)
- attr_info = {'foo': {'key': 'val'}, 'bar': {'key': 'val'}}
+ attr_info = {
+ 'foo': {'key': 'val', 'is_sort_key': True},
+ 'bar': {'key': 'val', 'is_sort_key': True}
+ }
expect_val = [('foo', False), ('bar', True)]
actual_val = api_common.get_sorts(request, attr_info)
self.assertEqual(expect_val, actual_val)
@@ -1568,11 +1571,23 @@ class SortingTestCase(base.BaseTestCase):
def test_get_sorts_with_project_id(self):
path = '/?sort_key=project_id&sort_dir=desc'
request = webob.Request.blank(path)
- attr_info = {'tenant_id': {'key': 'val'}}
+ attr_info = {'tenant_id': {'key': 'val', 'is_sort_key': True}}
expect_val = [('project_id', False)]
actual_val = api_common.get_sorts(request, attr_info)
self.assertEqual(expect_val, actual_val)
+ def test_get_sorts_with_non_sort_key(self):
+ path = '/?sort_key=created_at&sort_dir=desc'
+ request = webob.Request.blank(path)
+ attr_info = {
+ 'foo': {'key': 'val', 'is_sort_key': True},
+ 'bar': {'key': 'val', 'is_sort_key': True},
+ 'created_at': {'key': 'val'}
+ }
+ self.assertRaises(exc.HTTPBadRequest,
+ api_common.get_sorts,
+ request, attr_info)
+
class FiltersTestCase(base.BaseTestCase):
def test_all_skip_args(self):
diff --git a/releasenotes/notes/add-sort-keys-check-for-get-sorts-b9e3e86ddcb3bc3a.yaml b/releasenotes/notes/add-sort-keys-check-for-get-sorts-b9e3e86ddcb3bc3a.yaml
new file mode 100644
index 0000000000..0f77727238
--- /dev/null
+++ b/releasenotes/notes/add-sort-keys-check-for-get-sorts-b9e3e86ddcb3bc3a.yaml
@@ -0,0 +1,6 @@
+---
+fixes:
+ - |
+ Add sort-keys validation logic to method ``get_sorts`` in
+ ``neutron.api.api_common``. See the link below for more:
+ https://bugs.launchpad.net/neutron/+bug/1659175