Skip to content

Commit fd2f29c

Browse files
committed
Fix minor bugs
- add TODO for issue found with post and patch requesr #525
1 parent 2492b49 commit fd2f29c

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

datagateway_api/src/common/date_handler.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,22 @@ class DateHandler:
1515
def is_str_a_date(potential_date):
1616
"""
1717
This function identifies if a string contains a date. This function doesn't
18-
detect which format the date is, just if there's a date or not.
18+
detect which format the date is, just if there's a date or not. An additional
19+
check is performed to ensure purely numeric strings (e.g. "5", "20200101") are
20+
not incorrectly treated as dates.
21+
1922
2023
:param potential_date: String data that could contain a date of any format
2124
:type potential_date: :class:`str`
2225
:return: Boolean to signify whether `potential_date` is a date or not
2326
"""
2427

28+
text = potential_date.strip()
29+
30+
# Reject if the string is just digits (like "5" or "20200101")
31+
if text.isdigit():
32+
return False
33+
2534
try:
2635
# Disabled fuzzy to avoid picking up dates in things like descriptions etc.
2736
parse(potential_date, fuzzy=False)

datagateway_api/src/datagateway_api/build_models.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,16 @@ def build_datagateway_api_model(**kwargs):
144144
post_type = ICATId
145145

146146
patch_type = Optional[post_type]
147+
rel_type_str = f"Optional[{rel_type_str}]"
147148
if not field.notNullable:
148-
rel_type_str = f"Optional[{rel_type_str}]"
149149
post_type = Optional[post_type]
150150

151151
description = getattr(field, "comment", None)
152152
field_metadata = Field(description=description)
153153
annotated_type = Annotated[rel_type_str, field_metadata]
154154
post_annotated_type = Annotated[post_type, field_metadata]
155155
patch_annotated_type = Annotated[patch_type, field_metadata]
156-
fields[field.name] = (
157-
(annotated_type, None) if not field.notNullable else annotated_type
158-
)
156+
fields[field.name] = (annotated_type, None)
159157
post_fields[field.name] = (
160158
(post_annotated_type, None)
161159
if not field.notNullable
@@ -165,7 +163,7 @@ def build_datagateway_api_model(**kwargs):
165163

166164
model = create_model(name, __base__=ICATBaseEntity, **fields)
167165
post_model = create_model(post_name, **post_fields)
168-
patch_model = create_model(patch_name, **patch_fields)
166+
patch_model = create_model(patch_name, __base__=ICATId, **patch_fields)
169167
datagateway_api_models[name] = model
170168
datagateway_api_models[post_name] = post_model
171169
datagateway_api_models[patch_name] = patch_model

datagateway_api/src/datagateway_api/icat/helpers.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,24 @@ def create_entities(client, entity_type, data):
573573
else:
574574
# This means the attribute has a relationship with another object
575575
try:
576-
related_object = client.get(entity_info.type, value)
576+
# TODO:
577+
# The field "value" can be either List[{"id": 1}] or {"id": 1},
578+
# but only the single-object case works correctly.
579+
#
580+
# When a field requires a list of objects, the API fails
581+
# because the list type is not handled during creation.
582+
#
583+
# Even when forcing it to work by using the wrong type, the
584+
# GET request still does not return the one-to-one related
585+
# values (e.g. "Facility f INCLUDE f.parameterTypes").
586+
#
587+
# After attempting to fix the GET behaviour, the create
588+
# operation now throws a duplicate reference error when
589+
# saving related entities.
590+
#
591+
# Fix list handling, one-to-one include behaviour, and
592+
# duplicate reference errors.
593+
related_object = client.get(entity_info.type, value["id"])
577594
except ICATNoObjectError as e:
578595
raise BadRequestError(e) from e
579596
if entity_info.relType.lower() == "many":

0 commit comments

Comments
 (0)