| 1397 | } |
| 1398 | |
| 1399 | fn sanitize_gemini(obj: serde_json::Value) -> serde_json::Value { |
| 1400 | use serde_json::{json, Map, Value}; |
| 1401 | |
| 1402 | match obj { |
| 1403 | Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => obj, |
| 1404 | Value::Array(arr) => Value::Array(arr.into_iter().map(sanitize_gemini).collect()), |
| 1405 | Value::Object(map) => { |
| 1406 | let mut result = Map::new(); |
| 1407 | |
| 1408 | for (key, value) in map { |
| 1409 | if key == "enum" { |
| 1410 | if let Value::Array(ref enum_vals) = value { |
| 1411 | // Convert all enum values to strings |
| 1412 | let string_vals: Vec<Value> = enum_vals |
| 1413 | .iter() |
| 1414 | .map(|v| match v { |
| 1415 | Value::String(s) => Value::String(s.clone()), |
| 1416 | other => Value::String(other.to_string()), |
| 1417 | }) |
| 1418 | .collect(); |
| 1419 | result.insert(key, Value::Array(string_vals)); |
| 1420 | |
| 1421 | // If we have integer/number type with enum, change to string |
| 1422 | if let Some(Value::String(t)) = result.get("type") { |
| 1423 | if t == "integer" || t == "number" { |
| 1424 | result.insert( |
| 1425 | "type".to_string(), |
| 1426 | Value::String("string".to_string()), |
| 1427 | ); |
| 1428 | } |
| 1429 | } |
| 1430 | } else { |
| 1431 | result.insert(key, value); |
| 1432 | } |
| 1433 | } else if value.is_object() || value.is_array() { |
| 1434 | result.insert(key, sanitize_gemini(value)); |
| 1435 | } else { |
| 1436 | result.insert(key, value); |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | // Also check if type was set before enum was processed |
| 1441 | // (enum might appear before type in iteration order) |
| 1442 | if let Some(Value::Array(ref enum_vals)) = result.get("enum") { |
| 1443 | if !enum_vals.is_empty() { |
| 1444 | if let Some(Value::String(t)) = result.get("type") { |
| 1445 | if t == "integer" || t == "number" { |
| 1446 | result.insert("type".to_string(), Value::String("string".to_string())); |
| 1447 | } |
| 1448 | } |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | // Filter required array to only include fields in properties |
| 1453 | if result.get("type") == Some(&json!("object")) { |
| 1454 | if let (Some(Value::Object(ref props)), Some(Value::Array(ref required))) = |
| 1455 | (result.get("properties"), result.get("required")) |
| 1456 | { |