Skip to content

Commit e645e07

Browse files
committed
Fix Pydantic serialization warning when updating text structure
1 parent 26a5643 commit e645e07

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

Tekst-API/tekst/routers/texts.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
status,
1717
)
1818
from fastapi.responses import FileResponse
19+
from pydantic import TypeAdapter, ValidationError
1920
from starlette.background import BackgroundTask
2021

2122
from tekst import errors, tasks
@@ -269,6 +270,11 @@ async def _update_text_structure_task(
269270
updated_docs = []
270271
last_text_id = None
271272
all_locs_same_text = True
273+
label_field_adapter = TypeAdapter(LocationDocument.model_fields["label"].annotation)
274+
aliases_field_adapter = TypeAdapter(
275+
LocationDocument.model_fields["aliases"].annotation
276+
)
277+
272278
for loc in location_updates:
273279
try:
274280
doc_id = PydanticObjectId(loc["id"])
@@ -298,11 +304,12 @@ async def _update_text_structure_task(
298304
# modify label and aliases according to updates (and nothing else!)
299305
try:
300306
if "label" in loc:
307+
label_field_adapter.validate_python(loc["label"])
301308
loc_doc.label = loc["label"]
302309
if "aliases" in loc:
310+
aliases_field_adapter.validate_python(loc["aliases"])
303311
loc_doc.aliases = loc["aliases"]
304-
LocationDocument.model_validate(loc_doc.model_dump())
305-
except Exception as e:
312+
except ValidationError as e:
306313
raise errors.update_values(
307314
exc=errors.E_422_UPLOAD_INVALID_DATA,
308315
values={"errors": str(e)},

0 commit comments

Comments
 (0)