spikard 0.3.2 → 0.3.3

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 (180) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/README.md +659 -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 +386 -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 +221 -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 +360 -360
  23. data/vendor/crates/spikard-core/Cargo.toml +40 -40
  24. data/vendor/crates/spikard-core/src/bindings/mod.rs +3 -3
  25. data/vendor/crates/spikard-core/src/bindings/response.rs +133 -133
  26. data/vendor/crates/spikard-core/src/debug.rs +63 -63
  27. data/vendor/crates/spikard-core/src/di/container.rs +726 -726
  28. data/vendor/crates/spikard-core/src/di/dependency.rs +273 -273
  29. data/vendor/crates/spikard-core/src/di/error.rs +118 -118
  30. data/vendor/crates/spikard-core/src/di/factory.rs +538 -538
  31. data/vendor/crates/spikard-core/src/di/graph.rs +545 -545
  32. data/vendor/crates/spikard-core/src/di/mod.rs +192 -192
  33. data/vendor/crates/spikard-core/src/di/resolved.rs +411 -411
  34. data/vendor/crates/spikard-core/src/di/value.rs +283 -283
  35. data/vendor/crates/spikard-core/src/errors.rs +39 -39
  36. data/vendor/crates/spikard-core/src/http.rs +153 -153
  37. data/vendor/crates/spikard-core/src/lib.rs +29 -29
  38. data/vendor/crates/spikard-core/src/lifecycle.rs +422 -422
  39. data/vendor/crates/spikard-core/src/parameters.rs +722 -722
  40. data/vendor/crates/spikard-core/src/problem.rs +310 -310
  41. data/vendor/crates/spikard-core/src/request_data.rs +189 -189
  42. data/vendor/crates/spikard-core/src/router.rs +249 -249
  43. data/vendor/crates/spikard-core/src/schema_registry.rs +183 -183
  44. data/vendor/crates/spikard-core/src/type_hints.rs +304 -304
  45. data/vendor/crates/spikard-core/src/validation.rs +699 -699
  46. data/vendor/crates/spikard-http/Cargo.toml +58 -58
  47. data/vendor/crates/spikard-http/src/auth.rs +247 -247
  48. data/vendor/crates/spikard-http/src/background.rs +249 -249
  49. data/vendor/crates/spikard-http/src/bindings/mod.rs +3 -3
  50. data/vendor/crates/spikard-http/src/bindings/response.rs +1 -1
  51. data/vendor/crates/spikard-http/src/body_metadata.rs +8 -8
  52. data/vendor/crates/spikard-http/src/cors.rs +490 -490
  53. data/vendor/crates/spikard-http/src/debug.rs +63 -63
  54. data/vendor/crates/spikard-http/src/di_handler.rs +423 -423
  55. data/vendor/crates/spikard-http/src/handler_response.rs +190 -190
  56. data/vendor/crates/spikard-http/src/handler_trait.rs +228 -228
  57. data/vendor/crates/spikard-http/src/handler_trait_tests.rs +284 -284
  58. data/vendor/crates/spikard-http/src/lib.rs +529 -529
  59. data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +149 -149
  60. data/vendor/crates/spikard-http/src/lifecycle.rs +428 -428
  61. data/vendor/crates/spikard-http/src/middleware/mod.rs +285 -285
  62. data/vendor/crates/spikard-http/src/middleware/multipart.rs +86 -86
  63. data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +147 -147
  64. data/vendor/crates/spikard-http/src/middleware/validation.rs +287 -287
  65. data/vendor/crates/spikard-http/src/openapi/mod.rs +309 -309
  66. data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +190 -190
  67. data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +308 -308
  68. data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +195 -195
  69. data/vendor/crates/spikard-http/src/parameters.rs +1 -1
  70. data/vendor/crates/spikard-http/src/problem.rs +1 -1
  71. data/vendor/crates/spikard-http/src/query_parser.rs +369 -369
  72. data/vendor/crates/spikard-http/src/response.rs +399 -399
  73. data/vendor/crates/spikard-http/src/router.rs +1 -1
  74. data/vendor/crates/spikard-http/src/schema_registry.rs +1 -1
  75. data/vendor/crates/spikard-http/src/server/handler.rs +87 -87
  76. data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +98 -98
  77. data/vendor/crates/spikard-http/src/server/mod.rs +805 -805
  78. data/vendor/crates/spikard-http/src/server/request_extraction.rs +119 -119
  79. data/vendor/crates/spikard-http/src/sse.rs +447 -447
  80. data/vendor/crates/spikard-http/src/testing/form.rs +14 -14
  81. data/vendor/crates/spikard-http/src/testing/multipart.rs +60 -60
  82. data/vendor/crates/spikard-http/src/testing/test_client.rs +285 -285
  83. data/vendor/crates/spikard-http/src/testing.rs +377 -377
  84. data/vendor/crates/spikard-http/src/type_hints.rs +1 -1
  85. data/vendor/crates/spikard-http/src/validation.rs +1 -1
  86. data/vendor/crates/spikard-http/src/websocket.rs +324 -324
  87. data/vendor/crates/spikard-rb/Cargo.toml +42 -42
  88. data/vendor/crates/spikard-rb/build.rs +8 -8
  89. data/vendor/crates/spikard-rb/src/background.rs +63 -63
  90. data/vendor/crates/spikard-rb/src/config.rs +294 -294
  91. data/vendor/crates/spikard-rb/src/conversion.rs +453 -453
  92. data/vendor/crates/spikard-rb/src/di.rs +409 -409
  93. data/vendor/crates/spikard-rb/src/handler.rs +625 -625
  94. data/vendor/crates/spikard-rb/src/lib.rs +2771 -2771
  95. data/vendor/crates/spikard-rb/src/lifecycle.rs +274 -274
  96. data/vendor/crates/spikard-rb/src/server.rs +283 -283
  97. data/vendor/crates/spikard-rb/src/sse.rs +231 -231
  98. data/vendor/crates/spikard-rb/src/test_client.rs +404 -404
  99. data/vendor/crates/spikard-rb/src/test_sse.rs +143 -143
  100. data/vendor/crates/spikard-rb/src/test_websocket.rs +221 -221
  101. data/vendor/crates/spikard-rb/src/websocket.rs +233 -233
  102. data/vendor/spikard-core/Cargo.toml +40 -40
  103. data/vendor/spikard-core/src/bindings/mod.rs +3 -3
  104. data/vendor/spikard-core/src/bindings/response.rs +133 -133
  105. data/vendor/spikard-core/src/debug.rs +63 -63
  106. data/vendor/spikard-core/src/di/container.rs +726 -726
  107. data/vendor/spikard-core/src/di/dependency.rs +273 -273
  108. data/vendor/spikard-core/src/di/error.rs +118 -118
  109. data/vendor/spikard-core/src/di/factory.rs +538 -538
  110. data/vendor/spikard-core/src/di/graph.rs +545 -545
  111. data/vendor/spikard-core/src/di/mod.rs +192 -192
  112. data/vendor/spikard-core/src/di/resolved.rs +411 -411
  113. data/vendor/spikard-core/src/di/value.rs +283 -283
  114. data/vendor/spikard-core/src/http.rs +153 -153
  115. data/vendor/spikard-core/src/lib.rs +28 -28
  116. data/vendor/spikard-core/src/lifecycle.rs +422 -422
  117. data/vendor/spikard-core/src/parameters.rs +719 -719
  118. data/vendor/spikard-core/src/problem.rs +310 -310
  119. data/vendor/spikard-core/src/request_data.rs +189 -189
  120. data/vendor/spikard-core/src/router.rs +249 -249
  121. data/vendor/spikard-core/src/schema_registry.rs +183 -183
  122. data/vendor/spikard-core/src/type_hints.rs +304 -304
  123. data/vendor/spikard-core/src/validation.rs +699 -699
  124. data/vendor/spikard-http/Cargo.toml +58 -58
  125. data/vendor/spikard-http/src/auth.rs +247 -247
  126. data/vendor/spikard-http/src/background.rs +249 -249
  127. data/vendor/spikard-http/src/bindings/mod.rs +3 -3
  128. data/vendor/spikard-http/src/bindings/response.rs +1 -1
  129. data/vendor/spikard-http/src/body_metadata.rs +8 -8
  130. data/vendor/spikard-http/src/cors.rs +490 -490
  131. data/vendor/spikard-http/src/debug.rs +63 -63
  132. data/vendor/spikard-http/src/di_handler.rs +423 -423
  133. data/vendor/spikard-http/src/handler_response.rs +190 -190
  134. data/vendor/spikard-http/src/handler_trait.rs +228 -228
  135. data/vendor/spikard-http/src/handler_trait_tests.rs +284 -284
  136. data/vendor/spikard-http/src/lib.rs +529 -529
  137. data/vendor/spikard-http/src/lifecycle/adapter.rs +149 -149
  138. data/vendor/spikard-http/src/lifecycle.rs +428 -428
  139. data/vendor/spikard-http/src/middleware/mod.rs +285 -285
  140. data/vendor/spikard-http/src/middleware/multipart.rs +86 -86
  141. data/vendor/spikard-http/src/middleware/urlencoded.rs +147 -147
  142. data/vendor/spikard-http/src/middleware/validation.rs +287 -287
  143. data/vendor/spikard-http/src/openapi/mod.rs +309 -309
  144. data/vendor/spikard-http/src/openapi/parameter_extraction.rs +190 -190
  145. data/vendor/spikard-http/src/openapi/schema_conversion.rs +308 -308
  146. data/vendor/spikard-http/src/openapi/spec_generation.rs +195 -195
  147. data/vendor/spikard-http/src/parameters.rs +1 -1
  148. data/vendor/spikard-http/src/problem.rs +1 -1
  149. data/vendor/spikard-http/src/query_parser.rs +369 -369
  150. data/vendor/spikard-http/src/response.rs +399 -399
  151. data/vendor/spikard-http/src/router.rs +1 -1
  152. data/vendor/spikard-http/src/schema_registry.rs +1 -1
  153. data/vendor/spikard-http/src/server/handler.rs +80 -80
  154. data/vendor/spikard-http/src/server/lifecycle_execution.rs +98 -98
  155. data/vendor/spikard-http/src/server/mod.rs +805 -805
  156. data/vendor/spikard-http/src/server/request_extraction.rs +119 -119
  157. data/vendor/spikard-http/src/sse.rs +447 -447
  158. data/vendor/spikard-http/src/testing/form.rs +14 -14
  159. data/vendor/spikard-http/src/testing/multipart.rs +60 -60
  160. data/vendor/spikard-http/src/testing/test_client.rs +285 -285
  161. data/vendor/spikard-http/src/testing.rs +377 -377
  162. data/vendor/spikard-http/src/type_hints.rs +1 -1
  163. data/vendor/spikard-http/src/validation.rs +1 -1
  164. data/vendor/spikard-http/src/websocket.rs +324 -324
  165. data/vendor/spikard-rb/Cargo.toml +42 -42
  166. data/vendor/spikard-rb/build.rs +8 -8
  167. data/vendor/spikard-rb/src/background.rs +63 -63
  168. data/vendor/spikard-rb/src/config.rs +294 -294
  169. data/vendor/spikard-rb/src/conversion.rs +392 -392
  170. data/vendor/spikard-rb/src/di.rs +409 -409
  171. data/vendor/spikard-rb/src/handler.rs +534 -534
  172. data/vendor/spikard-rb/src/lib.rs +2020 -2020
  173. data/vendor/spikard-rb/src/lifecycle.rs +267 -267
  174. data/vendor/spikard-rb/src/server.rs +283 -283
  175. data/vendor/spikard-rb/src/sse.rs +231 -231
  176. data/vendor/spikard-rb/src/test_client.rs +404 -404
  177. data/vendor/spikard-rb/src/test_sse.rs +143 -143
  178. data/vendor/spikard-rb/src/test_websocket.rs +221 -221
  179. data/vendor/spikard-rb/src/websocket.rs +233 -233
  180. metadata +1 -1
