From 0fa09355e764d6ef560c9944de423c746b79877c Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Mon, 22 Jul 2024 18:41:23 +0200 Subject: [PATCH] Improve typing for archived histories API models Drop Any and use partial model for custom keys. Consolidate archived histories serialization with regular histories. --- lib/galaxy/schema/schema.py | 24 ++++++++++++++----- lib/galaxy/webapps/galaxy/api/histories.py | 2 ++ .../webapps/galaxy/services/histories.py | 14 ++++++----- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/lib/galaxy/schema/schema.py b/lib/galaxy/schema/schema.py index 9cdf22507164..e8c83b4f9a62 100644 --- a/lib/galaxy/schema/schema.py +++ b/lib/galaxy/schema/schema.py @@ -1890,12 +1890,24 @@ class ArchivedHistoryDetailed(HistoryDetailed, ExportAssociationData): pass -AnyArchivedHistoryView = Union[ - ArchivedHistorySummary, - ArchivedHistoryDetailed, - # Any will cover those cases in which only specific `keys` are requested - # otherwise the validation will fail because the required fields are not returned - Any, +@partial_model() +class CustomArchivedHistoryView(CustomHistoryView, ExportAssociationData): + """Archived History Response with all optional fields. + + It is used for serializing only specific attributes using the "keys" + query parameter. + """ + + pass + + +AnyArchivedHistoryView = Annotated[ + Union[ + CustomArchivedHistoryView, + ArchivedHistoryDetailed, + ArchivedHistorySummary, + ], + Field(union_mode="left_to_right"), ] diff --git a/lib/galaxy/webapps/galaxy/api/histories.py b/lib/galaxy/webapps/galaxy/api/histories.py index 067dd425369a..3f9bdfd7b829 100644 --- a/lib/galaxy/webapps/galaxy/api/histories.py +++ b/lib/galaxy/webapps/galaxy/api/histories.py @@ -271,6 +271,7 @@ def shared_with_me( @router.get( "/api/histories/archived", summary="Get a list of all archived histories for the current user.", + response_model_exclude_unset=True, ) def get_archived_histories( self, @@ -613,6 +614,7 @@ def get_custom_builds_metadata( @router.post( "/api/histories/{history_id}/archive", summary="Archive a history.", + response_model_exclude_unset=True, ) def archive_history( self, diff --git a/lib/galaxy/webapps/galaxy/services/histories.py b/lib/galaxy/webapps/galaxy/services/histories.py index d5a6a75ffba7..53db9a8dc675 100644 --- a/lib/galaxy/webapps/galaxy/services/histories.py +++ b/lib/galaxy/webapps/galaxy/services/histories.py @@ -793,7 +793,10 @@ def get_archived_histories( filters=filters, order_by=order_by, limit=filter_query_params.limit, offset=filter_query_params.offset ) - histories = [self._serialize_archived_history(trans, history, serialization_params) for history in histories] + histories = [ + self._serialize_archived_history(trans, history, serialization_params, default_view="summary") + for history in histories + ] return histories, total_matches def _serialize_archived_history( @@ -801,14 +804,13 @@ def _serialize_archived_history( trans: ProvidesHistoryContext, history: model.History, serialization_params: Optional[SerializationParams] = None, + default_view: str = "detailed", ): if serialization_params is None: - serialization_params = SerializationParams(default_view="summary") - archived_history = self.serializer.serialize_to_view( - history, user=trans.user, trans=trans, **serialization_params.dict() - ) + serialization_params = SerializationParams() + archived_history = self._serialize_history(trans, history, serialization_params, default_view) export_record_data = self._get_export_record_data(history) - archived_history["export_record_data"] = export_record_data.dict() if export_record_data else None + archived_history["export_record_data"] = export_record_data.model_dump() if export_record_data else None return archived_history def _get_export_record_data(self, history: model.History) -> Optional[WriteStoreToPayload]: