Pecan: strip duplicate and empty user fields

Remove duplicated and empty fields from users requests
in Pecan to preserve the old legacy API controller behavior.

Closes-Bug: #1714384
Change-Id: I1afc24b146a8fcc6c8ebae708f32dd7c1795292e
(cherry picked from commit 700d609ace)
This commit is contained in:
Kevin Benton 2017-08-30 20:14:20 -07:00
parent 671cbad9b9
commit 6fe51999d5
2 changed files with 16 additions and 3 deletions

View File

@ -181,11 +181,13 @@ class NeutronPecanController(object):
def build_field_list(self, request_fields):
added_fields = []
combined_fields = []
if request_fields:
req_fields_set = set(request_fields)
req_fields_set = {f for f in request_fields if f}
if req_fields_set:
added_fields = self._mandatory_fields - req_fields_set
combined_fields = req_fields_set | self._mandatory_fields
return list(combined_fields), list(added_fields)
# field sorting is to match old behavior of legacy API and to make
# this drop-in compatible with the old API unit tests
return sorted(combined_fields), list(added_fields)
@property
def plugin(self):

View File

@ -386,6 +386,17 @@ class TestResourceController(TestRootController):
self._check_item(['id', 'tenant_id'],
jsonutils.loads(item_resp.body)['port'])
def test_duped_and_empty_fields_stripped(self):
mock_get = mock.patch.object(self.plugin, 'get_ports',
return_value=[]).start()
self.app.get(
'/v2.0/ports.json?fields=id&fields=name&fields=&fields=name',
headers={'X-Project-Id': 'tenid'}
)
received = mock_get.mock_calls[-1][2]['fields']
self.assertNotIn('', received)
self.assertEqual(len(received), len(set(received)))
def test_post(self):
response = self.app.post_json(
'/v2.0/ports.json',