Skip to main content

rustapi_core/
auto_schema.rs

1//! Auto-schema registration using linkme distributed slices
2//!
3//! **Implementation detail**: Companion to `auto_route`.
4//! Provides link-time registration of types that should appear in the generated
5//! OpenAPI spec (via `#[rustapi_rs::schema]` and implicit schema derivation).
6//!
7//! This is an internal detail. The only stable surface is that schemas referenced
8//! by auto-registered routes (and explicitly annotated types) end up in the
9//! OpenAPI document when using `RustApi::auto()`.
10//!
11//! This module enables zero-config OpenAPI schema registration.
12//! Route macros can register schemas at link-time, and `RustApi::auto()`
13//! will collect and apply them before serving docs.
14
15use linkme::distributed_slice;
16
17/// Distributed slice containing all auto-registered schema registration functions.
18///
19/// Each element is a function that takes a mutable reference to the current
20/// [`rustapi_openapi::OpenApiSpec`] and registers one or more schemas.
21#[distributed_slice]
22pub static AUTO_SCHEMAS: [fn(&mut rustapi_openapi::OpenApiSpec)];
23
24/// Apply all auto-registered schemas into the given OpenAPI spec.
25pub fn apply_auto_schemas(spec: &mut rustapi_openapi::OpenApiSpec) {
26    for f in AUTO_SCHEMAS.iter() {
27        f(spec);
28    }
29}
30
31/// Get the count of auto-registered schema registration functions.
32#[allow(dead_code)]
33pub fn auto_schema_count() -> usize {
34    AUTO_SCHEMAS.len()
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn test_auto_schemas_slice_exists() {
43        let _count = auto_schema_count();
44    }
45
46    #[test]
47    fn test_apply_auto_schemas_does_not_panic() {
48        let mut spec = rustapi_openapi::OpenApiSpec::new("Test", "0.0.0");
49        apply_auto_schemas(&mut spec);
50    }
51}