use std::sync::OnceLock;
const SPEC_YAML: &str = include_str!("../../.well-known/openapi.yaml");
pub fn openapi_spec() -> serde_json::Value {
static SPEC: OnceLock<serde_json::Value> = OnceLock::new();
SPEC.get_or_init(|| {
serde_yaml_ng::from_str(SPEC_YAML)
.expect("embedded .well-known/openapi.yaml must be valid YAML")
})
.clone()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_served_spec_is_the_canonical_spec() {
let spec = openapi_spec();
let paths = spec
.get("paths")
.and_then(|p| p.as_object())
.expect("spec has paths");
assert!(
paths.len() >= 50,
"served spec declares only {} paths; it should be the whole canonical spec",
paths.len()
);
assert_eq!(
spec.get("info")
.and_then(|i| i.get("version"))
.and_then(|v| v.as_str()),
Some(env!("CARGO_PKG_VERSION")),
"served spec must declare the crate version"
);
}
#[test]
fn the_spec_parses_and_is_cached() {
assert_eq!(openapi_spec(), openapi_spec());
}
}