Skip to main content

orpc_procedure/
schema.rs

1/// Type-erased schema interface for procedure input/output.
2///
3/// This is the type-erased counterpart of the typed `Schema` trait in the `orpc` crate.
4/// Allows heterogeneous storage in `ErasedProcedure` while preserving schema information
5/// for OpenAPI generation and TypeScript type export.
6pub trait ErasedSchema: Send + Sync + 'static {
7    /// Generate a JSON Schema representation.
8    fn json_schema(&self) -> serde_json::Value;
9
10    /// Downcast support for extension crates (e.g., orpc-specta).
11    fn as_any(&self) -> &dyn std::any::Any;
12}
13
14/// No-op schema placeholder for procedures without schema validation.
15pub struct NoSchema;
16
17impl ErasedSchema for NoSchema {
18    fn json_schema(&self) -> serde_json::Value {
19        serde_json::Value::Null
20    }
21
22    fn as_any(&self) -> &dyn std::any::Any {
23        self
24    }
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30
31    #[test]
32    fn no_schema_returns_null() {
33        let schema = NoSchema;
34        assert_eq!(schema.json_schema(), serde_json::Value::Null);
35    }
36
37    #[test]
38    fn erased_schema_is_object_safe() {
39        let _boxed: Box<dyn ErasedSchema> = Box::new(NoSchema);
40    }
41
42    #[test]
43    fn erased_schema_send_sync() {
44        fn assert_send_sync<T: Send + Sync>() {}
45        assert_send_sync::<Box<dyn ErasedSchema>>();
46    }
47}