Skip to content

Commit eae7ddd

Browse files
enhanced the response sanitization
1 parent 1d0fea5 commit eae7ddd

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

src/api/api/api_routes.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,11 @@ async def fetch_chart_data_with_filters(chart_filters: ChartFilters):
7676
{"status": "success", "filters": chart_filters.model_dump()}
7777
)
7878
# Sanitize the response to handle NaN and Infinity values
79-
def sanitize(obj):
80-
if isinstance(obj, dict):
81-
return {k: sanitize(v) for k, v in obj.items()}
82-
elif isinstance(obj, list):
83-
return [sanitize(v) for v in obj]
84-
elif isinstance(obj, float) and (math.isnan(obj) or math.isinf(obj)):
85-
return None
86-
else:
87-
return obj
88-
response = sanitize(response)
79+
for record in response:
80+
if isinstance(record.get("chart_value"), list):
81+
for item in record["chart_value"]:
82+
if isinstance(item.get("value"), float) and (math.isnan(item["value"]) or math.isinf(item["value"])):
83+
item["value"] = None
8984
return JSONResponse(content=response)
9085
except Exception as e:
9186
logger.exception("Error in fetch_chart_data_with_filters: %s", str(e))

0 commit comments

Comments
 (0)