Skip to content

Commit

Permalink
Merge pull request #390 from ral-facilities/bugfix/related-entities-c…
Browse files Browse the repository at this point in the history
…reation-#387

Creation of related entities attempted when not specified to be included
  • Loading branch information
VKTB authored Nov 23, 2022
2 parents d7a5a16 + fcbfb0f commit 1e6c600
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
13 changes: 13 additions & 0 deletions datagateway_api/src/search_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
3 changes: 0 additions & 3 deletions test/search_api/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 1e6c600

Please sign in to comment.