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,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
+ }