diff --git a/poem-openapi/src/types/external/bson.rs b/poem-openapi/src/types/external/bson.rs index f51e0fdbee..cec396ff3f 100644 --- a/poem-openapi/src/types/external/bson.rs +++ b/poem-openapi/src/types/external/bson.rs @@ -62,7 +62,7 @@ impl ParseFromMultipartField for ObjectId { impl ToJSON for ObjectId { fn to_json(&self) -> Option { - Some(serde_json::to_value(self).unwrap()) + serde_json::to_value(self).ok() } } diff --git a/poem-openapi/src/types/external/floats.rs b/poem-openapi/src/types/external/floats.rs index cb387f3874..7f80cfe5f1 100644 --- a/poem-openapi/src/types/external/floats.rs +++ b/poem-openapi/src/types/external/floats.rs @@ -71,7 +71,7 @@ macro_rules! impl_type_for_floats { impl ToJSON for $ty { fn to_json(&self) -> Option { - Some(Value::Number(Number::from_f64(*self as f64).unwrap())) + Number::from_f64(*self as f64).map(Value::Number) } } diff --git a/poem-openapi/src/types/external/time.rs b/poem-openapi/src/types/external/time.rs index 51f0dc37c8..1d34d19006 100644 --- a/poem-openapi/src/types/external/time.rs +++ b/poem-openapi/src/types/external/time.rs @@ -69,7 +69,7 @@ impl ParseFromMultipartField for OffsetDateTime { impl ToJSON for OffsetDateTime { fn to_json(&self) -> Option { - Some(Value::String(self.format(&Rfc3339).unwrap_or_default())) + self.format(&Rfc3339).ok().map(Value::String) } } @@ -129,7 +129,7 @@ macro_rules! impl_naive_datetime_types { impl ToJSON for $ty { fn to_json(&self) -> Option { - Some(Value::String(self.format($format_description).unwrap())) + self.format($format_description).ok().map(Value::String) } } };