base/interface/tool/
schema.rs1use super::*;
4
5pub fn compact_schema(mut schema: Value) -> Value {
13 if let Some(root) = schema.as_object_mut() {
14 root.remove("$schema");
15 root.remove("title");
16 }
17 compact_node(&mut schema);
18 schema
19}
20
21fn compact_node(node: &mut Value) {
22 match node {
23 Value::Object(map) => {
24 if map.get("default").is_some_and(Value::is_null) {
25 map.remove("default");
26 }
27 if map
28 .get("format")
29 .and_then(Value::as_str)
30 .is_some_and(is_numeric_format)
31 {
32 map.remove("format");
33 }
34 if let Some(constants) = map.get("oneOf").and_then(string_constants) {
35 map.remove("oneOf");
36 map.insert("type".into(), Value::String("string".into()));
37 map.insert("enum".into(), Value::Array(constants));
38 }
39 for child in map.values_mut() {
40 compact_node(child);
41 }
42 }
43 Value::Array(items) => items.iter_mut().for_each(compact_node),
44 _ => {}
45 }
46}
47
48fn is_numeric_format(format: &str) -> bool {
49 matches!(
50 format,
51 "uint"
52 | "uint8"
53 | "uint16"
54 | "uint32"
55 | "uint64"
56 | "int8"
57 | "int16"
58 | "int32"
59 | "int64"
60 | "float"
61 | "double"
62 )
63}
64
65fn string_constants(one_of: &Value) -> Option<Vec<Value>> {
68 let branches = one_of.as_array()?;
69 if branches.is_empty() {
70 return None;
71 }
72 branches
73 .iter()
74 .map(|branch| {
75 let branch = branch.as_object()?;
76 let constant = branch.get("const").filter(|c| c.is_string())?;
77 let only_known_keys = branch.keys().all(|k| {
78 k == "const" || k == "description" || (k == "type" && branch[k] == "string")
79 });
80 only_known_keys.then(|| constant.clone())
81 })
82 .collect()
83}
84
85#[cfg(test)]
86mod compact_schema_tests {
87 use super::*;
88 use serde_json::json;
89
90 #[test]
91 fn drops_generator_noise_and_keeps_every_parameter() {
92 let schema = json!({
93 "$schema": "https://json-schema.org/draft/2020-12/schema",
94 "title": "BashInput",
95 "type": "object",
96 "required": ["command"],
97 "properties": {
98 "command": {"type": "string", "description": "The command."},
99 "timeout_ms": {"type": ["integer", "null"], "default": null,
100 "format": "uint64", "minimum": 0},
101 "url": {"type": "string", "format": "uri"},
102 "flag": {"type": "boolean", "default": false}
103 }
104 });
105 assert_eq!(
106 compact_schema(schema),
107 json!({
108 "type": "object",
109 "required": ["command"],
110 "properties": {
111 "command": {"type": "string", "description": "The command."},
112 "timeout_ms": {"type": ["integer", "null"], "minimum": 0},
113 "url": {"type": "string", "format": "uri"},
114 "flag": {"type": "boolean", "default": false}
115 }
116 })
117 );
118 }
119
120 #[test]
121 fn an_enum_of_described_constants_becomes_a_plain_enum() {
122 let schema = json!({"$defs": {"Mode": {
123 "description": "Permission mode.",
124 "oneOf": [
125 {"const": "default", "type": "string", "description": "Ask."},
126 {"const": "plan", "type": "string", "description": "Read-only."}
127 ]
128 }}});
129 assert_eq!(
130 compact_schema(schema),
131 json!({"$defs": {"Mode": {
132 "description": "Permission mode.",
133 "type": "string",
134 "enum": ["default", "plan"]
135 }}})
136 );
137 }
138
139 #[test]
140 fn a_oneof_that_is_not_only_constants_is_left_alone() {
141 let one_of = json!([
142 {"const": "auto", "type": "string"},
143 {"type": "object", "properties": {"n": {"type": "integer"}}}
144 ]);
145 let compacted = compact_schema(json!({"oneOf": one_of.clone()}));
146 assert_eq!(compacted["oneOf"], one_of);
147 }
148}