diff --git a/datagateway_api/src/search_api/models.py b/datagateway_api/src/search_api/models.py index 2fc1e3ad..4b8eebc3 100644 --- a/datagateway_api/src/search_api/models.py +++ b/datagateway_api/src/search_api/models.py @@ -111,6 +111,19 @@ def from_icat(cls, icat_data, required_related_fields): # noqa: B902, N805 # we have to get hold of its class definition and call its `from_icat` # method to create an instance of itself with the ICAT data provided. # Doing this allows for recursion. + + if entity_field_alias not in [ + required_related_field.split(".")[0] + for required_related_field in required_related_fields + ]: + # Before proceeding, check if the related entity really needs to be created. + # Do not attempt to create the related entity if ICAT data for it is available + # but the entity has not been specified to be included. In such cases, the ICAT + # data is likely available because the data for another entity field is + # retrieved via that ICAT entity. We do not want to return data for related + # entities unless explicitly specified to be included by the user. + continue + data = ( [field_value] if not isinstance(field_value, list) else field_value ) diff --git a/test/search_api/test_models.py b/test/search_api/test_models.py index f88d0ec1..222e9c41 100644 --- a/test/search_api/test_models.py +++ b/test/search_api/test_models.py @@ -411,8 +411,6 @@ def test_from_icat_document_entity_with_data_for_all_related_entities(self): expected_entity_data["pid"] = f"pid:{INVESTIGATION_ICAT_DATA['id']}" expected_entity_data["doi"] = None expected_entity_data["datasets"] = [DATASET_PANOSC_DATA, DATASET_PANOSC_DATA] - expected_entity_data["members"] = [MEMBER_PANOSC_DATA] - expected_entity_data["parameters"] = [PARAMETER_PANOSC_DATA] icat_data = INVESTIGATION_ICAT_DATA.copy() icat_data["doi"] = None @@ -434,7 +432,6 @@ def test_from_icat_file_entity_without_data_for_related_entities(self): def test_from_icat_file_entity_with_data_for_all_related_entities(self): expected_entity_data = FILE_PANOSC_DATA.copy() - expected_entity_data["dataset"] = DATASET_PANOSC_DATA icat_data = DATAFILE_ICAT_DATA.copy() icat_data["dataset"] = DATASET_ICAT_DATA