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
This commit is contained in:
Kevin Benton 2017-08-30 20:14:20 -07:00
parent 8d2c1bd88b
commit 700d609ace
2 changed files with 16 additions and 3 deletions

View File

@ -177,11 +177,13 @@ class NeutronPecanController(object):
def build_field_list(self, request_fields): def build_field_list(self, request_fields):
added_fields = [] added_fields = []
combined_fields = [] combined_fields = []
if request_fields: req_fields_set = {f for f in request_fields if f}
req_fields_set = set(request_fields) if req_fields_set:
added_fields = self._mandatory_fields - req_fields_set added_fields = self._mandatory_fields - req_fields_set
combined_fields = req_fields_set | self._mandatory_fields 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 @property
def plugin(self): def plugin(self):

View File

@ -375,6 +375,17 @@ class TestResourceController(TestRootController):
self._check_item(['id', 'tenant_id'], self._check_item(['id', 'tenant_id'],
jsonutils.loads(item_resp.body)['port']) 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): def test_post(self):
response = self.app.post_json( response = self.app.post_json(
'/v2.0/ports.json', '/v2.0/ports.json',