spikard 0.3.5 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/README.md +674 -659
  4. data/ext/spikard_rb/Cargo.toml +17 -17
  5. data/ext/spikard_rb/extconf.rb +10 -10
  6. data/ext/spikard_rb/src/lib.rs +6 -6
  7. data/lib/spikard/app.rb +405 -386
  8. data/lib/spikard/background.rb +27 -27
  9. data/lib/spikard/config.rb +396 -396
  10. data/lib/spikard/converters.rb +13 -13
  11. data/lib/spikard/handler_wrapper.rb +113 -113
  12. data/lib/spikard/provide.rb +214 -214
  13. data/lib/spikard/response.rb +173 -173
  14. data/lib/spikard/schema.rb +243 -243
  15. data/lib/spikard/sse.rb +111 -111
  16. data/lib/spikard/streaming_response.rb +44 -44
  17. data/lib/spikard/testing.rb +256 -221
  18. data/lib/spikard/upload_file.rb +131 -131
  19. data/lib/spikard/version.rb +5 -5
  20. data/lib/spikard/websocket.rb +59 -59
  21. data/lib/spikard.rb +43 -43
  22. data/sig/spikard.rbs +366 -360
  23. data/vendor/crates/spikard-bindings-shared/Cargo.toml +63 -0
  24. data/vendor/crates/spikard-bindings-shared/examples/config_extraction.rs +132 -0
  25. data/vendor/crates/spikard-bindings-shared/src/config_extractor.rs +752 -0
  26. data/vendor/crates/spikard-bindings-shared/src/conversion_traits.rs +194 -0
  27. data/vendor/crates/spikard-bindings-shared/src/di_traits.rs +246 -0
  28. data/vendor/crates/spikard-bindings-shared/src/error_response.rs +401 -0
  29. data/vendor/crates/spikard-bindings-shared/src/handler_base.rs +238 -0
  30. data/vendor/crates/spikard-bindings-shared/src/lib.rs +24 -0
  31. data/vendor/crates/spikard-bindings-shared/src/lifecycle_base.rs +292 -0
  32. data/vendor/crates/spikard-bindings-shared/src/lifecycle_executor.rs +616 -0
  33. data/vendor/crates/spikard-bindings-shared/src/response_builder.rs +305 -0
  34. data/vendor/crates/spikard-bindings-shared/src/test_client_base.rs +248 -0
  35. data/vendor/crates/spikard-bindings-shared/src/validation_helpers.rs +351 -0
  36. data/vendor/crates/spikard-bindings-shared/tests/comprehensive_coverage.rs +454 -0
  37. data/vendor/crates/spikard-bindings-shared/tests/error_response_edge_cases.rs +383 -0
  38. data/vendor/crates/spikard-bindings-shared/tests/handler_base_integration.rs +280 -0
  39. data/vendor/crates/spikard-core/Cargo.toml +40 -40
  40. data/vendor/crates/spikard-core/src/bindings/mod.rs +3 -3
  41. data/vendor/crates/spikard-core/src/bindings/response.rs +133 -133
  42. data/vendor/crates/spikard-core/src/debug.rs +127 -63
  43. data/vendor/crates/spikard-core/src/di/container.rs +702 -726
  44. data/vendor/crates/spikard-core/src/di/dependency.rs +273 -273
  45. data/vendor/crates/spikard-core/src/di/error.rs +118 -118
  46. data/vendor/crates/spikard-core/src/di/factory.rs +534 -538
  47. data/vendor/crates/spikard-core/src/di/graph.rs +506 -545
  48. data/vendor/crates/spikard-core/src/di/mod.rs +192 -192
  49. data/vendor/crates/spikard-core/src/di/resolved.rs +405 -411
  50. data/vendor/crates/spikard-core/src/di/value.rs +281 -283
  51. data/vendor/crates/spikard-core/src/errors.rs +69 -39
  52. data/vendor/crates/spikard-core/src/http.rs +415 -153
  53. data/vendor/crates/spikard-core/src/lib.rs +29 -29
  54. data/vendor/crates/spikard-core/src/lifecycle.rs +1186 -422
  55. data/vendor/crates/spikard-core/src/metadata.rs +389 -0
  56. data/vendor/crates/spikard-core/src/parameters.rs +2525 -722
  57. data/vendor/crates/spikard-core/src/problem.rs +344 -310
  58. data/vendor/crates/spikard-core/src/request_data.rs +1154 -189
  59. data/vendor/crates/spikard-core/src/router.rs +510 -249
  60. data/vendor/crates/spikard-core/src/schema_registry.rs +183 -183
  61. data/vendor/crates/spikard-core/src/type_hints.rs +304 -304
  62. data/vendor/crates/spikard-core/src/validation/error_mapper.rs +688 -0
  63. data/vendor/crates/spikard-core/src/{validation.rs → validation/mod.rs} +457 -699
  64. data/vendor/crates/spikard-http/Cargo.toml +64 -68
  65. data/vendor/crates/spikard-http/examples/sse-notifications.rs +148 -0
  66. data/vendor/crates/spikard-http/examples/websocket-chat.rs +92 -0
  67. data/vendor/crates/spikard-http/src/auth.rs +296 -247
  68. data/vendor/crates/spikard-http/src/background.rs +1860 -249
  69. data/vendor/crates/spikard-http/src/bindings/mod.rs +3 -3
  70. data/vendor/crates/spikard-http/src/bindings/response.rs +1 -1
  71. data/vendor/crates/spikard-http/src/body_metadata.rs +8 -8
  72. data/vendor/crates/spikard-http/src/cors.rs +1005 -490
  73. data/vendor/crates/spikard-http/src/debug.rs +128 -63
  74. data/vendor/crates/spikard-http/src/di_handler.rs +1668 -423
  75. data/vendor/crates/spikard-http/src/handler_response.rs +901 -190
  76. data/vendor/crates/spikard-http/src/handler_trait.rs +830 -228
  77. data/vendor/crates/spikard-http/src/handler_trait_tests.rs +290 -284
  78. data/vendor/crates/spikard-http/src/lib.rs +534 -529
  79. data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +230 -149
  80. data/vendor/crates/spikard-http/src/lifecycle.rs +1193 -428
  81. data/vendor/crates/spikard-http/src/middleware/mod.rs +540 -285
  82. data/vendor/crates/spikard-http/src/middleware/multipart.rs +912 -86
  83. data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +513 -147
  84. data/vendor/crates/spikard-http/src/middleware/validation.rs +735 -287
  85. data/vendor/crates/spikard-http/src/openapi/mod.rs +309 -309
  86. data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +535 -190
  87. data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +1363 -308
  88. data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +665 -195
  89. data/vendor/crates/spikard-http/src/query_parser.rs +793 -369
  90. data/vendor/crates/spikard-http/src/response.rs +720 -399
  91. data/vendor/crates/spikard-http/src/server/handler.rs +1650 -87
  92. data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +234 -98
  93. data/vendor/crates/spikard-http/src/server/mod.rs +1502 -805
  94. data/vendor/crates/spikard-http/src/server/request_extraction.rs +770 -119
  95. data/vendor/crates/spikard-http/src/server/routing_factory.rs +599 -0
  96. data/vendor/crates/spikard-http/src/sse.rs +1409 -447
  97. data/vendor/crates/spikard-http/src/testing/form.rs +52 -14
  98. data/vendor/crates/spikard-http/src/testing/multipart.rs +60 -60
  99. data/vendor/crates/spikard-http/src/testing/test_client.rs +283 -285
  100. data/vendor/crates/spikard-http/src/testing.rs +377 -377
  101. data/vendor/crates/spikard-http/src/websocket.rs +1375 -324
  102. data/vendor/crates/spikard-http/tests/background_behavior.rs +832 -0
  103. data/vendor/crates/spikard-http/tests/common/handlers.rs +309 -0
  104. data/vendor/crates/spikard-http/tests/common/mod.rs +26 -0
  105. data/vendor/crates/spikard-http/tests/di_integration.rs +192 -0
  106. data/vendor/crates/spikard-http/tests/doc_snippets.rs +5 -0
  107. data/vendor/crates/spikard-http/tests/lifecycle_execution.rs +1093 -0
  108. data/vendor/crates/spikard-http/tests/multipart_behavior.rs +656 -0
  109. data/vendor/crates/spikard-http/tests/server_config_builder.rs +314 -0
  110. data/vendor/crates/spikard-http/tests/sse_behavior.rs +620 -0
  111. data/vendor/crates/spikard-http/tests/websocket_behavior.rs +663 -0
  112. data/vendor/crates/spikard-rb/Cargo.toml +48 -42
  113. data/vendor/crates/spikard-rb/build.rs +199 -8
  114. data/vendor/crates/spikard-rb/src/background.rs +63 -63
  115. data/vendor/crates/spikard-rb/src/config/mod.rs +5 -0
  116. data/vendor/crates/spikard-rb/src/{config.rs → config/server_config.rs} +285 -294
  117. data/vendor/crates/spikard-rb/src/conversion.rs +554 -453
  118. data/vendor/crates/spikard-rb/src/di/builder.rs +100 -0
  119. data/vendor/crates/spikard-rb/src/{di.rs → di/mod.rs} +375 -409
  120. data/vendor/crates/spikard-rb/src/handler.rs +618 -625
  121. data/vendor/crates/spikard-rb/src/integration/mod.rs +3 -0
  122. data/vendor/crates/spikard-rb/src/lib.rs +1810 -2771
  123. data/vendor/crates/spikard-rb/src/lifecycle.rs +275 -274
  124. data/vendor/crates/spikard-rb/src/metadata/mod.rs +5 -0
  125. data/vendor/crates/spikard-rb/src/metadata/route_extraction.rs +447 -0
  126. data/vendor/crates/spikard-rb/src/runtime/mod.rs +5 -0
  127. data/vendor/crates/spikard-rb/src/runtime/server_runner.rs +324 -0
  128. data/vendor/crates/spikard-rb/src/server.rs +308 -283
  129. data/vendor/crates/spikard-rb/src/sse.rs +231 -231
  130. data/vendor/crates/spikard-rb/src/{test_client.rs → testing/client.rs} +551 -404
  131. data/vendor/crates/spikard-rb/src/testing/mod.rs +7 -0
  132. data/vendor/crates/spikard-rb/src/{test_sse.rs → testing/sse.rs} +143 -143
  133. data/vendor/crates/spikard-rb/src/testing/websocket.rs +635 -0
  134. data/vendor/crates/spikard-rb/src/websocket.rs +374 -233
  135. metadata +46 -13
  136. data/vendor/crates/spikard-http/src/parameters.rs +0 -1
  137. data/vendor/crates/spikard-http/src/problem.rs +0 -1
  138. data/vendor/crates/spikard-http/src/router.rs +0 -1
  139. data/vendor/crates/spikard-http/src/schema_registry.rs +0 -1
  140. data/vendor/crates/spikard-http/src/type_hints.rs +0 -1
  141. data/vendor/crates/spikard-http/src/validation.rs +0 -1
  142. data/vendor/crates/spikard-rb/src/test_websocket.rs +0 -221
