Conditionally include elements_datatypes when serializing collections

Only requests with a particular media type (used by the history UI) will include this additional field for dataset collections.
This commit is contained in:
davelopez
2022-04-22 13:19:02 +02:00
parent d019bc9a88
commit 1beabe1b88
2 changed files with 27 additions and 0 deletions
@@ -971,6 +971,7 @@ class HistoriesContentsService(ServiceBase):
f"Invalid filter found. When requesting stats, please avoid filtering by {non_orm_filter_keys}"
)
serialization_params = self._handle_extra_serialization_for_media_type(serialization_params, accept)
filter_query_params.order = filter_query_params.order or "hid-asc"
order_by = self.build_order_by(self.history_contents_manager, filter_query_params.order)
contents = self.history_contents_manager.contents(
@@ -999,6 +1000,18 @@ class HistoriesContentsService(ServiceBase):
return HistoryContentsWithStatsResult.construct(contents=items, stats=stats)
return HistoryContentsResult.construct(__root__=items)
def _handle_extra_serialization_for_media_type(
self,
serialization_params: SerializationParams,
request_media_type: str,
) -> SerializationParams:
"""According to the requested media type the response may include extra information."""
if request_media_type == HistoryContentsWithStatsResult.__accept_type__:
if not serialization_params.keys:
serialization_params.keys = []
serialization_params.keys.append("elements_datatypes")
return serialization_params
def _serialize_legacy_content_item(
self,
trans,
@@ -1138,6 +1138,20 @@ class HistoryContentsApiBulkOperationTestCase(ApiTestCase):
)
self._assert_status_code_is(response, 400)
def test_index_with_stats_has_extra_serialization(self):
expected_extra_keys_in_collections = ["elements_datatypes"]
with self.dataset_populator.test_history() as history_id:
self._create_collection_in_history(history_id)
response = self._get_contents_with_stats(
history_id,
search_query="&q=history_content_type-eq&qv=dataset_collection",
)
self._assert_status_code_is(response, 200)
contents_with_stats = response.json()
assert contents_with_stats["contents"]
collection = contents_with_stats["contents"][0]
self._assert_has_keys(collection, *expected_extra_keys_in_collections)
def _get_contents_with_stats(self, history_id: str, search_query: str = ""):
headers = {"accept": "application/vnd.galaxy.history.contents.stats+json"}
search_response = self._get(f"histories/{history_id}/contents?v=dev{search_query}", headers=headers)