spikard 0.5.0 → 0.6.1

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 (135) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/README.md +674 -674
  4. data/ext/spikard_rb/Cargo.toml +17 -17
  5. data/ext/spikard_rb/extconf.rb +13 -10
  6. data/ext/spikard_rb/src/lib.rs +6 -6
  7. data/lib/spikard/app.rb +405 -405
  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 -256
  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 -366
  23. data/vendor/crates/spikard-bindings-shared/Cargo.toml +63 -63
  24. data/vendor/crates/spikard-bindings-shared/examples/config_extraction.rs +132 -132
  25. data/vendor/crates/spikard-bindings-shared/src/config_extractor.rs +752 -752
  26. data/vendor/crates/spikard-bindings-shared/src/conversion_traits.rs +194 -194
  27. data/vendor/crates/spikard-bindings-shared/src/di_traits.rs +246 -246
  28. data/vendor/crates/spikard-bindings-shared/src/error_response.rs +401 -401
  29. data/vendor/crates/spikard-bindings-shared/src/handler_base.rs +238 -238
  30. data/vendor/crates/spikard-bindings-shared/src/lib.rs +24 -24
  31. data/vendor/crates/spikard-bindings-shared/src/lifecycle_base.rs +292 -292
  32. data/vendor/crates/spikard-bindings-shared/src/lifecycle_executor.rs +616 -616
  33. data/vendor/crates/spikard-bindings-shared/src/response_builder.rs +305 -305
  34. data/vendor/crates/spikard-bindings-shared/src/test_client_base.rs +248 -248
  35. data/vendor/crates/spikard-bindings-shared/src/validation_helpers.rs +351 -351
  36. data/vendor/crates/spikard-bindings-shared/tests/comprehensive_coverage.rs +454 -454
  37. data/vendor/crates/spikard-bindings-shared/tests/error_response_edge_cases.rs +383 -383
  38. data/vendor/crates/spikard-bindings-shared/tests/handler_base_integration.rs +280 -280
  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 -127
  43. data/vendor/crates/spikard-core/src/di/container.rs +702 -702
  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 -534
  47. data/vendor/crates/spikard-core/src/di/graph.rs +506 -506
  48. data/vendor/crates/spikard-core/src/di/mod.rs +192 -192
  49. data/vendor/crates/spikard-core/src/di/resolved.rs +405 -405
  50. data/vendor/crates/spikard-core/src/di/value.rs +281 -281
  51. data/vendor/crates/spikard-core/src/errors.rs +69 -69
  52. data/vendor/crates/spikard-core/src/http.rs +415 -415
  53. data/vendor/crates/spikard-core/src/lib.rs +29 -29
  54. data/vendor/crates/spikard-core/src/lifecycle.rs +1186 -1186
  55. data/vendor/crates/spikard-core/src/metadata.rs +389 -389
  56. data/vendor/crates/spikard-core/src/parameters.rs +2525 -2525
  57. data/vendor/crates/spikard-core/src/problem.rs +344 -344
  58. data/vendor/crates/spikard-core/src/request_data.rs +1154 -1154
  59. data/vendor/crates/spikard-core/src/router.rs +510 -510
  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 +696 -688
  63. data/vendor/crates/spikard-core/src/validation/mod.rs +457 -457
  64. data/vendor/crates/spikard-http/Cargo.toml +62 -64
  65. data/vendor/crates/spikard-http/examples/sse-notifications.rs +148 -148
  66. data/vendor/crates/spikard-http/examples/websocket-chat.rs +92 -92
  67. data/vendor/crates/spikard-http/src/auth.rs +296 -296
  68. data/vendor/crates/spikard-http/src/background.rs +1860 -1860
  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 -1005
  73. data/vendor/crates/spikard-http/src/debug.rs +128 -128
  74. data/vendor/crates/spikard-http/src/di_handler.rs +1668 -1668
  75. data/vendor/crates/spikard-http/src/handler_response.rs +901 -901
  76. data/vendor/crates/spikard-http/src/handler_trait.rs +838 -830
  77. data/vendor/crates/spikard-http/src/handler_trait_tests.rs +290 -290
  78. data/vendor/crates/spikard-http/src/lib.rs +534 -534
  79. data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +230 -230
  80. data/vendor/crates/spikard-http/src/lifecycle.rs +1193 -1193
  81. data/vendor/crates/spikard-http/src/middleware/mod.rs +560 -540
  82. data/vendor/crates/spikard-http/src/middleware/multipart.rs +912 -912
  83. data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +513 -513
  84. data/vendor/crates/spikard-http/src/middleware/validation.rs +768 -735
  85. data/vendor/crates/spikard-http/src/openapi/mod.rs +309 -309
  86. data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +535 -535
  87. data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +1363 -1363
  88. data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +665 -665
  89. data/vendor/crates/spikard-http/src/query_parser.rs +793 -793
  90. data/vendor/crates/spikard-http/src/response.rs +720 -720
  91. data/vendor/crates/spikard-http/src/server/handler.rs +1650 -1650
  92. data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +234 -234
  93. data/vendor/crates/spikard-http/src/server/mod.rs +1593 -1502
  94. data/vendor/crates/spikard-http/src/server/request_extraction.rs +789 -770
  95. data/vendor/crates/spikard-http/src/server/routing_factory.rs +629 -599
  96. data/vendor/crates/spikard-http/src/sse.rs +1409 -1409
  97. data/vendor/crates/spikard-http/src/testing/form.rs +52 -52
  98. data/vendor/crates/spikard-http/src/testing/multipart.rs +64 -60
  99. data/vendor/crates/spikard-http/src/testing/test_client.rs +311 -283
  100. data/vendor/crates/spikard-http/src/testing.rs +406 -377
  101. data/vendor/crates/spikard-http/src/websocket.rs +1404 -1375
  102. data/vendor/crates/spikard-http/tests/background_behavior.rs +832 -832
  103. data/vendor/crates/spikard-http/tests/common/handlers.rs +309 -309
  104. data/vendor/crates/spikard-http/tests/common/mod.rs +26 -26
  105. data/vendor/crates/spikard-http/tests/di_integration.rs +192 -192
  106. data/vendor/crates/spikard-http/tests/doc_snippets.rs +5 -5
  107. data/vendor/crates/spikard-http/tests/lifecycle_execution.rs +1093 -1093
  108. data/vendor/crates/spikard-http/tests/multipart_behavior.rs +656 -656
  109. data/vendor/crates/spikard-http/tests/server_config_builder.rs +314 -314
  110. data/vendor/crates/spikard-http/tests/sse_behavior.rs +620 -620
  111. data/vendor/crates/spikard-http/tests/websocket_behavior.rs +663 -663
  112. data/vendor/crates/spikard-rb/Cargo.toml +48 -48
  113. data/vendor/crates/spikard-rb/build.rs +199 -199
  114. data/vendor/crates/spikard-rb/src/background.rs +63 -63
  115. data/vendor/crates/spikard-rb/src/config/mod.rs +5 -5
  116. data/vendor/crates/spikard-rb/src/config/server_config.rs +285 -285
  117. data/vendor/crates/spikard-rb/src/conversion.rs +554 -554
  118. data/vendor/crates/spikard-rb/src/di/builder.rs +100 -100
  119. data/vendor/crates/spikard-rb/src/di/mod.rs +375 -375
  120. data/vendor/crates/spikard-rb/src/handler.rs +618 -618
  121. data/vendor/crates/spikard-rb/src/integration/mod.rs +3 -3
  122. data/vendor/crates/spikard-rb/src/lib.rs +1806 -1810
  123. data/vendor/crates/spikard-rb/src/lifecycle.rs +275 -275
  124. data/vendor/crates/spikard-rb/src/metadata/mod.rs +5 -5
  125. data/vendor/crates/spikard-rb/src/metadata/route_extraction.rs +442 -447
  126. data/vendor/crates/spikard-rb/src/runtime/mod.rs +5 -5
  127. data/vendor/crates/spikard-rb/src/runtime/server_runner.rs +324 -324
  128. data/vendor/crates/spikard-rb/src/server.rs +305 -308
  129. data/vendor/crates/spikard-rb/src/sse.rs +231 -231
  130. data/vendor/crates/spikard-rb/src/testing/client.rs +538 -551
  131. data/vendor/crates/spikard-rb/src/testing/mod.rs +7 -7
  132. data/vendor/crates/spikard-rb/src/testing/sse.rs +143 -143
  133. data/vendor/crates/spikard-rb/src/testing/websocket.rs +608 -635
  134. data/vendor/crates/spikard-rb/src/websocket.rs +377 -374
  135. metadata +15 -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
+ }