@@ -1,304 +1,304 @@
1
- //! Path parameter type hint parsing
2
- //!
3
- //! Supports FastAPI-style type syntax in route paths:
4
- //! - `/items/{id:uuid}` → auto-generates UUID validation
5
- //! - `/users/{user_id:int}` → auto-generates integer type
6
- //! - `/files/{path:path}` → wildcard path capture
7
- //!
8
- //! Explicit parameter schemas override auto-generated ones.
9
-
10
- use regex::Regex;
11
- use serde_json::{Value, json};
12
- use std::collections::HashMap;
13
- use std::sync::OnceLock;
14
-
15
- /// Regex for matching type hints in path parameters
16
- static TYPE_HINT_REGEX: OnceLock<Regex> = OnceLock::new();
17
-
18
- /// Regex for matching wildcard path parameters
19
- static PATH_TYPE_REGEX: OnceLock<Regex> = OnceLock::new();
20
-
21
- /// Get the type hint regex (compiled once)
22
- fn type_hint_regex() -> &'static Regex {
23
- TYPE_HINT_REGEX.get_or_init(|| Regex::new(r"\{([^:}]+):([^}]+)\}").unwrap())
24
- }
25
-
26
- /// Get the path type regex (compiled once)
27
- fn path_type_regex() -> &'static Regex {
28
- PATH_TYPE_REGEX.get_or_init(|| Regex::new(r"\{([^:}]+):path\}").unwrap())
29
- }
30
-
31
- /// Parse type hints from a route path
32
- ///
33
- /// # Examples
34
- ///
35
- /// ```
36
- /// use spikard_core::type_hints::parse_type_hints;
37
- ///
38
- /// let hints = parse_type_hints("/items/{id:uuid}/tags/{tag_id:int}");
39
- /// assert_eq!(hints.get("id"), Some(&"uuid".to_string()));
40
- /// assert_eq!(hints.get("tag_id"), Some(&"int".to_string()));
41
- /// ```
42
- pub fn parse_type_hints(route_path: &str) -> HashMap<String, String> {
43
- let mut hints = HashMap::new();
44
- let re = type_hint_regex();
45
-
46
- for cap in re.captures_iter(route_path) {
47
- let param_name = cap.get(1).unwrap().as_str().to_string();
48
- let type_hint = cap.get(2).unwrap().as_str().to_string();
49
- hints.insert(param_name, type_hint);
50
- }
51
-
52
- hints
53
- }
54
-
55
- /// Strip type hints from path for Axum compatibility
56
- ///
57
- /// Converts FastAPI-style syntax to Axum syntax:
58
- /// - `/items/{id:uuid}` → `/items/{id}`
59
- /// - `/files/{path:path}` → `/files/{*path}` (wildcard for Axum v0.7)
60
- ///
61
- /// # Examples
62
- ///
63
- /// ```
64
- /// use spikard_core::type_hints::strip_type_hints;
65
- ///
66
- /// assert_eq!(strip_type_hints("/items/{id:uuid}"), "/items/{id}");
67
- /// assert_eq!(strip_type_hints("/files/{path:path}"), "/files/{*path}");
68
- /// ```
69
- pub fn strip_type_hints(route_path: &str) -> String {
70
- let path_re = path_type_regex();
71
- let route_path = path_re.replace_all(route_path, "{*$1}");
72
-
73
- let re = type_hint_regex();
74
- re.replace_all(&route_path, "{$1}").to_string()
75
- }
76
-
77
- /// Generate JSON Schema from a type hint
78
- ///
79
- /// # Supported Types
80
- ///
81
- /// - `uuid` → `{"type": "string", "format": "uuid"}`
82
- /// - `int` / `integer` → `{"type": "integer"}`
83
- /// - `str` / `string` → `{"type": "string"}`
84
- /// - `float` / `number` → `{"type": "number"}`
85
- /// - `bool` / `boolean` → `{"type": "boolean"}`
86
- /// - `date` → `{"type": "string", "format": "date"}`
87
- /// - `datetime` → `{"type": "string", "format": "date-time"}`
88
- /// - `path` → `{"type": "string"}` (wildcard capture)
89
- pub fn type_hint_to_schema(type_hint: &str) -> Value {
90
- match type_hint {
91
- "uuid" => json!({
92
- "type": "string",
93
- "format": "uuid"
94
- }),
95
- "int" | "integer" => json!({
96
- "type": "integer"
97
- }),
98
- "str" | "string" => json!({
99
- "type": "string"
100
- }),
101
- "float" | "number" => json!({
102
- "type": "number"
103
- }),
104
- "bool" | "boolean" => json!({
105
- "type": "boolean"
106
- }),
107
- "date" => json!({
108
- "type": "string",
109
- "format": "date"
110
- }),
111
- "datetime" | "date-time" => json!({
112
- "type": "string",
113
- "format": "date-time"
114
- }),
115
- "path" => json!({
116
- "type": "string"
117
- }),
118
- _ => json!({
119
- "type": "string"
120
- }),
121
- }
122
- }
123
-
124
- /// Auto-generate parameter schema from type hints in route path
125
- ///
126
- /// Creates a JSON Schema with path parameters based on type hints.
127
- /// Returns None if no type hints are found.
128
- ///
129
- /// # Examples
130
- ///
131
- /// ```
132
- /// use spikard_core::type_hints::auto_generate_parameter_schema;
133
- /// use serde_json::json;
134
- ///
135
- /// let schema = auto_generate_parameter_schema("/items/{id:uuid}");
136
- /// assert_eq!(schema, Some(json!({
137
- /// "type": "object",
138
- /// "properties": {
139
- /// "id": {
140
- /// "type": "string",
141
- /// "format": "uuid",
142
- /// "source": "path"
143
- /// }
144
- /// },
145
- /// "required": ["id"]
146
- /// })));
147
- /// ```
148
- pub fn auto_generate_parameter_schema(route_path: &str) -> Option<Value> {
149
- let type_hints = parse_type_hints(route_path);
150
-
151
- if type_hints.is_empty() {
152
- return None;
153
- }
154
-
155
- let mut properties = serde_json::Map::new();
156
- let mut required = Vec::new();
157
-
158
- for (param_name, type_hint) in type_hints {
159
- let mut param_schema = type_hint_to_schema(&type_hint);
160
-
161
- if let Some(obj) = param_schema.as_object_mut() {
162
- obj.insert("source".to_string(), json!("path"));
163
- }
164
-
165
- properties.insert(param_name.clone(), param_schema);
166
- required.push(json!(param_name));
167
- }
168
-
169
- Some(json!({
170
- "type": "object",
171
- "properties": properties,
172
- "required": required
173
- }))
174
- }
175
-
176
- /// Merge auto-generated schema with explicit schema
177
- ///
178
- /// Explicit schema takes precedence. Only auto-generates schemas for
179
- /// parameters not explicitly defined.
180
- ///
181
- /// # Examples
182
- ///
183
- /// ```
184
- /// use spikard_core::type_hints::merge_parameter_schemas;
185
- /// use serde_json::json;
186
- ///
187
- /// let auto_schema = json!({
188
- /// "type": "object",
189
- /// "properties": {
190
- /// "id": {"type": "string", "format": "uuid", "source": "path"},
191
- /// "count": {"type": "integer", "source": "path"}
192
- /// },
193
- /// "required": ["id", "count"]
194
- /// });
195
- ///
196
- /// let explicit_schema = json!({
197
- /// "type": "object",
198
- /// "properties": {
199
- /// "count": {"type": "integer", "minimum": 1, "maximum": 100, "source": "path"}
200
- /// },
201
- /// "required": ["count"]
202
- /// });
203
- ///
204
- /// let merged = merge_parameter_schemas(auto_schema, explicit_schema);
205
- /// // Result: auto-generated id + explicit count with constraints
206
- /// ```
207
- pub fn merge_parameter_schemas(auto_schema: Value, explicit_schema: Value) -> Value {
208
- let mut result = auto_schema.clone();
209
-
210
- let auto_props = result.get_mut("properties").and_then(|v| v.as_object_mut());
211
- let explicit_props = explicit_schema.get("properties").and_then(|v| v.as_object());
212
-
213
- if let (Some(auto_props), Some(explicit_props)) = (auto_props, explicit_props) {
214
- for (key, value) in explicit_props {
215
- auto_props.insert(key.clone(), value.clone());
216
- }
217
- }
218
-
219
- if let Some(explicit_required) = explicit_schema.get("required").and_then(|v| v.as_array())
220
- && let Some(auto_required) = result.get_mut("required").and_then(|v| v.as_array_mut())
221
- {
222
- for req in explicit_required {
223
- if !auto_required.contains(req) {
224
- auto_required.push(req.clone());
225
- }
226
- }
227
- }
228
-
229
- result
230
- }
231
-
232
- #[cfg(test)]
233
- mod tests {
234
- use super::*;
235
-
236
- #[test]
237
- fn test_parse_type_hints() {
238
- let hints = parse_type_hints("/items/{id:uuid}");
239
- assert_eq!(hints.get("id"), Some(&"uuid".to_string()));
240
-
241
- let hints = parse_type_hints("/users/{user_id:int}/posts/{post_id:int}");
242
- assert_eq!(hints.get("user_id"), Some(&"int".to_string()));
243
- assert_eq!(hints.get("post_id"), Some(&"int".to_string()));
244
- }
245
-
246
- #[test]
247
- fn test_strip_type_hints() {
248
- assert_eq!(strip_type_hints("/items/{id:uuid}"), "/items/{id}");
249
- assert_eq!(strip_type_hints("/files/{path:path}"), "/files/{*path}");
250
- assert_eq!(
251
- strip_type_hints("/users/{user_id:int}/posts/{post_id:int}"),
252
- "/users/{user_id}/posts/{post_id}"
253
- );
254
- }
255
-
256
- #[test]
257
- fn test_type_hint_to_schema() {
258
- let schema = type_hint_to_schema("uuid");
259
- assert_eq!(schema["type"], "string");
260
- assert_eq!(schema["format"], "uuid");
261
-
262
- let schema = type_hint_to_schema("int");
263
- assert_eq!(schema["type"], "integer");
264
- }
265
-
266
- #[test]
267
- fn test_auto_generate_parameter_schema() {
268
- let schema = auto_generate_parameter_schema("/items/{id:uuid}").unwrap();
269
- assert_eq!(schema["type"], "object");
270
- assert_eq!(schema["properties"]["id"]["type"], "string");
271
- assert_eq!(schema["properties"]["id"]["format"], "uuid");
272
- assert_eq!(schema["required"], json!(["id"]));
273
- }
274
-
275
- #[test]
276
- fn test_no_type_hints() {
277
- let schema = auto_generate_parameter_schema("/items/{id}");
278
- assert!(schema.is_none());
279
- }
280
-
281
- #[test]
282
- fn test_merge_schemas() {
283
- let auto_schema = json!({
284
- "type": "object",
285
- "properties": {
286
- "id": {"type": "string", "format": "uuid", "source": "path"}
287
- },
288
- "required": ["id"]
289
- });
290
-
291
- let explicit_schema = json!({
292
- "type": "object",
293
- "properties": {
294
- "count": {"type": "integer", "minimum": 1, "source": "path"}
295
- },
296
- "required": ["count"]
297
- });
298
-
299
- let merged = merge_parameter_schemas(auto_schema, explicit_schema);
300
- assert!(merged["properties"]["id"].is_object());
301
- assert!(merged["properties"]["count"].is_object());
302
- assert_eq!(merged["properties"]["count"]["minimum"], 1);
303
- }
304
- }
1
+ //! Path parameter type hint parsing
2
+ //!
3
+ //! Supports FastAPI-style type syntax in route paths:
4
+ //! - `/items/{id:uuid}` → auto-generates UUID validation
5
+ //! - `/users/{user_id:int}` → auto-generates integer type
6
+ //! - `/files/{path:path}` → wildcard path capture
7
+ //!
8
+ //! Explicit parameter schemas override auto-generated ones.
9
+
10
+ use regex::Regex;
11
+ use serde_json::{Value, json};
12
+ use std::collections::HashMap;
13
+ use std::sync::OnceLock;
14
+
15
+ /// Regex for matching type hints in path parameters
16
+ static TYPE_HINT_REGEX: OnceLock<Regex> = OnceLock::new();
17
+
18
+ /// Regex for matching wildcard path parameters
19
+ static PATH_TYPE_REGEX: OnceLock<Regex> = OnceLock::new();
20
+
21
+ /// Get the type hint regex (compiled once)
22
+ fn type_hint_regex() -> &'static Regex {
23
+ TYPE_HINT_REGEX.get_or_init(|| Regex::new(r"\{([^:}]+):([^}]+)\}").unwrap())
24
+ }
25
+
26
+ /// Get the path type regex (compiled once)
27
+ fn path_type_regex() -> &'static Regex {
28
+ PATH_TYPE_REGEX.get_or_init(|| Regex::new(r"\{([^:}]+):path\}").unwrap())
29
+ }
30
+
31
+ /// Parse type hints from a route path
32
+ ///
33
+ /// # Examples
34
+ ///
35
+ /// ```
36
+ /// use spikard_core::type_hints::parse_type_hints;
37
+ ///
38
+ /// let hints = parse_type_hints("/items/{id:uuid}/tags/{tag_id:int}");
39
+ /// assert_eq!(hints.get("id"), Some(&"uuid".to_string()));
40
+ /// assert_eq!(hints.get("tag_id"), Some(&"int".to_string()));
41
+ /// ```
42
+ pub fn parse_type_hints(route_path: &str) -> HashMap<String, String> {
43
+ let mut hints = HashMap::new();
44
+ let re = type_hint_regex();
45
+
46
+ for cap in re.captures_iter(route_path) {
47
+ let param_name = cap.get(1).unwrap().as_str().to_string();
48
+ let type_hint = cap.get(2).unwrap().as_str().to_string();
49
+ hints.insert(param_name, type_hint);
50
+ }
51
+
52
+ hints
53
+ }
54
+
55
+ /// Strip type hints from path for Axum compatibility
56
+ ///
57
+ /// Converts FastAPI-style syntax to Axum syntax:
58
+ /// - `/items/{id:uuid}` → `/items/{id}`
59
+ /// - `/files/{path:path}` → `/files/{*path}` (wildcard for Axum v0.7)
60
+ ///
61
+ /// # Examples
62
+ ///
63
+ /// ```
64
+ /// use spikard_core::type_hints::strip_type_hints;
65
+ ///
66
+ /// assert_eq!(strip_type_hints("/items/{id:uuid}"), "/items/{id}");
67
+ /// assert_eq!(strip_type_hints("/files/{path:path}"), "/files/{*path}");
68
+ /// ```
69
+ pub fn strip_type_hints(route_path: &str) -> String {
70
+ let path_re = path_type_regex();
71
+ let route_path = path_re.replace_all(route_path, "{*$1}");
72
+
73
+ let re = type_hint_regex();
74
+ re.replace_all(&route_path, "{$1}").to_string()
75
+ }
76
+
77
+ /// Generate JSON Schema from a type hint
78
+ ///
79
+ /// # Supported Types
80
+ ///
81
+ /// - `uuid` → `{"type": "string", "format": "uuid"}`
82
+ /// - `int` / `integer` → `{"type": "integer"}`
83
+ /// - `str` / `string` → `{"type": "string"}`
84
+ /// - `float` / `number` → `{"type": "number"}`
85
+ /// - `bool` / `boolean` → `{"type": "boolean"}`
86
+ /// - `date` → `{"type": "string", "format": "date"}`
87
+ /// - `datetime` → `{"type": "string", "format": "date-time"}`
88
+ /// - `path` → `{"type": "string"}` (wildcard capture)
89
+ pub fn type_hint_to_schema(type_hint: &str) -> Value {
90
+ match type_hint {
91
+ "uuid" => json!({
92
+ "type": "string",
93
+ "format": "uuid"
94
+ }),
95
+ "int" | "integer" => json!({
96
+ "type": "integer"
97
+ }),
98
+ "str" | "string" => json!({
99
+ "type": "string"
100
+ }),
101
+ "float" | "number" => json!({
102
+ "type": "number"
103
+ }),
104
+ "bool" | "boolean" => json!({
105
+ "type": "boolean"
106
+ }),
107
+ "date" => json!({
108
+ "type": "string",
109
+ "format": "date"
110
+ }),
111
+ "datetime" | "date-time" => json!({
112
+ "type": "string",
113
+ "format": "date-time"
114
+ }),
115
+ "path" => json!({
116
+ "type": "string"
117
+ }),
118
+ _ => json!({
119
+ "type": "string"
120
+ }),
121
+ }
122
+ }
123
+
124
+ /// Auto-generate parameter schema from type hints in route path
125
+ ///
126
+ /// Creates a JSON Schema with path parameters based on type hints.
127
+ /// Returns None if no type hints are found.
128
+ ///
129
+ /// # Examples
130
+ ///
131
+ /// ```
132
+ /// use spikard_core::type_hints::auto_generate_parameter_schema;
133
+ /// use serde_json::json;
134
+ ///
135
+ /// let schema = auto_generate_parameter_schema("/items/{id:uuid}");
136
+ /// assert_eq!(schema, Some(json!({
137
+ /// "type": "object",
138
+ /// "properties": {
139
+ /// "id": {
140
+ /// "type": "string",
141
+ /// "format": "uuid",
142
+ /// "source": "path"
143
+ /// }
144
+ /// },
145
+ /// "required": ["id"]
146
+ /// })));
147
+ /// ```
148
+ pub fn auto_generate_parameter_schema(route_path: &str) -> Option<Value> {
149
+ let type_hints = parse_type_hints(route_path);
150
+
151
+ if type_hints.is_empty() {
152
+ return None;
153
+ }
154
+
155
+ let mut properties = serde_json::Map::new();
156
+ let mut required = Vec::new();
157
+
158
+ for (param_name, type_hint) in type_hints {
159
+ let mut param_schema = type_hint_to_schema(&type_hint);
160
+
161
+ if let Some(obj) = param_schema.as_object_mut() {
162
+ obj.insert("source".to_string(), json!("path"));
163
+ }
164
+
165
+ properties.insert(param_name.clone(), param_schema);
166
+ required.push(json!(param_name));
167
+ }
168
+
169
+ Some(json!({
170
+ "type": "object",
171
+ "properties": properties,
172
+ "required": required
173
+ }))
174
+ }
175
+
176
+ /// Merge auto-generated schema with explicit schema
177
+ ///
178
+ /// Explicit schema takes precedence. Only auto-generates schemas for
179
+ /// parameters not explicitly defined.
180
+ ///
181
+ /// # Examples
182
+ ///
183
+ /// ```
184
+ /// use spikard_core::type_hints::merge_parameter_schemas;
185
+ /// use serde_json::json;
186
+ ///
187
+ /// let auto_schema = json!({
188
+ /// "type": "object",
189
+ /// "properties": {
190
+ /// "id": {"type": "string", "format": "uuid", "source": "path"},
191
+ /// "count": {"type": "integer", "source": "path"}
192
+ /// },
193
+ /// "required": ["id", "count"]
194
+ /// });
195
+ ///
196
+ /// let explicit_schema = json!({
197
+ /// "type": "object",
198
+ /// "properties": {
199
+ /// "count": {"type": "integer", "minimum": 1, "maximum": 100, "source": "path"}
200
+ /// },
201
+ /// "required": ["count"]
202
+ /// });
203
+ ///
204
+ /// let merged = merge_parameter_schemas(auto_schema, explicit_schema);
205
+ /// // Result: auto-generated id + explicit count with constraints
206
+ /// ```
207
+ pub fn merge_parameter_schemas(auto_schema: Value, explicit_schema: Value) -> Value {
208
+ let mut result = auto_schema.clone();
209
+
210
+ let auto_props = result.get_mut("properties").and_then(|v| v.as_object_mut());
211
+ let explicit_props = explicit_schema.get("properties").and_then(|v| v.as_object());
212
+
213
+ if let (Some(auto_props), Some(explicit_props)) = (auto_props, explicit_props) {
214
+ for (key, value) in explicit_props {
215
+ auto_props.insert(key.clone(), value.clone());
216
+ }
217
+ }
218
+
219
+ if let Some(explicit_required) = explicit_schema.get("required").and_then(|v| v.as_array())
220
+ && let Some(auto_required) = result.get_mut("required").and_then(|v| v.as_array_mut())
221
+ {
222
+ for req in explicit_required {
223
+ if !auto_required.contains(req) {
224
+ auto_required.push(req.clone());
225
+ }
226
+ }
227
+ }
228
+
229
+ result
230
+ }
231
+
232
+ #[cfg(test)]
233
+ mod tests {
234
+ use super::*;
235
+
236
+ #[test]
237
+ fn test_parse_type_hints() {
238
+ let hints = parse_type_hints("/items/{id:uuid}");
239
+ assert_eq!(hints.get("id"), Some(&"uuid".to_string()));
240
+
241
+ let hints = parse_type_hints("/users/{user_id:int}/posts/{post_id:int}");
242
+ assert_eq!(hints.get("user_id"), Some(&"int".to_string()));
243
+ assert_eq!(hints.get("post_id"), Some(&"int".to_string()));
244
+ }
245
+
246
+ #[test]
247
+ fn test_strip_type_hints() {
248
+ assert_eq!(strip_type_hints("/items/{id:uuid}"), "/items/{id}");
249
+ assert_eq!(strip_type_hints("/files/{path:path}"), "/files/{*path}");
250
+ assert_eq!(
251
+ strip_type_hints("/users/{user_id:int}/posts/{post_id:int}"),
252
+ "/users/{user_id}/posts/{post_id}"
253
+ );
254
+ }
255
+
256
+ #[test]
257
+ fn test_type_hint_to_schema() {
258
+ let schema = type_hint_to_schema("uuid");
259
+ assert_eq!(schema["type"], "string");
260
+ assert_eq!(schema["format"], "uuid");
261
+
262
+ let schema = type_hint_to_schema("int");
263
+ assert_eq!(schema["type"], "integer");
264
+ }
265
+
266
+ #[test]
267
+ fn test_auto_generate_parameter_schema() {
268
+ let schema = auto_generate_parameter_schema("/items/{id:uuid}").unwrap();
269
+ assert_eq!(schema["type"], "object");
270
+ assert_eq!(schema["properties"]["id"]["type"], "string");
271
+ assert_eq!(schema["properties"]["id"]["format"], "uuid");
272
+ assert_eq!(schema["required"], json!(["id"]));
273
+ }
274
+
275
+ #[test]
276
+ fn test_no_type_hints() {
277
+ let schema = auto_generate_parameter_schema("/items/{id}");
278
+ assert!(schema.is_none());
279
+ }
280
+
281
+ #[test]
282
+ fn test_merge_schemas() {
283
+ let auto_schema = json!({
284
+ "type": "object",
285
+ "properties": {
286
+ "id": {"type": "string", "format": "uuid", "source": "path"}
287
+ },
288
+ "required": ["id"]
289
+ });
290
+
291
+ let explicit_schema = json!({
292
+ "type": "object",
293
+ "properties": {
294
+ "count": {"type": "integer", "minimum": 1, "source": "path"}
295
+ },
296
+ "required": ["count"]
297
+ });
298
+
299
+ let merged = merge_parameter_schemas(auto_schema, explicit_schema);
300
+ assert!(merged["properties"]["id"].is_object());
301
+ assert!(merged["properties"]["count"].is_object());
302
+ assert_eq!(merged["properties"]["count"]["minimum"], 1);
303
+ }
304
+ }