diff --git a/src/openapi.rs b/src/openapi.rs index 97b63b1..ef6d02a 100644 --- a/src/openapi.rs +++ b/src/openapi.rs @@ -517,84 +517,48 @@ handwritten_schema!( handwritten_schema!( ServerStatus, "ServerStatus", - ObjectBuilder::new() - .description(Some( - "\"Server up\": a TCP connect to the peer's host and port." - )) - .required("up") - .required("since") - .required("last_checked") - .required("last_error") - .required("latency_ms") - .property("up", ObjectBuilder::new().schema_type(Type::Boolean)) - .property( - "since", - nullable(Type::String) - .format(Some(date_time())) - .description(Some(SINCE_DOC)), - ) - .property( - "last_checked", - nullable(Type::String).format(Some(date_time())) - ) - .property( - "last_error", - nullable(Type::String).description(Some( - "Failure class such as `timeout` or `unreachable`; never a URL or a secret.", - )), - ) - .property( - "latency_ms", - nullable(Type::Integer).minimum(Some(utoipa::Number::UInt(0))), - ) - .into() + check_properties( + ObjectBuilder::new() + .description(Some( + "\"Server up\": a TCP connect to the peer's host and port." + )) + .required("up") + .required("since") + .required("last_checked") + .required("last_error") + .required("latency_ms"), + "Failure class such as `timeout` or `unreachable`; never a URL or a secret.", + ) + .into() ); handwritten_schema!( ApiStatus, "ApiStatus", - ObjectBuilder::new() - .description(Some( - "\"API running\": an authenticated `GET /api/v1/ping`. `up` is true also when the \ - peer answered 401, so a revoked token reads as `auth = rejected` rather than as an \ - outage.", - )) - .required("up") - .required("since") - .required("last_checked") - .required("last_error") - .required("latency_ms") - .required("auth") - .required("peer_version") - .property("up", ObjectBuilder::new().schema_type(Type::Boolean)) - .property( - "since", - nullable(Type::String) - .format(Some(date_time())) - .description(Some(SINCE_DOC)), - ) - .property( - "last_checked", - nullable(Type::String).format(Some(date_time())) - ) - .property( - "last_error", - nullable(Type::String).description(Some( - "Failure class such as `http_500` or `token_file`; never a URL or a secret.", - )), - ) - .property( - "latency_ms", - nullable(Type::Integer).minimum(Some(utoipa::Number::UInt(0))), - ) - .property("auth", Ref::from_schema_name("AuthState")) - .property( - "peer_version", - nullable(Type::String).description(Some( - "Version the peer reported, when it has the ping route." - )), - ) - .into() + check_properties( + ObjectBuilder::new() + .description(Some( + "\"API running\": an authenticated `GET /api/v1/ping`. `up` is true also when the \ + peer answered 401, so a revoked token reads as `auth = rejected` rather than as an \ + outage.", + )) + .required("up") + .required("since") + .required("last_checked") + .required("last_error") + .required("latency_ms") + .required("auth") + .required("peer_version"), + "Failure class such as `http_500` or `token_file`; never a URL or a secret.", + ) + .property("auth", Ref::from_schema_name("AuthState")) + .property( + "peer_version", + nullable(Type::String).description(Some( + "Version the peer reported, when it has the ping route." + )), + ) + .into() ); handwritten_schema!( @@ -656,6 +620,32 @@ fn date_time() -> SchemaFormat { SchemaFormat::Custom("date-time".into()) } +/// The properties both peer checks share: `up`, `since` and `last_checked` +/// (described by [`SINCE_DOC`]), `last_error` with its check-specific wording +/// and `latency_ms`. Added in document order so the keys stay where they were. +fn check_properties(builder: ObjectBuilder, last_error: &str) -> ObjectBuilder { + builder + .property("up", ObjectBuilder::new().schema_type(Type::Boolean)) + .property( + "since", + nullable(Type::String) + .format(Some(date_time())) + .description(Some(SINCE_DOC)), + ) + .property( + "last_checked", + nullable(Type::String).format(Some(date_time())), + ) + .property( + "last_error", + nullable(Type::String).description(Some(last_error)), + ) + .property( + "latency_ms", + nullable(Type::Integer).minimum(Some(utoipa::Number::UInt(0))), + ) +} + #[cfg(test)] mod tests { use super::*;