G: G3 Serving and CLI serve the spec behind auth and print it offline

This commit is contained in:
ari committed 2026-10-20 15:54:13 -04:00
1 parent 7078ed15b1
commit 869b6818e9
7 files changed
+192 -12

No files matched your search

+25 -5
View File
@@ -15,6 +15,7 @@
//! produces, so a field cannot go missing quietly.
use std::borrow::Cow;
use std::sync::{Arc, OnceLock};
use schemars::generate::SchemaSettings;
use schemars::{JsonSchema, SchemaGenerator};
@@ -60,6 +61,7 @@ pub const BEARER_SCHEME: &str = "bearer_auth";
crate::api::peers::list,
crate::api::peers::one,
crate::api::peers::pass,
crate::api::openapi_json,
mcp_endpoint,
),
components(schemas(
@@ -81,11 +83,26 @@ pub const BEARER_SCHEME: &str = "bearer_auth";
)]
struct ApiDoc;
/// The description of one instance.
/// The description of one instance, built once per peer mode.
///
/// `peers` is `state.peers.is_some()`, the flag that also decides whether the
/// router merges the peer routes and whether MCP advertises the peer tools.
pub fn document(peers: bool) -> OpenApi {
/// Building it walks every DTO and the whole tool router, and the result
/// cannot change while the process runs, so both modes are cached.
pub fn document(peers: bool) -> Arc<OpenApi> {
static OFF: OnceLock<Arc<OpenApi>> = OnceLock::new();
static ON: OnceLock<Arc<OpenApi>> = OnceLock::new();
let cache = if peers { &ON } else { &OFF };
cache.get_or_init(|| Arc::new(build(peers))).clone()
}
/// The document as JSON text, the form the committed `docs/openapi.json` and
/// `slinuxm openapi` use.
pub fn to_pretty_json(peers: bool) -> serde_json::Result<String> {
serde_json::to_string_pretty(&*document(peers))
}
fn build(peers: bool) -> OpenApi {
let mut doc = ApiDoc::openapi();
doc.info = info();
doc.servers = Some(vec![ServerBuilder::new()
@@ -905,10 +922,13 @@ mod tests {
}
#[test]
fn document_is_deterministic() {
fn generation_is_deterministic() {
// The committed `docs/openapi.json` is compared with this output, so
// two builds must produce the same bytes.
assert_eq!(
serde_json::to_value(document(true)).unwrap(),
serde_json::to_value(document(true)).unwrap()
serde_json::to_value(build(true)).unwrap(),
serde_json::to_value(build(true)).unwrap()
);
assert_eq!(to_pretty_json(true).unwrap(), to_pretty_json(true).unwrap());
}
}