spikard 0.3.5 → 0.3.6
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/LICENSE +1 -1
- data/README.md +659 -659
- data/ext/spikard_rb/Cargo.toml +17 -17
- data/ext/spikard_rb/extconf.rb +10 -10
- data/ext/spikard_rb/src/lib.rs +6 -6
- data/lib/spikard/app.rb +386 -386
- data/lib/spikard/background.rb +27 -27
- data/lib/spikard/config.rb +396 -396
- data/lib/spikard/converters.rb +13 -13
- data/lib/spikard/handler_wrapper.rb +113 -113
- data/lib/spikard/provide.rb +214 -214
- data/lib/spikard/response.rb +173 -173
- data/lib/spikard/schema.rb +243 -243
- data/lib/spikard/sse.rb +111 -111
- data/lib/spikard/streaming_response.rb +44 -44
- data/lib/spikard/testing.rb +221 -221
- data/lib/spikard/upload_file.rb +131 -131
- data/lib/spikard/version.rb +5 -5
- data/lib/spikard/websocket.rb +59 -59
- data/lib/spikard.rb +43 -43
- data/sig/spikard.rbs +366 -360
- data/vendor/crates/spikard-core/Cargo.toml +40 -40
- data/vendor/crates/spikard-core/src/bindings/mod.rs +3 -3
- data/vendor/crates/spikard-core/src/bindings/response.rs +133 -133
- data/vendor/crates/spikard-core/src/debug.rs +63 -63
- data/vendor/crates/spikard-core/src/di/container.rs +726 -726
- data/vendor/crates/spikard-core/src/di/dependency.rs +273 -273
- data/vendor/crates/spikard-core/src/di/error.rs +118 -118
- data/vendor/crates/spikard-core/src/di/factory.rs +538 -538
- data/vendor/crates/spikard-core/src/di/graph.rs +545 -545
- data/vendor/crates/spikard-core/src/di/mod.rs +192 -192
- data/vendor/crates/spikard-core/src/di/resolved.rs +411 -411
- data/vendor/crates/spikard-core/src/di/value.rs +283 -283
- data/vendor/crates/spikard-core/src/errors.rs +39 -39
- data/vendor/crates/spikard-core/src/http.rs +153 -153
- data/vendor/crates/spikard-core/src/lib.rs +29 -29
- data/vendor/crates/spikard-core/src/lifecycle.rs +422 -422
- data/vendor/crates/spikard-core/src/parameters.rs +722 -722
- data/vendor/crates/spikard-core/src/problem.rs +310 -310
- data/vendor/crates/spikard-core/src/request_data.rs +189 -189
- data/vendor/crates/spikard-core/src/router.rs +249 -249
- data/vendor/crates/spikard-core/src/schema_registry.rs +183 -183
- data/vendor/crates/spikard-core/src/type_hints.rs +304 -304
- data/vendor/crates/spikard-core/src/validation.rs +699 -699
- data/vendor/crates/spikard-http/Cargo.toml +68 -68
- data/vendor/crates/spikard-http/src/auth.rs +247 -247
- data/vendor/crates/spikard-http/src/background.rs +249 -249
- data/vendor/crates/spikard-http/src/bindings/mod.rs +3 -3
- data/vendor/crates/spikard-http/src/bindings/response.rs +1 -1
- data/vendor/crates/spikard-http/src/body_metadata.rs +8 -8
- data/vendor/crates/spikard-http/src/cors.rs +490 -490
- data/vendor/crates/spikard-http/src/debug.rs +63 -63
- data/vendor/crates/spikard-http/src/di_handler.rs +423 -423
- data/vendor/crates/spikard-http/src/handler_response.rs +190 -190
- data/vendor/crates/spikard-http/src/handler_trait.rs +228 -228
- data/vendor/crates/spikard-http/src/handler_trait_tests.rs +284 -284
- data/vendor/crates/spikard-http/src/lib.rs +529 -529
- data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +149 -149
- data/vendor/crates/spikard-http/src/lifecycle.rs +428 -428
- data/vendor/crates/spikard-http/src/middleware/mod.rs +285 -285
- data/vendor/crates/spikard-http/src/middleware/multipart.rs +86 -86
- data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +147 -147
- data/vendor/crates/spikard-http/src/middleware/validation.rs +287 -287
- data/vendor/crates/spikard-http/src/openapi/mod.rs +309 -309
- data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +190 -190
- data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +308 -308
- data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +195 -195
- data/vendor/crates/spikard-http/src/parameters.rs +1 -1
- data/vendor/crates/spikard-http/src/problem.rs +1 -1
- data/vendor/crates/spikard-http/src/query_parser.rs +369 -369
- data/vendor/crates/spikard-http/src/response.rs +399 -399
- data/vendor/crates/spikard-http/src/router.rs +1 -1
- data/vendor/crates/spikard-http/src/schema_registry.rs +1 -1
- data/vendor/crates/spikard-http/src/server/handler.rs +87 -87
- data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +98 -98
- data/vendor/crates/spikard-http/src/server/mod.rs +805 -805
- data/vendor/crates/spikard-http/src/server/request_extraction.rs +119 -119
- data/vendor/crates/spikard-http/src/sse.rs +447 -447
- data/vendor/crates/spikard-http/src/testing/form.rs +14 -14
- data/vendor/crates/spikard-http/src/testing/multipart.rs +60 -60
- data/vendor/crates/spikard-http/src/testing/test_client.rs +285 -285
- data/vendor/crates/spikard-http/src/testing.rs +377 -377
- data/vendor/crates/spikard-http/src/type_hints.rs +1 -1
- data/vendor/crates/spikard-http/src/validation.rs +1 -1
- data/vendor/crates/spikard-http/src/websocket.rs +324 -324
- data/vendor/crates/spikard-rb/Cargo.toml +42 -42
- data/vendor/crates/spikard-rb/build.rs +8 -8
- data/vendor/crates/spikard-rb/src/background.rs +63 -63
- data/vendor/crates/spikard-rb/src/config.rs +294 -294
- data/vendor/crates/spikard-rb/src/conversion.rs +453 -453
- data/vendor/crates/spikard-rb/src/di.rs +409 -409
- data/vendor/crates/spikard-rb/src/handler.rs +625 -625
- data/vendor/crates/spikard-rb/src/lib.rs +2771 -2771
- data/vendor/crates/spikard-rb/src/lifecycle.rs +274 -274
- data/vendor/crates/spikard-rb/src/server.rs +283 -283
- data/vendor/crates/spikard-rb/src/sse.rs +231 -231
- data/vendor/crates/spikard-rb/src/test_client.rs +404 -404
- data/vendor/crates/spikard-rb/src/test_sse.rs +143 -143
- data/vendor/crates/spikard-rb/src/test_websocket.rs +221 -221
- data/vendor/crates/spikard-rb/src/websocket.rs +233 -233
- metadata +1 -1
|
@@ -1,308 +1,308 @@
|
|
|
1
|
-
//! JSON Schema to OpenAPI schema conversion utilities
|
|
2
|
-
|
|
3
|
-
use utoipa::openapi::{RefOr, Schema};
|
|
4
|
-
|
|
5
|
-
/// Convert serde_json::Value (JSON Schema) to utoipa Schema
|
|
6
|
-
/// OpenAPI 3.1.0 is fully compatible with JSON Schema Draft 2020-12
|
|
7
|
-
pub fn json_value_to_schema(value: &serde_json::Value) -> Result<RefOr<Schema>, String> {
|
|
8
|
-
if let Some(type_str) = value.get("type").and_then(|t| t.as_str()) {
|
|
9
|
-
match type_str {
|
|
10
|
-
"object" => {
|
|
11
|
-
let mut object_schema = utoipa::openapi::ObjectBuilder::new();
|
|
12
|
-
|
|
13
|
-
if let Some(properties) = value.get("properties").and_then(|p| p.as_object()) {
|
|
14
|
-
for (prop_name, prop_schema) in properties {
|
|
15
|
-
let prop_openapi_schema = json_value_to_schema(prop_schema)?;
|
|
16
|
-
object_schema = object_schema.property(prop_name, prop_openapi_schema);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if let Some(required) = value.get("required").and_then(|r| r.as_array()) {
|
|
21
|
-
for field in required {
|
|
22
|
-
if let Some(field_name) = field.as_str() {
|
|
23
|
-
object_schema = object_schema.required(field_name);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
Ok(RefOr::T(Schema::Object(object_schema.build())))
|
|
29
|
-
}
|
|
30
|
-
"array" => {
|
|
31
|
-
let mut array_schema = utoipa::openapi::ArrayBuilder::new();
|
|
32
|
-
|
|
33
|
-
if let Some(items) = value.get("items") {
|
|
34
|
-
let items_schema = json_value_to_schema(items)?;
|
|
35
|
-
array_schema = array_schema.items(items_schema);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
Ok(RefOr::T(Schema::Array(array_schema.build())))
|
|
39
|
-
}
|
|
40
|
-
"string" => {
|
|
41
|
-
let mut schema_type = utoipa::openapi::schema::Type::String;
|
|
42
|
-
|
|
43
|
-
if let Some(format) = value.get("format").and_then(|f| f.as_str()) {
|
|
44
|
-
match format {
|
|
45
|
-
"date-time" => schema_type = utoipa::openapi::schema::Type::String,
|
|
46
|
-
"date" => schema_type = utoipa::openapi::schema::Type::String,
|
|
47
|
-
"email" => schema_type = utoipa::openapi::schema::Type::String,
|
|
48
|
-
"uri" => schema_type = utoipa::openapi::schema::Type::String,
|
|
49
|
-
_ => {}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
Ok(RefOr::T(Schema::Object(
|
|
54
|
-
utoipa::openapi::ObjectBuilder::new().schema_type(schema_type).build(),
|
|
55
|
-
)))
|
|
56
|
-
}
|
|
57
|
-
"integer" => Ok(RefOr::T(Schema::Object(
|
|
58
|
-
utoipa::openapi::ObjectBuilder::new()
|
|
59
|
-
.schema_type(utoipa::openapi::schema::Type::Integer)
|
|
60
|
-
.build(),
|
|
61
|
-
))),
|
|
62
|
-
"number" => Ok(RefOr::T(Schema::Object(
|
|
63
|
-
utoipa::openapi::ObjectBuilder::new()
|
|
64
|
-
.schema_type(utoipa::openapi::schema::Type::Number)
|
|
65
|
-
.build(),
|
|
66
|
-
))),
|
|
67
|
-
"boolean" => Ok(RefOr::T(Schema::Object(
|
|
68
|
-
utoipa::openapi::ObjectBuilder::new()
|
|
69
|
-
.schema_type(utoipa::openapi::schema::Type::Boolean)
|
|
70
|
-
.build(),
|
|
71
|
-
))),
|
|
72
|
-
_ => Err(format!("Unsupported schema type: {}", type_str)),
|
|
73
|
-
}
|
|
74
|
-
} else {
|
|
75
|
-
Ok(RefOr::T(Schema::Object(utoipa::openapi::ObjectBuilder::new().build())))
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/// Convert JSON Schema to OpenAPI RequestBody
|
|
80
|
-
pub fn json_schema_to_request_body(
|
|
81
|
-
schema: &serde_json::Value,
|
|
82
|
-
) -> Result<utoipa::openapi::request_body::RequestBody, String> {
|
|
83
|
-
use utoipa::openapi::content::ContentBuilder;
|
|
84
|
-
|
|
85
|
-
let openapi_schema = json_value_to_schema(schema)?;
|
|
86
|
-
|
|
87
|
-
let content = ContentBuilder::new().schema(Some(openapi_schema)).build();
|
|
88
|
-
|
|
89
|
-
let mut request_body = utoipa::openapi::request_body::RequestBody::new();
|
|
90
|
-
request_body.content.insert("application/json".to_string(), content);
|
|
91
|
-
|
|
92
|
-
request_body.required = Some(utoipa::openapi::Required::True);
|
|
93
|
-
|
|
94
|
-
Ok(request_body)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/// Convert JSON Schema to OpenAPI Response
|
|
98
|
-
pub fn json_schema_to_response(schema: &serde_json::Value) -> Result<utoipa::openapi::Response, String> {
|
|
99
|
-
use utoipa::openapi::content::ContentBuilder;
|
|
100
|
-
|
|
101
|
-
let openapi_schema = json_value_to_schema(schema)?;
|
|
102
|
-
|
|
103
|
-
let content = ContentBuilder::new().schema(Some(openapi_schema)).build();
|
|
104
|
-
|
|
105
|
-
let mut response = utoipa::openapi::Response::new("Successful response");
|
|
106
|
-
response.content.insert("application/json".to_string(), content);
|
|
107
|
-
|
|
108
|
-
Ok(response)
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
#[cfg(test)]
|
|
112
|
-
mod tests {
|
|
113
|
-
use super::*;
|
|
114
|
-
|
|
115
|
-
#[test]
|
|
116
|
-
fn test_json_value_to_schema_string() {
|
|
117
|
-
let schema_json = serde_json::json!({
|
|
118
|
-
"type": "string"
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
let result = json_value_to_schema(&schema_json);
|
|
122
|
-
assert!(result.is_ok());
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
#[test]
|
|
126
|
-
fn test_json_value_to_schema_integer() {
|
|
127
|
-
let schema_json = serde_json::json!({
|
|
128
|
-
"type": "integer"
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
let result = json_value_to_schema(&schema_json);
|
|
132
|
-
assert!(result.is_ok());
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
#[test]
|
|
136
|
-
fn test_json_value_to_schema_number() {
|
|
137
|
-
let schema_json = serde_json::json!({
|
|
138
|
-
"type": "number"
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
let result = json_value_to_schema(&schema_json);
|
|
142
|
-
assert!(result.is_ok());
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
#[test]
|
|
146
|
-
fn test_json_value_to_schema_boolean() {
|
|
147
|
-
let schema_json = serde_json::json!({
|
|
148
|
-
"type": "boolean"
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
let result = json_value_to_schema(&schema_json);
|
|
152
|
-
assert!(result.is_ok());
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
#[test]
|
|
156
|
-
fn test_json_value_to_schema_object() {
|
|
157
|
-
let schema_json = serde_json::json!({
|
|
158
|
-
"type": "object",
|
|
159
|
-
"properties": {
|
|
160
|
-
"name": { "type": "string" },
|
|
161
|
-
"age": { "type": "integer" }
|
|
162
|
-
},
|
|
163
|
-
"required": ["name"]
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
let result = json_value_to_schema(&schema_json);
|
|
167
|
-
assert!(result.is_ok());
|
|
168
|
-
|
|
169
|
-
if let Ok(RefOr::T(Schema::Object(obj))) = result {
|
|
170
|
-
assert!(obj.properties.contains_key("name"));
|
|
171
|
-
assert!(obj.properties.contains_key("age"));
|
|
172
|
-
assert!(obj.required.contains(&"name".to_string()));
|
|
173
|
-
} else {
|
|
174
|
-
panic!("Expected Object schema");
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
#[test]
|
|
179
|
-
fn test_json_value_to_schema_array() {
|
|
180
|
-
let schema_json = serde_json::json!({
|
|
181
|
-
"type": "array",
|
|
182
|
-
"items": {
|
|
183
|
-
"type": "string"
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
let result = json_value_to_schema(&schema_json);
|
|
188
|
-
assert!(result.is_ok());
|
|
189
|
-
|
|
190
|
-
if let Ok(RefOr::T(Schema::Array(_))) = result {
|
|
191
|
-
} else {
|
|
192
|
-
panic!("Expected Array schema");
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
#[test]
|
|
197
|
-
fn test_json_value_to_schema_nested_object() {
|
|
198
|
-
let schema_json = serde_json::json!({
|
|
199
|
-
"type": "object",
|
|
200
|
-
"properties": {
|
|
201
|
-
"user": {
|
|
202
|
-
"type": "object",
|
|
203
|
-
"properties": {
|
|
204
|
-
"name": { "type": "string" },
|
|
205
|
-
"email": { "type": "string" }
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
let result = json_value_to_schema(&schema_json);
|
|
212
|
-
assert!(result.is_ok());
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
#[test]
|
|
216
|
-
fn test_json_schema_to_request_body() {
|
|
217
|
-
let schema_json = serde_json::json!({
|
|
218
|
-
"type": "object",
|
|
219
|
-
"properties": {
|
|
220
|
-
"title": { "type": "string" },
|
|
221
|
-
"count": { "type": "integer" }
|
|
222
|
-
},
|
|
223
|
-
"required": ["title"]
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
let result = json_schema_to_request_body(&schema_json);
|
|
227
|
-
assert!(result.is_ok());
|
|
228
|
-
|
|
229
|
-
let request_body = result.unwrap();
|
|
230
|
-
assert!(request_body.content.contains_key("application/json"));
|
|
231
|
-
assert!(matches!(request_body.required, Some(utoipa::openapi::Required::True)));
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
#[test]
|
|
235
|
-
fn test_json_schema_to_request_body_array() {
|
|
236
|
-
let schema_json = serde_json::json!({
|
|
237
|
-
"type": "array",
|
|
238
|
-
"items": {
|
|
239
|
-
"type": "object",
|
|
240
|
-
"properties": {
|
|
241
|
-
"id": { "type": "integer" }
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
let result = json_schema_to_request_body(&schema_json);
|
|
247
|
-
assert!(result.is_ok());
|
|
248
|
-
|
|
249
|
-
let request_body = result.unwrap();
|
|
250
|
-
assert!(request_body.content.contains_key("application/json"));
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
#[test]
|
|
254
|
-
fn test_json_schema_to_response() {
|
|
255
|
-
let schema_json = serde_json::json!({
|
|
256
|
-
"type": "object",
|
|
257
|
-
"properties": {
|
|
258
|
-
"id": { "type": "integer" },
|
|
259
|
-
"name": { "type": "string" }
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
let result = json_schema_to_response(&schema_json);
|
|
264
|
-
assert!(result.is_ok());
|
|
265
|
-
|
|
266
|
-
let response = result.unwrap();
|
|
267
|
-
assert!(response.content.contains_key("application/json"));
|
|
268
|
-
assert_eq!(response.description, "Successful response");
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
#[test]
|
|
272
|
-
fn test_json_schema_to_response_array() {
|
|
273
|
-
let schema_json = serde_json::json!({
|
|
274
|
-
"type": "array",
|
|
275
|
-
"items": {
|
|
276
|
-
"type": "string"
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
let result = json_schema_to_response(&schema_json);
|
|
281
|
-
assert!(result.is_ok());
|
|
282
|
-
|
|
283
|
-
let response = result.unwrap();
|
|
284
|
-
assert!(response.content.contains_key("application/json"));
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
#[test]
|
|
288
|
-
fn test_json_value_to_schema_string_with_format() {
|
|
289
|
-
let schema_json = serde_json::json!({
|
|
290
|
-
"type": "string",
|
|
291
|
-
"format": "date-time"
|
|
292
|
-
});
|
|
293
|
-
|
|
294
|
-
let result = json_value_to_schema(&schema_json);
|
|
295
|
-
assert!(result.is_ok());
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
#[test]
|
|
299
|
-
fn test_json_schema_to_request_body_empty_object() {
|
|
300
|
-
let schema_json = serde_json::json!({
|
|
301
|
-
"type": "object",
|
|
302
|
-
"properties": {}
|
|
303
|
-
});
|
|
304
|
-
|
|
305
|
-
let result = json_schema_to_request_body(&schema_json);
|
|
306
|
-
assert!(result.is_ok());
|
|
307
|
-
}
|
|
308
|
-
}
|
|
1
|
+
//! JSON Schema to OpenAPI schema conversion utilities
|
|
2
|
+
|
|
3
|
+
use utoipa::openapi::{RefOr, Schema};
|
|
4
|
+
|
|
5
|
+
/// Convert serde_json::Value (JSON Schema) to utoipa Schema
|
|
6
|
+
/// OpenAPI 3.1.0 is fully compatible with JSON Schema Draft 2020-12
|
|
7
|
+
pub fn json_value_to_schema(value: &serde_json::Value) -> Result<RefOr<Schema>, String> {
|
|
8
|
+
if let Some(type_str) = value.get("type").and_then(|t| t.as_str()) {
|
|
9
|
+
match type_str {
|
|
10
|
+
"object" => {
|
|
11
|
+
let mut object_schema = utoipa::openapi::ObjectBuilder::new();
|
|
12
|
+
|
|
13
|
+
if let Some(properties) = value.get("properties").and_then(|p| p.as_object()) {
|
|
14
|
+
for (prop_name, prop_schema) in properties {
|
|
15
|
+
let prop_openapi_schema = json_value_to_schema(prop_schema)?;
|
|
16
|
+
object_schema = object_schema.property(prop_name, prop_openapi_schema);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if let Some(required) = value.get("required").and_then(|r| r.as_array()) {
|
|
21
|
+
for field in required {
|
|
22
|
+
if let Some(field_name) = field.as_str() {
|
|
23
|
+
object_schema = object_schema.required(field_name);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
Ok(RefOr::T(Schema::Object(object_schema.build())))
|
|
29
|
+
}
|
|
30
|
+
"array" => {
|
|
31
|
+
let mut array_schema = utoipa::openapi::ArrayBuilder::new();
|
|
32
|
+
|
|
33
|
+
if let Some(items) = value.get("items") {
|
|
34
|
+
let items_schema = json_value_to_schema(items)?;
|
|
35
|
+
array_schema = array_schema.items(items_schema);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
Ok(RefOr::T(Schema::Array(array_schema.build())))
|
|
39
|
+
}
|
|
40
|
+
"string" => {
|
|
41
|
+
let mut schema_type = utoipa::openapi::schema::Type::String;
|
|
42
|
+
|
|
43
|
+
if let Some(format) = value.get("format").and_then(|f| f.as_str()) {
|
|
44
|
+
match format {
|
|
45
|
+
"date-time" => schema_type = utoipa::openapi::schema::Type::String,
|
|
46
|
+
"date" => schema_type = utoipa::openapi::schema::Type::String,
|
|
47
|
+
"email" => schema_type = utoipa::openapi::schema::Type::String,
|
|
48
|
+
"uri" => schema_type = utoipa::openapi::schema::Type::String,
|
|
49
|
+
_ => {}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
Ok(RefOr::T(Schema::Object(
|
|
54
|
+
utoipa::openapi::ObjectBuilder::new().schema_type(schema_type).build(),
|
|
55
|
+
)))
|
|
56
|
+
}
|
|
57
|
+
"integer" => Ok(RefOr::T(Schema::Object(
|
|
58
|
+
utoipa::openapi::ObjectBuilder::new()
|
|
59
|
+
.schema_type(utoipa::openapi::schema::Type::Integer)
|
|
60
|
+
.build(),
|
|
61
|
+
))),
|
|
62
|
+
"number" => Ok(RefOr::T(Schema::Object(
|
|
63
|
+
utoipa::openapi::ObjectBuilder::new()
|
|
64
|
+
.schema_type(utoipa::openapi::schema::Type::Number)
|
|
65
|
+
.build(),
|
|
66
|
+
))),
|
|
67
|
+
"boolean" => Ok(RefOr::T(Schema::Object(
|
|
68
|
+
utoipa::openapi::ObjectBuilder::new()
|
|
69
|
+
.schema_type(utoipa::openapi::schema::Type::Boolean)
|
|
70
|
+
.build(),
|
|
71
|
+
))),
|
|
72
|
+
_ => Err(format!("Unsupported schema type: {}", type_str)),
|
|
73
|
+
}
|
|
74
|
+
} else {
|
|
75
|
+
Ok(RefOr::T(Schema::Object(utoipa::openapi::ObjectBuilder::new().build())))
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/// Convert JSON Schema to OpenAPI RequestBody
|
|
80
|
+
pub fn json_schema_to_request_body(
|
|
81
|
+
schema: &serde_json::Value,
|
|
82
|
+
) -> Result<utoipa::openapi::request_body::RequestBody, String> {
|
|
83
|
+
use utoipa::openapi::content::ContentBuilder;
|
|
84
|
+
|
|
85
|
+
let openapi_schema = json_value_to_schema(schema)?;
|
|
86
|
+
|
|
87
|
+
let content = ContentBuilder::new().schema(Some(openapi_schema)).build();
|
|
88
|
+
|
|
89
|
+
let mut request_body = utoipa::openapi::request_body::RequestBody::new();
|
|
90
|
+
request_body.content.insert("application/json".to_string(), content);
|
|
91
|
+
|
|
92
|
+
request_body.required = Some(utoipa::openapi::Required::True);
|
|
93
|
+
|
|
94
|
+
Ok(request_body)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/// Convert JSON Schema to OpenAPI Response
|
|
98
|
+
pub fn json_schema_to_response(schema: &serde_json::Value) -> Result<utoipa::openapi::Response, String> {
|
|
99
|
+
use utoipa::openapi::content::ContentBuilder;
|
|
100
|
+
|
|
101
|
+
let openapi_schema = json_value_to_schema(schema)?;
|
|
102
|
+
|
|
103
|
+
let content = ContentBuilder::new().schema(Some(openapi_schema)).build();
|
|
104
|
+
|
|
105
|
+
let mut response = utoipa::openapi::Response::new("Successful response");
|
|
106
|
+
response.content.insert("application/json".to_string(), content);
|
|
107
|
+
|
|
108
|
+
Ok(response)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
#[cfg(test)]
|
|
112
|
+
mod tests {
|
|
113
|
+
use super::*;
|
|
114
|
+
|
|
115
|
+
#[test]
|
|
116
|
+
fn test_json_value_to_schema_string() {
|
|
117
|
+
let schema_json = serde_json::json!({
|
|
118
|
+
"type": "string"
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
let result = json_value_to_schema(&schema_json);
|
|
122
|
+
assert!(result.is_ok());
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
#[test]
|
|
126
|
+
fn test_json_value_to_schema_integer() {
|
|
127
|
+
let schema_json = serde_json::json!({
|
|
128
|
+
"type": "integer"
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
let result = json_value_to_schema(&schema_json);
|
|
132
|
+
assert!(result.is_ok());
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
#[test]
|
|
136
|
+
fn test_json_value_to_schema_number() {
|
|
137
|
+
let schema_json = serde_json::json!({
|
|
138
|
+
"type": "number"
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
let result = json_value_to_schema(&schema_json);
|
|
142
|
+
assert!(result.is_ok());
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
#[test]
|
|
146
|
+
fn test_json_value_to_schema_boolean() {
|
|
147
|
+
let schema_json = serde_json::json!({
|
|
148
|
+
"type": "boolean"
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
let result = json_value_to_schema(&schema_json);
|
|
152
|
+
assert!(result.is_ok());
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
#[test]
|
|
156
|
+
fn test_json_value_to_schema_object() {
|
|
157
|
+
let schema_json = serde_json::json!({
|
|
158
|
+
"type": "object",
|
|
159
|
+
"properties": {
|
|
160
|
+
"name": { "type": "string" },
|
|
161
|
+
"age": { "type": "integer" }
|
|
162
|
+
},
|
|
163
|
+
"required": ["name"]
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
let result = json_value_to_schema(&schema_json);
|
|
167
|
+
assert!(result.is_ok());
|
|
168
|
+
|
|
169
|
+
if let Ok(RefOr::T(Schema::Object(obj))) = result {
|
|
170
|
+
assert!(obj.properties.contains_key("name"));
|
|
171
|
+
assert!(obj.properties.contains_key("age"));
|
|
172
|
+
assert!(obj.required.contains(&"name".to_string()));
|
|
173
|
+
} else {
|
|
174
|
+
panic!("Expected Object schema");
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
#[test]
|
|
179
|
+
fn test_json_value_to_schema_array() {
|
|
180
|
+
let schema_json = serde_json::json!({
|
|
181
|
+
"type": "array",
|
|
182
|
+
"items": {
|
|
183
|
+
"type": "string"
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
let result = json_value_to_schema(&schema_json);
|
|
188
|
+
assert!(result.is_ok());
|
|
189
|
+
|
|
190
|
+
if let Ok(RefOr::T(Schema::Array(_))) = result {
|
|
191
|
+
} else {
|
|
192
|
+
panic!("Expected Array schema");
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
#[test]
|
|
197
|
+
fn test_json_value_to_schema_nested_object() {
|
|
198
|
+
let schema_json = serde_json::json!({
|
|
199
|
+
"type": "object",
|
|
200
|
+
"properties": {
|
|
201
|
+
"user": {
|
|
202
|
+
"type": "object",
|
|
203
|
+
"properties": {
|
|
204
|
+
"name": { "type": "string" },
|
|
205
|
+
"email": { "type": "string" }
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
let result = json_value_to_schema(&schema_json);
|
|
212
|
+
assert!(result.is_ok());
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
#[test]
|
|
216
|
+
fn test_json_schema_to_request_body() {
|
|
217
|
+
let schema_json = serde_json::json!({
|
|
218
|
+
"type": "object",
|
|
219
|
+
"properties": {
|
|
220
|
+
"title": { "type": "string" },
|
|
221
|
+
"count": { "type": "integer" }
|
|
222
|
+
},
|
|
223
|
+
"required": ["title"]
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
let result = json_schema_to_request_body(&schema_json);
|
|
227
|
+
assert!(result.is_ok());
|
|
228
|
+
|
|
229
|
+
let request_body = result.unwrap();
|
|
230
|
+
assert!(request_body.content.contains_key("application/json"));
|
|
231
|
+
assert!(matches!(request_body.required, Some(utoipa::openapi::Required::True)));
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
#[test]
|
|
235
|
+
fn test_json_schema_to_request_body_array() {
|
|
236
|
+
let schema_json = serde_json::json!({
|
|
237
|
+
"type": "array",
|
|
238
|
+
"items": {
|
|
239
|
+
"type": "object",
|
|
240
|
+
"properties": {
|
|
241
|
+
"id": { "type": "integer" }
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
let result = json_schema_to_request_body(&schema_json);
|
|
247
|
+
assert!(result.is_ok());
|
|
248
|
+
|
|
249
|
+
let request_body = result.unwrap();
|
|
250
|
+
assert!(request_body.content.contains_key("application/json"));
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
#[test]
|
|
254
|
+
fn test_json_schema_to_response() {
|
|
255
|
+
let schema_json = serde_json::json!({
|
|
256
|
+
"type": "object",
|
|
257
|
+
"properties": {
|
|
258
|
+
"id": { "type": "integer" },
|
|
259
|
+
"name": { "type": "string" }
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
let result = json_schema_to_response(&schema_json);
|
|
264
|
+
assert!(result.is_ok());
|
|
265
|
+
|
|
266
|
+
let response = result.unwrap();
|
|
267
|
+
assert!(response.content.contains_key("application/json"));
|
|
268
|
+
assert_eq!(response.description, "Successful response");
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
#[test]
|
|
272
|
+
fn test_json_schema_to_response_array() {
|
|
273
|
+
let schema_json = serde_json::json!({
|
|
274
|
+
"type": "array",
|
|
275
|
+
"items": {
|
|
276
|
+
"type": "string"
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
let result = json_schema_to_response(&schema_json);
|
|
281
|
+
assert!(result.is_ok());
|
|
282
|
+
|
|
283
|
+
let response = result.unwrap();
|
|
284
|
+
assert!(response.content.contains_key("application/json"));
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
#[test]
|
|
288
|
+
fn test_json_value_to_schema_string_with_format() {
|
|
289
|
+
let schema_json = serde_json::json!({
|
|
290
|
+
"type": "string",
|
|
291
|
+
"format": "date-time"
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
let result = json_value_to_schema(&schema_json);
|
|
295
|
+
assert!(result.is_ok());
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
#[test]
|
|
299
|
+
fn test_json_schema_to_request_body_empty_object() {
|
|
300
|
+
let schema_json = serde_json::json!({
|
|
301
|
+
"type": "object",
|
|
302
|
+
"properties": {}
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
let result = json_schema_to_request_body(&schema_json);
|
|
306
|
+
assert!(result.is_ok());
|
|
307
|
+
}
|
|
308
|
+
}
|