From 6f1d0b3ad9b9bd86252cc0d08b0fb3b7eda30d11 Mon Sep 17 00:00:00 2001 From: Chenxiong Qi Date: Aug 11 2019 14:18:38 +0000 Subject: Return empty result if short=true is specified on empty list of builds Fixes #1376 Signed-off-by: Chenxiong Qi --- diff --git a/module_build_service/views.py b/module_build_service/views.py index ae73d35..af15b5e 100644 --- a/module_build_service/views.py +++ b/module_build_service/views.py @@ -125,7 +125,7 @@ class AbstractQueryableBuildAPI(MethodView): json_func_kwargs["show_state_url"] = True json_func_kwargs["api_version"] = api_version elif short_flag == "true" or short_flag == "1": - if hasattr(p_query.items[0], "short_json"): + if p_query.items and hasattr(p_query.items[0], "short_json"): json_func_name = "short_json" if json_func_name == "json" or json_func_name == "extended_json": # Only ModuleBuild.json and ModuleBuild.extended_json has argument db_session diff --git a/tests/test_views/test_views.py b/tests/test_views/test_views.py index 33b33c9..4523815 100644 --- a/tests/test_views/test_views.py +++ b/tests/test_views/test_views.py @@ -524,6 +524,12 @@ class TestViews: assert rv.status_code == 400 assert results == expected_error + def test_query_builds_get_short_json_from_empty_list_of_builds(self): + rv = self.client.get("/module-build-service/1/module-builds/?name=pkgname&short=true") + data = json.loads(rv.data) + assert [] == data["items"] + assert 0 == data["meta"]["total"] + def test_query_component_build(self): rv = self.client.get("/module-build-service/1/component-builds/1") data = json.loads(rv.data)