spikard 0.3.1 → 0.3.2
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.
- checksums.yaml +4 -4
- data/ext/spikard_rb/Cargo.toml +1 -1
- data/lib/spikard/version.rb +1 -1
- data/vendor/crates/spikard-core/Cargo.toml +40 -0
- data/vendor/crates/spikard-core/src/bindings/mod.rs +3 -0
- data/vendor/crates/spikard-core/src/bindings/response.rs +133 -0
- data/vendor/crates/spikard-core/src/debug.rs +63 -0
- data/vendor/crates/spikard-core/src/di/container.rs +726 -0
- data/vendor/crates/spikard-core/src/di/dependency.rs +273 -0
- data/vendor/crates/spikard-core/src/di/error.rs +118 -0
- data/vendor/crates/spikard-core/src/di/factory.rs +538 -0
- data/vendor/crates/spikard-core/src/di/graph.rs +545 -0
- data/vendor/crates/spikard-core/src/di/mod.rs +192 -0
- data/vendor/crates/spikard-core/src/di/resolved.rs +411 -0
- data/vendor/crates/spikard-core/src/di/value.rs +283 -0
- data/vendor/crates/spikard-core/src/errors.rs +39 -0
- data/vendor/crates/spikard-core/src/http.rs +153 -0
- data/vendor/crates/spikard-core/src/lib.rs +29 -0
- data/vendor/crates/spikard-core/src/lifecycle.rs +422 -0
- data/vendor/crates/spikard-core/src/parameters.rs +722 -0
- data/vendor/crates/spikard-core/src/problem.rs +310 -0
- data/vendor/crates/spikard-core/src/request_data.rs +189 -0
- data/vendor/crates/spikard-core/src/router.rs +249 -0
- data/vendor/crates/spikard-core/src/schema_registry.rs +183 -0
- data/vendor/crates/spikard-core/src/type_hints.rs +304 -0
- data/vendor/crates/spikard-core/src/validation.rs +699 -0
- data/vendor/crates/spikard-http/Cargo.toml +58 -0
- data/vendor/crates/spikard-http/src/auth.rs +247 -0
- data/vendor/crates/spikard-http/src/background.rs +249 -0
- data/vendor/crates/spikard-http/src/bindings/mod.rs +3 -0
- data/vendor/crates/spikard-http/src/bindings/response.rs +1 -0
- data/vendor/crates/spikard-http/src/body_metadata.rs +8 -0
- data/vendor/crates/spikard-http/src/cors.rs +490 -0
- data/vendor/crates/spikard-http/src/debug.rs +63 -0
- data/vendor/crates/spikard-http/src/di_handler.rs +423 -0
- data/vendor/crates/spikard-http/src/handler_response.rs +190 -0
- data/vendor/crates/spikard-http/src/handler_trait.rs +228 -0
- data/vendor/crates/spikard-http/src/handler_trait_tests.rs +284 -0
- data/vendor/crates/spikard-http/src/lib.rs +529 -0
- data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +149 -0
- data/vendor/crates/spikard-http/src/lifecycle.rs +428 -0
- data/vendor/crates/spikard-http/src/middleware/mod.rs +285 -0
- data/vendor/crates/spikard-http/src/middleware/multipart.rs +86 -0
- data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +147 -0
- data/vendor/crates/spikard-http/src/middleware/validation.rs +287 -0
- data/vendor/crates/spikard-http/src/openapi/mod.rs +309 -0
- data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +190 -0
- data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +308 -0
- data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +195 -0
- data/vendor/crates/spikard-http/src/parameters.rs +1 -0
- data/vendor/crates/spikard-http/src/problem.rs +1 -0
- data/vendor/crates/spikard-http/src/query_parser.rs +369 -0
- data/vendor/crates/spikard-http/src/response.rs +399 -0
- data/vendor/crates/spikard-http/src/router.rs +1 -0
- data/vendor/crates/spikard-http/src/schema_registry.rs +1 -0
- data/vendor/crates/spikard-http/src/server/handler.rs +87 -0
- data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +98 -0
- data/vendor/crates/spikard-http/src/server/mod.rs +805 -0
- data/vendor/crates/spikard-http/src/server/request_extraction.rs +119 -0
- data/vendor/crates/spikard-http/src/sse.rs +447 -0
- data/vendor/crates/spikard-http/src/testing/form.rs +14 -0
- data/vendor/crates/spikard-http/src/testing/multipart.rs +60 -0
- data/vendor/crates/spikard-http/src/testing/test_client.rs +285 -0
- data/vendor/crates/spikard-http/src/testing.rs +377 -0
- data/vendor/crates/spikard-http/src/type_hints.rs +1 -0
- data/vendor/crates/spikard-http/src/validation.rs +1 -0
- data/vendor/crates/spikard-http/src/websocket.rs +324 -0
- data/vendor/crates/spikard-rb/Cargo.toml +42 -0
- data/vendor/crates/spikard-rb/build.rs +8 -0
- data/vendor/crates/spikard-rb/src/background.rs +63 -0
- data/vendor/crates/spikard-rb/src/config.rs +294 -0
- data/vendor/crates/spikard-rb/src/conversion.rs +453 -0
- data/vendor/crates/spikard-rb/src/di.rs +409 -0
- data/vendor/crates/spikard-rb/src/handler.rs +625 -0
- data/vendor/crates/spikard-rb/src/lib.rs +2771 -0
- data/vendor/crates/spikard-rb/src/lifecycle.rs +274 -0
- data/vendor/crates/spikard-rb/src/server.rs +283 -0
- data/vendor/crates/spikard-rb/src/sse.rs +231 -0
- data/vendor/crates/spikard-rb/src/test_client.rs +404 -0
- data/vendor/crates/spikard-rb/src/test_sse.rs +143 -0
- data/vendor/crates/spikard-rb/src/test_websocket.rs +221 -0
- data/vendor/crates/spikard-rb/src/websocket.rs +233 -0
- data/vendor/spikard-core/Cargo.toml +40 -0
- data/vendor/spikard-core/src/bindings/mod.rs +3 -0
- data/vendor/spikard-core/src/bindings/response.rs +133 -0
- data/vendor/spikard-core/src/debug.rs +63 -0
- data/vendor/spikard-core/src/di/container.rs +726 -0
- data/vendor/spikard-core/src/di/dependency.rs +273 -0
- data/vendor/spikard-core/src/di/error.rs +118 -0
- data/vendor/spikard-core/src/di/factory.rs +538 -0
- data/vendor/spikard-core/src/di/graph.rs +545 -0
- data/vendor/spikard-core/src/di/mod.rs +192 -0
- data/vendor/spikard-core/src/di/resolved.rs +411 -0
- data/vendor/spikard-core/src/di/value.rs +283 -0
- data/vendor/spikard-core/src/http.rs +153 -0
- data/vendor/spikard-core/src/lib.rs +28 -0
- data/vendor/spikard-core/src/lifecycle.rs +422 -0
- data/vendor/spikard-core/src/parameters.rs +719 -0
- data/vendor/spikard-core/src/problem.rs +310 -0
- data/vendor/spikard-core/src/request_data.rs +189 -0
- data/vendor/spikard-core/src/router.rs +249 -0
- data/vendor/spikard-core/src/schema_registry.rs +183 -0
- data/vendor/spikard-core/src/type_hints.rs +304 -0
- data/vendor/spikard-core/src/validation.rs +699 -0
- data/vendor/spikard-http/Cargo.toml +58 -0
- data/vendor/spikard-http/src/auth.rs +247 -0
- data/vendor/spikard-http/src/background.rs +249 -0
- data/vendor/spikard-http/src/bindings/mod.rs +3 -0
- data/vendor/spikard-http/src/bindings/response.rs +1 -0
- data/vendor/spikard-http/src/body_metadata.rs +8 -0
- data/vendor/spikard-http/src/cors.rs +490 -0
- data/vendor/spikard-http/src/debug.rs +63 -0
- data/vendor/spikard-http/src/di_handler.rs +423 -0
- data/vendor/spikard-http/src/handler_response.rs +190 -0
- data/vendor/spikard-http/src/handler_trait.rs +228 -0
- data/vendor/spikard-http/src/handler_trait_tests.rs +284 -0
- data/vendor/spikard-http/src/lib.rs +529 -0
- data/vendor/spikard-http/src/lifecycle/adapter.rs +149 -0
- data/vendor/spikard-http/src/lifecycle.rs +428 -0
- data/vendor/spikard-http/src/middleware/mod.rs +285 -0
- data/vendor/spikard-http/src/middleware/multipart.rs +86 -0
- data/vendor/spikard-http/src/middleware/urlencoded.rs +147 -0
- data/vendor/spikard-http/src/middleware/validation.rs +287 -0
- data/vendor/spikard-http/src/openapi/mod.rs +309 -0
- data/vendor/spikard-http/src/openapi/parameter_extraction.rs +190 -0
- data/vendor/spikard-http/src/openapi/schema_conversion.rs +308 -0
- data/vendor/spikard-http/src/openapi/spec_generation.rs +195 -0
- data/vendor/spikard-http/src/parameters.rs +1 -0
- data/vendor/spikard-http/src/problem.rs +1 -0
- data/vendor/spikard-http/src/query_parser.rs +369 -0
- data/vendor/spikard-http/src/response.rs +399 -0
- data/vendor/spikard-http/src/router.rs +1 -0
- data/vendor/spikard-http/src/schema_registry.rs +1 -0
- data/vendor/spikard-http/src/server/handler.rs +80 -0
- data/vendor/spikard-http/src/server/lifecycle_execution.rs +98 -0
- data/vendor/spikard-http/src/server/mod.rs +805 -0
- data/vendor/spikard-http/src/server/request_extraction.rs +119 -0
- data/vendor/spikard-http/src/sse.rs +447 -0
- data/vendor/spikard-http/src/testing/form.rs +14 -0
- data/vendor/spikard-http/src/testing/multipart.rs +60 -0
- data/vendor/spikard-http/src/testing/test_client.rs +285 -0
- data/vendor/spikard-http/src/testing.rs +377 -0
- data/vendor/spikard-http/src/type_hints.rs +1 -0
- data/vendor/spikard-http/src/validation.rs +1 -0
- data/vendor/spikard-http/src/websocket.rs +324 -0
- data/vendor/spikard-rb/Cargo.toml +42 -0
- data/vendor/spikard-rb/build.rs +8 -0
- data/vendor/spikard-rb/src/background.rs +63 -0
- data/vendor/spikard-rb/src/config.rs +294 -0
- data/vendor/spikard-rb/src/conversion.rs +392 -0
- data/vendor/spikard-rb/src/di.rs +409 -0
- data/vendor/spikard-rb/src/handler.rs +534 -0
- data/vendor/spikard-rb/src/lib.rs +2020 -0
- data/vendor/spikard-rb/src/lifecycle.rs +267 -0
- data/vendor/spikard-rb/src/server.rs +283 -0
- data/vendor/spikard-rb/src/sse.rs +231 -0
- data/vendor/spikard-rb/src/test_client.rs +404 -0
- data/vendor/spikard-rb/src/test_sse.rs +143 -0
- data/vendor/spikard-rb/src/test_websocket.rs +221 -0
- data/vendor/spikard-rb/src/websocket.rs +233 -0
- metadata +158 -1
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
//! JSON schema validation middleware
|
|
2
|
+
|
|
3
|
+
use crate::problem::{CONTENT_TYPE_PROBLEM_JSON, ProblemDetails};
|
|
4
|
+
use axum::http::{HeaderMap, StatusCode};
|
|
5
|
+
use axum::response::{IntoResponse, Response};
|
|
6
|
+
use serde_json::json;
|
|
7
|
+
|
|
8
|
+
/// Check if a media type is JSON or has a +json suffix
|
|
9
|
+
pub fn is_json_content_type(mime: &mime::Mime) -> bool {
|
|
10
|
+
(mime.type_() == mime::APPLICATION && mime.subtype() == mime::JSON) || mime.suffix() == Some(mime::JSON)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/// Validate that Content-Type is JSON-compatible when route expects JSON
|
|
14
|
+
#[allow(clippy::result_large_err)]
|
|
15
|
+
pub fn validate_json_content_type(headers: &HeaderMap) -> Result<(), Response> {
|
|
16
|
+
if let Some(content_type_header) = headers.get(axum::http::header::CONTENT_TYPE)
|
|
17
|
+
&& let Ok(content_type_str) = content_type_header.to_str()
|
|
18
|
+
&& let Ok(parsed_mime) = content_type_str.parse::<mime::Mime>()
|
|
19
|
+
{
|
|
20
|
+
let is_json = (parsed_mime.type_() == mime::APPLICATION && parsed_mime.subtype() == mime::JSON)
|
|
21
|
+
|| parsed_mime.suffix() == Some(mime::JSON);
|
|
22
|
+
|
|
23
|
+
let is_form = (parsed_mime.type_() == mime::APPLICATION && parsed_mime.subtype() == "x-www-form-urlencoded")
|
|
24
|
+
|| (parsed_mime.type_() == mime::MULTIPART && parsed_mime.subtype() == "form-data");
|
|
25
|
+
|
|
26
|
+
if !is_json && !is_form {
|
|
27
|
+
let problem = ProblemDetails::new(
|
|
28
|
+
"https://spikard.dev/errors/unsupported-media-type",
|
|
29
|
+
"Unsupported Media Type",
|
|
30
|
+
StatusCode::UNSUPPORTED_MEDIA_TYPE,
|
|
31
|
+
)
|
|
32
|
+
.with_detail("Unsupported media type");
|
|
33
|
+
|
|
34
|
+
let body = problem.to_json().unwrap_or_else(|_| "{}".to_string());
|
|
35
|
+
return Err((
|
|
36
|
+
StatusCode::UNSUPPORTED_MEDIA_TYPE,
|
|
37
|
+
[(axum::http::header::CONTENT_TYPE, CONTENT_TYPE_PROBLEM_JSON)],
|
|
38
|
+
body,
|
|
39
|
+
)
|
|
40
|
+
.into_response());
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
Ok(())
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/// Validate Content-Length header matches actual body size
|
|
47
|
+
#[allow(clippy::result_large_err, clippy::collapsible_if)]
|
|
48
|
+
pub fn validate_content_length(headers: &HeaderMap, actual_size: usize) -> Result<(), Response> {
|
|
49
|
+
if let Some(content_length_header) = headers.get(axum::http::header::CONTENT_LENGTH) {
|
|
50
|
+
if let Ok(content_length_str) = content_length_header.to_str() {
|
|
51
|
+
if let Ok(declared_length) = content_length_str.parse::<usize>() {
|
|
52
|
+
if declared_length != actual_size {
|
|
53
|
+
let problem = ProblemDetails::bad_request(format!(
|
|
54
|
+
"Content-Length header ({}) does not match actual body size ({})",
|
|
55
|
+
declared_length, actual_size
|
|
56
|
+
));
|
|
57
|
+
|
|
58
|
+
let body = problem.to_json().unwrap_or_else(|_| "{}".to_string());
|
|
59
|
+
return Err((
|
|
60
|
+
StatusCode::BAD_REQUEST,
|
|
61
|
+
[(axum::http::header::CONTENT_TYPE, CONTENT_TYPE_PROBLEM_JSON)],
|
|
62
|
+
body,
|
|
63
|
+
)
|
|
64
|
+
.into_response());
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
Ok(())
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/// Validate Content-Type header and related requirements
|
|
73
|
+
#[allow(clippy::result_large_err)]
|
|
74
|
+
pub fn validate_content_type_headers(headers: &HeaderMap, _declared_body_size: usize) -> Result<(), Response> {
|
|
75
|
+
if let Some(content_type_str) = headers
|
|
76
|
+
.get(axum::http::header::CONTENT_TYPE)
|
|
77
|
+
.and_then(|h| h.to_str().ok())
|
|
78
|
+
{
|
|
79
|
+
let parsed_mime = match content_type_str.parse::<mime::Mime>() {
|
|
80
|
+
Ok(m) => m,
|
|
81
|
+
Err(_) => {
|
|
82
|
+
let error_body = json!({
|
|
83
|
+
"error": format!("Invalid Content-Type header: {}", content_type_str)
|
|
84
|
+
});
|
|
85
|
+
return Err((StatusCode::BAD_REQUEST, axum::Json(error_body)).into_response());
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
let is_json = is_json_content_type(&parsed_mime);
|
|
90
|
+
let is_multipart = parsed_mime.type_() == mime::MULTIPART && parsed_mime.subtype() == "form-data";
|
|
91
|
+
|
|
92
|
+
if is_multipart && parsed_mime.get_param(mime::BOUNDARY).is_none() {
|
|
93
|
+
let error_body = json!({
|
|
94
|
+
"error": "multipart/form-data requires 'boundary' parameter"
|
|
95
|
+
});
|
|
96
|
+
return Err((StatusCode::BAD_REQUEST, axum::Json(error_body)).into_response());
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
#[allow(clippy::collapsible_if)]
|
|
100
|
+
if is_json {
|
|
101
|
+
if let Some(charset) = parsed_mime.get_param(mime::CHARSET).map(|c| c.as_str()) {
|
|
102
|
+
if !charset.eq_ignore_ascii_case("utf-8") && !charset.eq_ignore_ascii_case("utf8") {
|
|
103
|
+
let problem = ProblemDetails::new(
|
|
104
|
+
"https://spikard.dev/errors/unsupported-charset",
|
|
105
|
+
"Unsupported Charset",
|
|
106
|
+
StatusCode::UNSUPPORTED_MEDIA_TYPE,
|
|
107
|
+
)
|
|
108
|
+
.with_detail(format!(
|
|
109
|
+
"Unsupported charset '{}' for JSON. Only UTF-8 is supported.",
|
|
110
|
+
charset
|
|
111
|
+
));
|
|
112
|
+
|
|
113
|
+
let body = problem.to_json().unwrap_or_else(|_| "{}".to_string());
|
|
114
|
+
return Err((
|
|
115
|
+
StatusCode::UNSUPPORTED_MEDIA_TYPE,
|
|
116
|
+
[(axum::http::header::CONTENT_TYPE, CONTENT_TYPE_PROBLEM_JSON)],
|
|
117
|
+
body,
|
|
118
|
+
)
|
|
119
|
+
.into_response());
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
Ok(())
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
#[cfg(test)]
|
|
129
|
+
mod tests {
|
|
130
|
+
use super::*;
|
|
131
|
+
use axum::http::HeaderValue;
|
|
132
|
+
|
|
133
|
+
#[test]
|
|
134
|
+
fn validate_content_length_accepts_matching_sizes() {
|
|
135
|
+
let mut headers = HeaderMap::new();
|
|
136
|
+
headers.insert(axum::http::header::CONTENT_LENGTH, HeaderValue::from_static("5"));
|
|
137
|
+
|
|
138
|
+
assert!(validate_content_length(&headers, 5).is_ok());
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
#[test]
|
|
142
|
+
fn validate_content_length_rejects_mismatched_sizes() {
|
|
143
|
+
let mut headers = HeaderMap::new();
|
|
144
|
+
headers.insert(axum::http::header::CONTENT_LENGTH, HeaderValue::from_static("10"));
|
|
145
|
+
|
|
146
|
+
let err = validate_content_length(&headers, 4).expect_err("expected mismatch");
|
|
147
|
+
assert_eq!(err.status(), StatusCode::BAD_REQUEST);
|
|
148
|
+
assert_eq!(
|
|
149
|
+
err.headers()
|
|
150
|
+
.get(axum::http::header::CONTENT_TYPE)
|
|
151
|
+
.and_then(|value| value.to_str().ok()),
|
|
152
|
+
Some(CONTENT_TYPE_PROBLEM_JSON)
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
#[test]
|
|
157
|
+
fn test_multipart_without_boundary() {
|
|
158
|
+
let mut headers = HeaderMap::new();
|
|
159
|
+
headers.insert(
|
|
160
|
+
axum::http::header::CONTENT_TYPE,
|
|
161
|
+
HeaderValue::from_static("multipart/form-data"),
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
let result = validate_content_type_headers(&headers, 0);
|
|
165
|
+
assert!(result.is_err());
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
#[test]
|
|
169
|
+
fn test_multipart_with_boundary() {
|
|
170
|
+
let mut headers = HeaderMap::new();
|
|
171
|
+
headers.insert(
|
|
172
|
+
axum::http::header::CONTENT_TYPE,
|
|
173
|
+
HeaderValue::from_static("multipart/form-data; boundary=----WebKitFormBoundary"),
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
let result = validate_content_type_headers(&headers, 0);
|
|
177
|
+
assert!(result.is_ok());
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
#[test]
|
|
181
|
+
fn test_json_with_utf16_charset() {
|
|
182
|
+
let mut headers = HeaderMap::new();
|
|
183
|
+
headers.insert(
|
|
184
|
+
axum::http::header::CONTENT_TYPE,
|
|
185
|
+
HeaderValue::from_static("application/json; charset=utf-16"),
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
let result = validate_content_type_headers(&headers, 0);
|
|
189
|
+
assert!(result.is_err());
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
#[test]
|
|
193
|
+
fn test_json_with_utf8_charset() {
|
|
194
|
+
let mut headers = HeaderMap::new();
|
|
195
|
+
headers.insert(
|
|
196
|
+
axum::http::header::CONTENT_TYPE,
|
|
197
|
+
HeaderValue::from_static("application/json; charset=utf-8"),
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
let result = validate_content_type_headers(&headers, 0);
|
|
201
|
+
assert!(result.is_ok());
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
#[test]
|
|
205
|
+
fn test_json_without_charset() {
|
|
206
|
+
let mut headers = HeaderMap::new();
|
|
207
|
+
headers.insert(
|
|
208
|
+
axum::http::header::CONTENT_TYPE,
|
|
209
|
+
HeaderValue::from_static("application/json"),
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
let result = validate_content_type_headers(&headers, 0);
|
|
213
|
+
assert!(result.is_ok());
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
#[test]
|
|
217
|
+
fn test_vendor_json_accepted() {
|
|
218
|
+
let mut headers = HeaderMap::new();
|
|
219
|
+
headers.insert(
|
|
220
|
+
axum::http::header::CONTENT_TYPE,
|
|
221
|
+
HeaderValue::from_static("application/vnd.api+json"),
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
let result = validate_content_type_headers(&headers, 0);
|
|
225
|
+
assert!(result.is_ok());
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
#[test]
|
|
229
|
+
fn test_problem_json_accepted() {
|
|
230
|
+
let mut headers = HeaderMap::new();
|
|
231
|
+
headers.insert(
|
|
232
|
+
axum::http::header::CONTENT_TYPE,
|
|
233
|
+
HeaderValue::from_static("application/problem+json"),
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
let result = validate_content_type_headers(&headers, 0);
|
|
237
|
+
assert!(result.is_ok());
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
#[test]
|
|
241
|
+
fn test_vendor_json_with_utf16_charset_rejected() {
|
|
242
|
+
let mut headers = HeaderMap::new();
|
|
243
|
+
headers.insert(
|
|
244
|
+
axum::http::header::CONTENT_TYPE,
|
|
245
|
+
HeaderValue::from_static("application/vnd.api+json; charset=utf-16"),
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
let result = validate_content_type_headers(&headers, 0);
|
|
249
|
+
assert!(result.is_err());
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
#[test]
|
|
253
|
+
fn test_vendor_json_with_utf8_charset_accepted() {
|
|
254
|
+
let mut headers = HeaderMap::new();
|
|
255
|
+
headers.insert(
|
|
256
|
+
axum::http::header::CONTENT_TYPE,
|
|
257
|
+
HeaderValue::from_static("application/vnd.api+json; charset=utf-8"),
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
let result = validate_content_type_headers(&headers, 0);
|
|
261
|
+
assert!(result.is_ok());
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
#[test]
|
|
265
|
+
fn test_is_json_content_type() {
|
|
266
|
+
let mime = "application/json".parse::<mime::Mime>().unwrap();
|
|
267
|
+
assert!(is_json_content_type(&mime));
|
|
268
|
+
|
|
269
|
+
let mime = "application/vnd.api+json".parse::<mime::Mime>().unwrap();
|
|
270
|
+
assert!(is_json_content_type(&mime));
|
|
271
|
+
|
|
272
|
+
let mime = "application/problem+json".parse::<mime::Mime>().unwrap();
|
|
273
|
+
assert!(is_json_content_type(&mime));
|
|
274
|
+
|
|
275
|
+
let mime = "application/hal+json".parse::<mime::Mime>().unwrap();
|
|
276
|
+
assert!(is_json_content_type(&mime));
|
|
277
|
+
|
|
278
|
+
let mime = "text/plain".parse::<mime::Mime>().unwrap();
|
|
279
|
+
assert!(!is_json_content_type(&mime));
|
|
280
|
+
|
|
281
|
+
let mime = "application/xml".parse::<mime::Mime>().unwrap();
|
|
282
|
+
assert!(!is_json_content_type(&mime));
|
|
283
|
+
|
|
284
|
+
let mime = "application/x-www-form-urlencoded".parse::<mime::Mime>().unwrap();
|
|
285
|
+
assert!(!is_json_content_type(&mime));
|
|
286
|
+
}
|
|
287
|
+
}
|
|
@@ -0,0 +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, ®istry, 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, ®istry, 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, ®istry, 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, ®istry, 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
|
+
}
|