fix: deserialize scheduled_at_ms as string or number (postgres bigint serializes as string)

This commit is contained in:
M1 2026-03-18 17:17:29 +04:00
parent c5eb514990
commit 980261632e
1 changed files with 14 additions and 1 deletions

View File

@ -1,7 +1,19 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value; use serde_json::Value;
use std::collections::HashMap; use std::collections::HashMap;
// Postgres BIGINT sometimes comes back as a JSON string — handle both
fn deserialize_ms<'de, D: Deserializer<'de>>(d: D) -> Result<Option<i64>, D::Error> {
use serde::de::Error;
let v: Option<serde_json::Value> = Option::deserialize(d)?;
match v {
None => Ok(None),
Some(serde_json::Value::Number(n)) => Ok(n.as_i64()),
Some(serde_json::Value::String(s)) => s.parse::<i64>().map(Some).map_err(D::Error::custom),
Some(other) => Err(D::Error::custom(format!("unexpected type for scheduled_at_ms: {other}"))),
}
}
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct Monitor { pub struct Monitor {
pub id: String, pub id: String,
@ -13,6 +25,7 @@ pub struct Monitor {
pub interval_s: i64, pub interval_s: i64,
pub query: Option<Value>, pub query: Option<Value>,
pub scheduled_at: Option<String>, // ISO string for backward compat in PingResult pub scheduled_at: Option<String>, // ISO string for backward compat in PingResult
#[serde(deserialize_with = "deserialize_ms")]
pub scheduled_at_ms: Option<i64>, // unix ms from API for precise scheduling pub scheduled_at_ms: Option<i64>, // unix ms from API for precise scheduling
pub regions: Option<Vec<String>>, pub regions: Option<Vec<String>>,
} }