Ali2206 commited on
Commit
0ee46b5
·
1 Parent(s): 7ca3ae1

Fix ObjectId serialization with comprehensive recursive conversion - Handle all ObjectId fields in nested structures and datetime objects

Browse files
Files changed (1) hide show
  1. app.py +16 -16
app.py CHANGED
@@ -305,27 +305,27 @@ def serialize_checklist(checklist_doc: Dict[str, Any]) -> Dict[str, Any]:
305
  Dictionary with ObjectId converted to string
306
  """
307
  import copy
 
 
308
 
309
  # Make a deep copy to avoid modifying the original document
310
  serialized = copy.deepcopy(checklist_doc)
311
 
312
- if serialized and '_id' in serialized:
313
- serialized['_id'] = str(serialized['_id'])
314
-
315
- # Convert datetime objects to ISO strings
316
- for field in ['createdAt', 'updatedAt', 'verificationDate']:
317
- if field in serialized and serialized[field]:
318
- # Only convert if it's a datetime object, not a string
319
- if hasattr(serialized[field], 'isoformat'):
320
- serialized[field] = serialized[field].isoformat()
 
 
 
321
 
322
- # Convert datetime objects in items
323
- for section in serialized.get('sections', []):
324
- for item in section.get('items', []):
325
- if 'checkedAt' in item and item['checkedAt']:
326
- # Only convert if it's a datetime object, not a string
327
- if hasattr(item['checkedAt'], 'isoformat'):
328
- item['checkedAt'] = item['checkedAt'].isoformat()
329
 
330
  return serialized
331
 
 
305
  Dictionary with ObjectId converted to string
306
  """
307
  import copy
308
+ import json
309
+ from bson import ObjectId
310
 
311
  # Make a deep copy to avoid modifying the original document
312
  serialized = copy.deepcopy(checklist_doc)
313
 
314
+ # Convert ObjectId to string recursively
315
+ def convert_objectid(obj):
316
+ if isinstance(obj, ObjectId):
317
+ return str(obj)
318
+ elif isinstance(obj, dict):
319
+ return {key: convert_objectid(value) for key, value in obj.items()}
320
+ elif isinstance(obj, list):
321
+ return [convert_objectid(item) for item in obj]
322
+ elif hasattr(obj, 'isoformat'): # datetime objects
323
+ return obj.isoformat()
324
+ else:
325
+ return obj
326
 
327
+ # Apply ObjectId conversion to the entire document
328
+ serialized = convert_objectid(serialized)
 
 
 
 
 
329
 
330
  return serialized
331