Spaces:
Running
Running
Fix JSON parsing in tree retrieval endpoints
Browse files- Fixed get_trees endpoint to properly parse JSON fields when retrieving from database
- Fixed get_tree endpoint to properly parse JSON fields for individual tree retrieval
- Both endpoints now convert stored JSON strings back to Python objects before response validation
- This resolves the ResponseValidationError when retrieving trees with utility/phenology data
- Trees can now be created, stored, and retrieved without validation errors
app.py
CHANGED
@@ -466,7 +466,33 @@ async def get_trees(
|
|
466 |
params.extend([limit, offset])
|
467 |
|
468 |
cursor.execute(query, params)
|
469 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
470 |
|
471 |
logger.info(f"Retrieved {len(trees)} trees")
|
472 |
return trees
|
@@ -566,7 +592,27 @@ async def get_tree(tree_id: int):
|
|
566 |
detail=f"Tree with ID {tree_id} not found",
|
567 |
)
|
568 |
|
569 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
570 |
|
571 |
except HTTPException:
|
572 |
raise
|
|
|
466 |
params.extend([limit, offset])
|
467 |
|
468 |
cursor.execute(query, params)
|
469 |
+
rows = cursor.fetchall()
|
470 |
+
|
471 |
+
# Parse JSON fields for each tree
|
472 |
+
trees = []
|
473 |
+
for row in rows:
|
474 |
+
tree_data = dict(row)
|
475 |
+
|
476 |
+
# Parse JSON fields back to Python objects
|
477 |
+
if tree_data.get('utility'):
|
478 |
+
try:
|
479 |
+
tree_data['utility'] = json.loads(tree_data['utility'])
|
480 |
+
except (json.JSONDecodeError, TypeError):
|
481 |
+
tree_data['utility'] = None
|
482 |
+
|
483 |
+
if tree_data.get('phenology_stages'):
|
484 |
+
try:
|
485 |
+
tree_data['phenology_stages'] = json.loads(tree_data['phenology_stages'])
|
486 |
+
except (json.JSONDecodeError, TypeError):
|
487 |
+
tree_data['phenology_stages'] = None
|
488 |
+
|
489 |
+
if tree_data.get('photographs'):
|
490 |
+
try:
|
491 |
+
tree_data['photographs'] = json.loads(tree_data['photographs'])
|
492 |
+
except (json.JSONDecodeError, TypeError):
|
493 |
+
tree_data['photographs'] = None
|
494 |
+
|
495 |
+
trees.append(tree_data)
|
496 |
|
497 |
logger.info(f"Retrieved {len(trees)} trees")
|
498 |
return trees
|
|
|
592 |
detail=f"Tree with ID {tree_id} not found",
|
593 |
)
|
594 |
|
595 |
+
# Parse JSON fields back to Python objects
|
596 |
+
tree_data = dict(tree)
|
597 |
+
if tree_data.get('utility'):
|
598 |
+
try:
|
599 |
+
tree_data['utility'] = json.loads(tree_data['utility'])
|
600 |
+
except (json.JSONDecodeError, TypeError):
|
601 |
+
tree_data['utility'] = None
|
602 |
+
|
603 |
+
if tree_data.get('phenology_stages'):
|
604 |
+
try:
|
605 |
+
tree_data['phenology_stages'] = json.loads(tree_data['phenology_stages'])
|
606 |
+
except (json.JSONDecodeError, TypeError):
|
607 |
+
tree_data['phenology_stages'] = None
|
608 |
+
|
609 |
+
if tree_data.get('photographs'):
|
610 |
+
try:
|
611 |
+
tree_data['photographs'] = json.loads(tree_data['photographs'])
|
612 |
+
except (json.JSONDecodeError, TypeError):
|
613 |
+
tree_data['photographs'] = None
|
614 |
+
|
615 |
+
return tree_data
|
616 |
|
617 |
except HTTPException:
|
618 |
raise
|