@@ -1,309 +1,309 @@
1
- //! OpenAPI 3.1.0 specification generation
2
- //!
3
- //! Generates OpenAPI specs from route definitions using existing JSON Schema infrastructure.
4
- //! OpenAPI 3.1.0 is fully compatible with JSON Schema Draft 2020-12.
5
-
6
- pub mod parameter_extraction;
7
- pub mod schema_conversion;
8
- pub mod spec_generation;
9
-
10
- use crate::SchemaRegistry;
11
- use serde::{Deserialize, Serialize};
12
- use std::collections::HashMap;
13
- use utoipa::openapi::security::SecurityScheme;
14
-
15
- /// OpenAPI configuration
16
- #[derive(Debug, Clone, Serialize, Deserialize)]
17
- pub struct OpenApiConfig {
18
- /// Enable OpenAPI generation (default: false for zero overhead)
19
- pub enabled: bool,
20
-
21
- /// API title
22
- pub title: String,
23
-
24
- /// API version
25
- pub version: String,
26
-
27
- /// API description (supports markdown)
28
- #[serde(default)]
29
- pub description: Option<String>,
30
-
31
- /// Path to serve Swagger UI (default: "/docs")
32
- #[serde(default = "default_swagger_path")]
33
- pub swagger_ui_path: String,
34
-
35
- /// Path to serve Redoc (default: "/redoc")
36
- #[serde(default = "default_redoc_path")]
37
- pub redoc_path: String,
38
-
39
- /// Path to serve OpenAPI JSON spec (default: "/openapi.json")
40
- #[serde(default = "default_openapi_json_path")]
41
- pub openapi_json_path: String,
42
-
43
- /// Contact information
44
- #[serde(default)]
45
- pub contact: Option<ContactInfo>,
46
-
47
- /// License information
48
- #[serde(default)]
49
- pub license: Option<LicenseInfo>,
50
-
51
- /// Server definitions
52
- #[serde(default)]
53
- pub servers: Vec<ServerInfo>,
54
-
55
- /// Security schemes (auto-detected from middleware if not provided)
56
- #[serde(default)]
57
- pub security_schemes: HashMap<String, SecuritySchemeInfo>,
58
- }
59
-
60
- impl Default for OpenApiConfig {
61
- fn default() -> Self {
62
- Self {
63
- enabled: false,
64
- title: "API".to_string(),
65
- version: "1.0.0".to_string(),
66
- description: None,
67
- swagger_ui_path: default_swagger_path(),
68
- redoc_path: default_redoc_path(),
69
- openapi_json_path: default_openapi_json_path(),
70
- contact: None,
71
- license: None,
72
- servers: Vec::new(),
73
- security_schemes: HashMap::new(),
74
- }
75
- }
76
- }
77
-
78
- fn default_swagger_path() -> String {
79
- "/docs".to_string()
80
- }
81
-
82
- fn default_redoc_path() -> String {
83
- "/redoc".to_string()
84
- }
85
-
86
- fn default_openapi_json_path() -> String {
87
- "/openapi.json".to_string()
88
- }
89
-
90
- /// Contact information
91
- #[derive(Debug, Clone, Serialize, Deserialize)]
92
- pub struct ContactInfo {
93
- pub name: Option<String>,
94
- pub email: Option<String>,
95
- pub url: Option<String>,
96
- }
97
-
98
- /// License information
99
- #[derive(Debug, Clone, Serialize, Deserialize)]
100
- pub struct LicenseInfo {
101
- pub name: String,
102
- pub url: Option<String>,
103
- }
104
-
105
- /// Server information
106
- #[derive(Debug, Clone, Serialize, Deserialize)]
107
- pub struct ServerInfo {
108
- pub url: String,
109
- pub description: Option<String>,
110
- }
111
-
112
- /// Security scheme types
113
- #[derive(Debug, Clone, Serialize, Deserialize)]
114
- #[serde(tag = "type", rename_all = "lowercase")]
115
- pub enum SecuritySchemeInfo {
116
- #[serde(rename = "http")]
117
- Http {
118
- scheme: String,
119
- #[serde(rename = "bearerFormat")]
120
- bearer_format: Option<String>,
121
- },
122
- #[serde(rename = "apiKey")]
123
- ApiKey {
124
- #[serde(rename = "in")]
125
- location: String,
126
- name: String,
127
- },
128
- }
129
-
130
- /// Convert SecuritySchemeInfo to OpenAPI SecurityScheme
131
- pub fn security_scheme_info_to_openapi(info: &SecuritySchemeInfo) -> SecurityScheme {
132
- match info {
133
- SecuritySchemeInfo::Http { scheme, bearer_format } => {
134
- let mut http_scheme = SecurityScheme::Http(utoipa::openapi::security::Http::new(
135
- utoipa::openapi::security::HttpAuthScheme::Bearer,
136
- ));
137
- if let (SecurityScheme::Http(http), "bearer") = (&mut http_scheme, scheme.as_str()) {
138
- http.scheme = utoipa::openapi::security::HttpAuthScheme::Bearer;
139
- if let Some(format) = bearer_format {
140
- http.bearer_format = Some(format.clone());
141
- }
142
- }
143
- http_scheme
144
- }
145
- SecuritySchemeInfo::ApiKey { location, name } => {
146
- use utoipa::openapi::security::ApiKey;
147
-
148
- let api_key = match location.as_str() {
149
- "header" => ApiKey::Header(utoipa::openapi::security::ApiKeyValue::new(name)),
150
- "query" => ApiKey::Query(utoipa::openapi::security::ApiKeyValue::new(name)),
151
- "cookie" => ApiKey::Cookie(utoipa::openapi::security::ApiKeyValue::new(name)),
152
- _ => ApiKey::Header(utoipa::openapi::security::ApiKeyValue::new(name)),
153
- };
154
- SecurityScheme::ApiKey(api_key)
155
- }
156
- }
157
- }
158
-
159
- /// Generate OpenAPI specification from routes with auto-detection of security schemes
160
- pub fn generate_openapi_spec(
161
- routes: &[crate::RouteMetadata],
162
- config: &OpenApiConfig,
163
- _schema_registry: &SchemaRegistry,
164
- server_config: Option<&crate::ServerConfig>,
165
- ) -> Result<utoipa::openapi::OpenApi, String> {
166
- spec_generation::assemble_openapi_spec(routes, config, server_config)
167
- }
168
-
169
- #[cfg(test)]
170
- mod tests {
171
- use super::*;
172
-
173
- #[test]
174
- fn test_openapi_config_default() {
175
- let config = OpenApiConfig::default();
176
- assert!(!config.enabled);
177
- assert_eq!(config.title, "API");
178
- assert_eq!(config.version, "1.0.0");
179
- assert_eq!(config.swagger_ui_path, "/docs");
180
- assert_eq!(config.redoc_path, "/redoc");
181
- assert_eq!(config.openapi_json_path, "/openapi.json");
182
- }
183
-
184
- #[test]
185
- fn test_generate_minimal_spec() {
186
- let config = OpenApiConfig {
187
- enabled: true,
188
- title: "Test API".to_string(),
189
- version: "1.0.0".to_string(),
190
- ..Default::default()
191
- };
192
-
193
- let routes = vec![];
194
- let registry = SchemaRegistry::new();
195
-
196
- let spec = generate_openapi_spec(&routes, &config, &registry, None).unwrap();
197
- assert_eq!(spec.info.title, "Test API");
198
- assert_eq!(spec.info.version, "1.0.0");
199
- }
200
-
201
- #[test]
202
- fn test_generate_spec_with_contact() {
203
- let config = OpenApiConfig {
204
- enabled: true,
205
- title: "Test API".to_string(),
206
- version: "1.0.0".to_string(),
207
- contact: Some(ContactInfo {
208
- name: Some("API Team".to_string()),
209
- email: Some("api@example.com".to_string()),
210
- url: Some("https://example.com".to_string()),
211
- }),
212
- ..Default::default()
213
- };
214
-
215
- let routes = vec![];
216
- let registry = SchemaRegistry::new();
217
-
218
- let spec = generate_openapi_spec(&routes, &config, &registry, None).unwrap();
219
- assert!(spec.info.contact.is_some());
220
- let contact = spec.info.contact.unwrap();
221
- assert_eq!(contact.name, Some("API Team".to_string()));
222
- assert_eq!(contact.email, Some("api@example.com".to_string()));
223
- }
224
-
225
- #[test]
226
- fn test_generate_spec_with_license() {
227
- let config = OpenApiConfig {
228
- enabled: true,
229
- title: "Test API".to_string(),
230
- version: "1.0.0".to_string(),
231
- license: Some(LicenseInfo {
232
- name: "MIT".to_string(),
233
- url: Some("https://opensource.org/licenses/MIT".to_string()),
234
- }),
235
- ..Default::default()
236
- };
237
-
238
- let routes = vec![];
239
- let registry = SchemaRegistry::new();
240
-
241
- let spec = generate_openapi_spec(&routes, &config, &registry, None).unwrap();
242
- assert!(spec.info.license.is_some());
243
- let license = spec.info.license.unwrap();
244
- assert_eq!(license.name, "MIT");
245
- }
246
-
247
- #[test]
248
- fn test_generate_spec_with_servers() {
249
- let config = OpenApiConfig {
250
- enabled: true,
251
- title: "Test API".to_string(),
252
- version: "1.0.0".to_string(),
253
- servers: vec![
254
- ServerInfo {
255
- url: "https://api.example.com".to_string(),
256
- description: Some("Production".to_string()),
257
- },
258
- ServerInfo {
259
- url: "http://localhost:8080".to_string(),
260
- description: Some("Development".to_string()),
261
- },
262
- ],
263
- ..Default::default()
264
- };
265
-
266
- let routes = vec![];
267
- let registry = SchemaRegistry::new();
268
-
269
- let spec = generate_openapi_spec(&routes, &config, &registry, None).unwrap();
270
- assert!(spec.servers.is_some());
271
- let servers = spec.servers.unwrap();
272
- assert_eq!(servers.len(), 2);
273
- assert_eq!(servers[0].url, "https://api.example.com");
274
- assert_eq!(servers[1].url, "http://localhost:8080");
275
- }
276
-
277
- #[test]
278
- fn test_security_scheme_http_bearer() {
279
- let scheme_info = SecuritySchemeInfo::Http {
280
- scheme: "bearer".to_string(),
281
- bearer_format: Some("JWT".to_string()),
282
- };
283
-
284
- let scheme = security_scheme_info_to_openapi(&scheme_info);
285
- match scheme {
286
- SecurityScheme::Http(http) => {
287
- assert!(matches!(http.scheme, utoipa::openapi::security::HttpAuthScheme::Bearer));
288
- assert_eq!(http.bearer_format, Some("JWT".to_string()));
289
- }
290
- _ => panic!("Expected Http security scheme"),
291
- }
292
- }
293
-
294
- #[test]
295
- fn test_security_scheme_api_key() {
296
- let scheme_info = SecuritySchemeInfo::ApiKey {
297
- location: "header".to_string(),
298
- name: "X-API-Key".to_string(),
299
- };
300
-
301
- let scheme = security_scheme_info_to_openapi(&scheme_info);
302
- match scheme {
303
- SecurityScheme::ApiKey(api_key) => {
304
- assert!(matches!(api_key, utoipa::openapi::security::ApiKey::Header(_)));
305
- }
306
- _ => panic!("Expected ApiKey security scheme"),
307
- }
308
- }
309
- }
1
+ //! OpenAPI 3.1.0 specification generation
2
+ //!
3
+ //! Generates OpenAPI specs from route definitions using existing JSON Schema infrastructure.
4
+ //! OpenAPI 3.1.0 is fully compatible with JSON Schema Draft 2020-12.
5
+
6
+ pub mod parameter_extraction;
7
+ pub mod schema_conversion;
8
+ pub mod spec_generation;
9
+
10
+ use crate::SchemaRegistry;
11
+ use serde::{Deserialize, Serialize};
12
+ use std::collections::HashMap;
13
+ use utoipa::openapi::security::SecurityScheme;
14
+
15
+ /// OpenAPI configuration
16
+ #[derive(Debug, Clone, Serialize, Deserialize)]
17
+ pub struct OpenApiConfig {
18
+ /// Enable OpenAPI generation (default: false for zero overhead)
19
+ pub enabled: bool,
20
+
21
+ /// API title
22
+ pub title: String,
23
+
24
+ /// API version
25
+ pub version: String,
26
+
27
+ /// API description (supports markdown)
28
+ #[serde(default)]
29
+ pub description: Option<String>,
30
+
31
+ /// Path to serve Swagger UI (default: "/docs")
32
+ #[serde(default = "default_swagger_path")]
33
+ pub swagger_ui_path: String,
34
+
35
+ /// Path to serve Redoc (default: "/redoc")
36
+ #[serde(default = "default_redoc_path")]
37
+ pub redoc_path: String,
38
+
39
+ /// Path to serve OpenAPI JSON spec (default: "/openapi.json")
40
+ #[serde(default = "default_openapi_json_path")]
41
+ pub openapi_json_path: String,
42
+
43
+ /// Contact information
44
+ #[serde(default)]
45
+ pub contact: Option<ContactInfo>,
46
+
47
+ /// License information
48
+ #[serde(default)]
49
+ pub license: Option<LicenseInfo>,
50
+
51
+ /// Server definitions
52
+ #[serde(default)]
53
+ pub servers: Vec<ServerInfo>,
54
+
55
+ /// Security schemes (auto-detected from middleware if not provided)
56
+ #[serde(default)]
57
+ pub security_schemes: HashMap<String, SecuritySchemeInfo>,
58
+ }
59
+
60
+ impl Default for OpenApiConfig {
61
+ fn default() -> Self {
62
+ Self {
63
+ enabled: false,
64
+ title: "API".to_string(),
65
+ version: "1.0.0".to_string(),
66
+ description: None,
67
+ swagger_ui_path: default_swagger_path(),
68
+ redoc_path: default_redoc_path(),
69
+ openapi_json_path: default_openapi_json_path(),
70
+ contact: None,
71
+ license: None,
72
+ servers: Vec::new(),
73
+ security_schemes: HashMap::new(),
74
+ }
75
+ }
76
+ }
77
+
78
+ fn default_swagger_path() -> String {
79
+ "/docs".to_string()
80
+ }
81
+
82
+ fn default_redoc_path() -> String {
83
+ "/redoc".to_string()
84
+ }
85
+
86
+ fn default_openapi_json_path() -> String {
87
+ "/openapi.json".to_string()
88
+ }
89
+
90
+ /// Contact information
91
+ #[derive(Debug, Clone, Serialize, Deserialize)]
92
+ pub struct ContactInfo {
93
+ pub name: Option<String>,
94
+ pub email: Option<String>,
95
+ pub url: Option<String>,
96
+ }
97
+
98
+ /// License information
99
+ #[derive(Debug, Clone, Serialize, Deserialize)]
100
+ pub struct LicenseInfo {
101
+ pub name: String,
102
+ pub url: Option<String>,
103
+ }
104
+
105
+ /// Server information
106
+ #[derive(Debug, Clone, Serialize, Deserialize)]
107
+ pub struct ServerInfo {
108
+ pub url: String,
109
+ pub description: Option<String>,
110
+ }
111
+
112
+ /// Security scheme types
113
+ #[derive(Debug, Clone, Serialize, Deserialize)]
114
+ #[serde(tag = "type", rename_all = "lowercase")]
115
+ pub enum SecuritySchemeInfo {
116
+ #[serde(rename = "http")]
117
+ Http {
118
+ scheme: String,
119
+ #[serde(rename = "bearerFormat")]
120
+ bearer_format: Option<String>,
121
+ },
122
+ #[serde(rename = "apiKey")]
123
+ ApiKey {
124
+ #[serde(rename = "in")]
125
+ location: String,
126
+ name: String,
127
+ },
128
+ }
129
+
130
+ /// Convert SecuritySchemeInfo to OpenAPI SecurityScheme
131
+ pub fn security_scheme_info_to_openapi(info: &SecuritySchemeInfo) -> SecurityScheme {
132
+ match info {
133
+ SecuritySchemeInfo::Http { scheme, bearer_format } => {
134
+ let mut http_scheme = SecurityScheme::Http(utoipa::openapi::security::Http::new(
135
+ utoipa::openapi::security::HttpAuthScheme::Bearer,
136
+ ));
137
+ if let (SecurityScheme::Http(http), "bearer") = (&mut http_scheme, scheme.as_str()) {
138
+ http.scheme = utoipa::openapi::security::HttpAuthScheme::Bearer;
139
+ if let Some(format) = bearer_format {
140
+ http.bearer_format = Some(format.clone());
141
+ }
142
+ }
143
+ http_scheme
144
+ }
145
+ SecuritySchemeInfo::ApiKey { location, name } => {
146
+ use utoipa::openapi::security::ApiKey;
147
+
148
+ let api_key = match location.as_str() {
149
+ "header" => ApiKey::Header(utoipa::openapi::security::ApiKeyValue::new(name)),
150
+ "query" => ApiKey::Query(utoipa::openapi::security::ApiKeyValue::new(name)),
151
+ "cookie" => ApiKey::Cookie(utoipa::openapi::security::ApiKeyValue::new(name)),
152
+ _ => ApiKey::Header(utoipa::openapi::security::ApiKeyValue::new(name)),
153
+ };
154
+ SecurityScheme::ApiKey(api_key)
155
+ }
156
+ }
157
+ }
158
+
159
+ /// Generate OpenAPI specification from routes with auto-detection of security schemes
160
+ pub fn generate_openapi_spec(
161
+ routes: &[crate::RouteMetadata],
162
+ config: &OpenApiConfig,
163
+ _schema_registry: &SchemaRegistry,
164
+ server_config: Option<&crate::ServerConfig>,
165
+ ) -> Result<utoipa::openapi::OpenApi, String> {
166
+ spec_generation::assemble_openapi_spec(routes, config, server_config)
167
+ }
168
+
169
+ #[cfg(test)]
170
+ mod tests {
171
+ use super::*;
172
+
173
+ #[test]
174
+ fn test_openapi_config_default() {
175
+ let config = OpenApiConfig::default();
176
+ assert!(!config.enabled);
177
+ assert_eq!(config.title, "API");
178
+ assert_eq!(config.version, "1.0.0");
179
+ assert_eq!(config.swagger_ui_path, "/docs");
180
+ assert_eq!(config.redoc_path, "/redoc");
181
+ assert_eq!(config.openapi_json_path, "/openapi.json");
182
+ }
183
+
184
+ #[test]
185
+ fn test_generate_minimal_spec() {
186
+ let config = OpenApiConfig {
187
+ enabled: true,
188
+ title: "Test API".to_string(),
189
+ version: "1.0.0".to_string(),
190
+ ..Default::default()
191
+ };
192
+
193
+ let routes = vec![];
194
+ let registry = SchemaRegistry::new();
195
+
196
+ let spec = generate_openapi_spec(&routes, &config, &registry, None).unwrap();
197
+ assert_eq!(spec.info.title, "Test API");
198
+ assert_eq!(spec.info.version, "1.0.0");
199
+ }
200
+
201
+ #[test]
202
+ fn test_generate_spec_with_contact() {
203
+ let config = OpenApiConfig {
204
+ enabled: true,
205
+ title: "Test API".to_string(),
206
+ version: "1.0.0".to_string(),
207
+ contact: Some(ContactInfo {
208
+ name: Some("API Team".to_string()),
209
+ email: Some("api@example.com".to_string()),
210
+ url: Some("https://example.com".to_string()),
211
+ }),
212
+ ..Default::default()
213
+ };
214
+
215
+ let routes = vec![];
216
+ let registry = SchemaRegistry::new();
217
+
218
+ let spec = generate_openapi_spec(&routes, &config, &registry, None).unwrap();
219
+ assert!(spec.info.contact.is_some());
220
+ let contact = spec.info.contact.unwrap();
221
+ assert_eq!(contact.name, Some("API Team".to_string()));
222
+ assert_eq!(contact.email, Some("api@example.com".to_string()));
223
+ }
224
+
225
+ #[test]
226
+ fn test_generate_spec_with_license() {
227
+ let config = OpenApiConfig {
228
+ enabled: true,
229
+ title: "Test API".to_string(),
230
+ version: "1.0.0".to_string(),
231
+ license: Some(LicenseInfo {
232
+ name: "MIT".to_string(),
233
+ url: Some("https://opensource.org/licenses/MIT".to_string()),
234
+ }),
235
+ ..Default::default()
236
+ };
237
+
238
+ let routes = vec![];
239
+ let registry = SchemaRegistry::new();
240
+
241
+ let spec = generate_openapi_spec(&routes, &config, &registry, None).unwrap();
242
+ assert!(spec.info.license.is_some());
243
+ let license = spec.info.license.unwrap();
244
+ assert_eq!(license.name, "MIT");
245
+ }
246
+
247
+ #[test]
248
+ fn test_generate_spec_with_servers() {
249
+ let config = OpenApiConfig {
250
+ enabled: true,
251
+ title: "Test API".to_string(),
252
+ version: "1.0.0".to_string(),
253
+ servers: vec![
254
+ ServerInfo {
255
+ url: "https://api.example.com".to_string(),
256
+ description: Some("Production".to_string()),
257
+ },
258
+ ServerInfo {
259
+ url: "http://localhost:8080".to_string(),
260
+ description: Some("Development".to_string()),
261
+ },
262
+ ],
263
+ ..Default::default()
264
+ };
265
+
266
+ let routes = vec![];
267
+ let registry = SchemaRegistry::new();
268
+
269
+ let spec = generate_openapi_spec(&routes, &config, &registry, None).unwrap();
270
+ assert!(spec.servers.is_some());
271
+ let servers = spec.servers.unwrap();
272
+ assert_eq!(servers.len(), 2);
273
+ assert_eq!(servers[0].url, "https://api.example.com");
274
+ assert_eq!(servers[1].url, "http://localhost:8080");
275
+ }
276
+
277
+ #[test]
278
+ fn test_security_scheme_http_bearer() {
279
+ let scheme_info = SecuritySchemeInfo::Http {
280
+ scheme: "bearer".to_string(),
281
+ bearer_format: Some("JWT".to_string()),
282
+ };
283
+
284
+ let scheme = security_scheme_info_to_openapi(&scheme_info);
285
+ match scheme {
286
+ SecurityScheme::Http(http) => {
287
+ assert!(matches!(http.scheme, utoipa::openapi::security::HttpAuthScheme::Bearer));
288
+ assert_eq!(http.bearer_format, Some("JWT".to_string()));
289
+ }
290
+ _ => panic!("Expected Http security scheme"),
291
+ }
292
+ }
293
+
294
+ #[test]
295
+ fn test_security_scheme_api_key() {
296
+ let scheme_info = SecuritySchemeInfo::ApiKey {
297
+ location: "header".to_string(),
298
+ name: "X-API-Key".to_string(),
299
+ };
300
+
301
+ let scheme = security_scheme_info_to_openapi(&scheme_info);
302
+ match scheme {
303
+ SecurityScheme::ApiKey(api_key) => {
304
+ assert!(matches!(api_key, utoipa::openapi::security::ApiKey::Header(_)));
305
+ }
306
+ _ => panic!("Expected ApiKey security scheme"),
307
+ }
308
+ }
309
+ }