1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
//! OpenAPI specification parsing and utilities.
//!
//! This module provides functionality for loading and querying OpenAPI specifications.
//! It supports loading from files and provides convenient accessors for common fields.
//!
//! # Examples
//!
//! ```no_run
//! use agenterra_core::openapi::OpenApiContext;
//! use agenterra_core::error::Result;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<()> {
//! // Load an OpenAPI spec from a file
//! let spec = OpenApiContext::from_file("openapi.json").await?;
//!
//! // Access common fields
//! if let Some(title) = spec.title() {
//! println!("API Title: {}", title);
//! }
//! if let Some(version) = spec.version() {
//! println!("API Version: {}", version);
//! }
//! # Ok(())
//! # }
//! ```
// Internal imports (std, crate)
use std::path::Path;
use crate::core::Error;
// External imports (alphabetized)
use serde::{Deserialize, Serialize};
use serde_json::{Value as JsonValue, json};
use tokio::fs;
/// Represents an OpenAPI specification
#[derive(Debug, serde::Serialize)]
#[serde(transparent)]
pub struct OpenApiContext {
/// The raw JSON value of the OpenAPI spec
pub json: JsonValue,
}
impl OpenApiContext {
/// Create a new OpenAPISpec from a file or URL (supports both YAML and JSON)
pub async fn from_file_or_url<P: AsRef<str>>(location: P) -> crate::core::error::Result<Self> {
let location = location.as_ref();
// Check if the input looks like a URL
if location.starts_with("http://") || location.starts_with("https://") {
return Self::from_url(location).await;
}
// Otherwise treat as a file path
Self::from_file(location).await
}
/// Create a new OpenAPISpec from a file (supports both YAML and JSON)
pub async fn from_file<P: AsRef<Path>>(path: P) -> crate::core::error::Result<Self> {
let path = path.as_ref();
let content = fs::read_to_string(path).await?;
Self::parse_content(&content).map_err(|e| {
crate::core::Error::openapi(format!(
"Failed to parse OpenAPI spec at {}: {}",
path.display(),
e
))
})
}
/// Create a new OpenAPISpec from a URL (supports both YAML and JSON)
async fn from_url(url: &str) -> crate::core::error::Result<Self> {
let response = reqwest::get(url).await.map_err(|e| {
crate::core::Error::openapi(format!("Failed to fetch OpenAPI spec from {}: {}", url, e))
})?;
if !response.status().is_success() {
return Err(crate::core::Error::openapi(format!(
"Failed to fetch OpenAPI spec from {}: HTTP {}",
url,
response.status()
)));
}
let content = response.text().await.map_err(|e| {
crate::core::Error::openapi(format!("Failed to read response from {}: {}", url, e))
})?;
Self::parse_content(&content).map_err(|e| {
crate::core::Error::openapi(format!("Failed to parse OpenAPI spec from {}: {}", url, e))
})
}
/// Parse content as either JSON or YAML
fn parse_content(content: &str) -> Result<Self, String> {
// Try to parse as JSON first
if let Ok(json) = serde_json::from_str(content) {
return Ok(Self { json });
}
// If JSON parsing fails, try YAML
if let Ok(json) = serde_yaml::from_str(content) {
return Ok(Self { json });
}
// If both parsers fail, return an error
Err("content is neither valid JSON nor YAML".to_string())
}
/// Get the title of the API
pub fn title(&self) -> Option<&str> {
self.json.get("info")?.get("title")?.as_str()
}
/// Get the version of the API
pub fn version(&self) -> Option<&str> {
self.json.get("info")?.get("version")?.as_str()
}
/// Get the base path of the API
pub fn base_path(&self) -> Option<String> {
// Try OpenAPI 3.0+ servers format first
if let Some(servers) = self.json.get("servers").and_then(|s| s.as_array()) {
if let Some(server) = servers.first() {
if let Some(url) = server.get("url").and_then(|u| u.as_str()) {
return Some(url.to_string());
}
}
}
// Fall back to Swagger 2.0 host + basePath format
if let (Some(host), base_path) = (
self.json.get("host").and_then(|h| h.as_str()),
self.json
.get("basePath")
.and_then(|bp| bp.as_str())
.unwrap_or(""),
) {
// Determine protocol - Swagger 2.0 specs can specify schemes
let scheme = if let Some(schemes) = self.json.get("schemes").and_then(|s| s.as_array())
{
// Use the first scheme, prefer https if available
if schemes.iter().any(|s| s.as_str() == Some("https")) {
"https"
} else {
schemes.first().and_then(|s| s.as_str()).unwrap_or("https")
}
} else {
"https" // Default to https if no schemes specified
};
return Some(format!("{}://{}{}", scheme, host, base_path));
}
None
}
/// Parse all endpoints into structured contexts for template rendering
pub async fn parse_operations(&self) -> crate::core::error::Result<Vec<OpenApiOperation>> {
let mut operations = Vec::new();
// Expect 'paths' object
let paths = self
.json
.get("paths")
.and_then(JsonValue::as_object)
.ok_or_else(|| Error::openapi("Missing 'paths' object"))?;
for (path, item) in paths {
// Handle both GET and POST operations
for method in ["get", "post"] {
if let Some(method_item) = item.get(method).and_then(JsonValue::as_object) {
let operation_id = method_item
.get("operationId")
.and_then(JsonValue::as_str)
.map(String::from)
.unwrap_or_else(|| {
format!(
"{}_{}",
method,
path.trim_start_matches('/').replace('/', "_")
)
});
let summary = method_item
.get("summary")
.and_then(JsonValue::as_str)
.map(String::from);
let description = method_item
.get("description")
.and_then(JsonValue::as_str)
.map(String::from);
let external_docs = method_item.get("externalDocs").cloned();
let parameters = self.extract_parameters(item);
let request_body = method_item.get("requestBody").cloned();
let responses = self.extract_responses(method_item);
let callbacks = method_item.get("callbacks").cloned();
let deprecated = method_item.get("deprecated").and_then(JsonValue::as_bool);
let security = method_item
.get("security")
.and_then(JsonValue::as_array)
.cloned();
let servers = method_item
.get("servers")
.and_then(JsonValue::as_array)
.cloned();
let tags = method_item
.get("tags")
.and_then(JsonValue::as_array)
.map(|arr| {
arr.iter()
.filter_map(JsonValue::as_str)
.map(String::from)
.collect()
});
let vendor_extensions = self.extract_vendor_extensions(method_item);
operations.push(OpenApiOperation {
id: operation_id,
path: path.clone(),
summary,
description,
external_docs,
parameters,
request_body,
responses,
callbacks,
deprecated,
security,
servers,
tags,
vendor_extensions,
});
}
}
}
Ok(operations)
}
/// Extracts parameters from an OpenAPI path item, resolving any $ref references
fn extract_parameters(&self, path_item: &JsonValue) -> Option<Vec<OpenApiParameter>> {
path_item
.get("parameters")
.and_then(JsonValue::as_array)
.map(|arr| {
arr.iter()
.filter_map(|param| {
if let Some(ref_str) = param.get("$ref").and_then(JsonValue::as_str) {
self.json
.pointer(&ref_str[1..])
.and_then(|p| serde_json::from_value(p.clone()).ok())
} else {
serde_json::from_value(param.clone()).ok()
}
})
.collect::<Vec<OpenApiParameter>>()
})
}
/// Extracts response definitions from an OpenAPI operation
fn extract_responses(
&self,
get_item: &serde_json::Map<String, JsonValue>,
) -> std::collections::HashMap<String, OpenApiResponse> {
get_item
.get("responses")
.and_then(JsonValue::as_object)
.map(|map| {
map.iter()
.filter_map(|(k, v)| {
serde_json::from_value(v.clone())
.ok()
.map(|resp| (k.clone(), resp))
})
.collect()
})
.unwrap_or_default()
}
/// Extracts vendor extensions (x-* prefixed properties) from an OpenAPI operation
fn extract_vendor_extensions(
&self,
get_item: &serde_json::Map<String, JsonValue>,
) -> std::collections::HashMap<String, JsonValue> {
get_item
.iter()
.filter(|(k, _)| k.starts_with("x-"))
.map(|(k, v)| (k.clone(), v.clone()))
.collect()
}
/// Extract row properties from properties JSON
pub fn extract_row_properties(properties_json: &JsonValue) -> Vec<JsonValue> {
if let Some(data) = properties_json.get("data").and_then(JsonValue::as_object) {
if let Some(props) = data.get("properties").and_then(JsonValue::as_object) {
return props
.iter()
.map(|(k, v)| json!({"name": k, "schema": v}))
.collect();
}
}
if let Some(props) = properties_json.as_object() {
return props
.iter()
.map(|(k, v)| json!({"name": k, "schema": v}))
.collect();
}
Vec::new()
}
/// Extract typed property info from properties JSON
pub fn extract_property_info(properties_json: &JsonValue) -> Vec<OpenApiPropertyInfo> {
OpenApiContext::extract_row_properties(properties_json)
.into_iter()
.map(|prop| {
let name = prop
.get("name")
.and_then(JsonValue::as_str)
.unwrap_or_default()
.to_string();
let schema = prop.get("schema");
let title = schema
.and_then(|s| s.get("title"))
.and_then(JsonValue::as_str)
.map(String::from);
let description = schema
.and_then(|s| s.get("description"))
.and_then(JsonValue::as_str)
.map(String::from);
let example = schema.and_then(|s| s.get("example")).cloned();
OpenApiPropertyInfo {
name: name.clone(),
title,
description,
example,
}
})
.collect()
}
/// Sanitize a string to be safe for use as a filename across all operating systems
#[allow(dead_code)]
fn sanitize_filename(name: &str) -> String {
name.chars()
.map(|c| {
if c.is_ascii_alphanumeric() || c == '_' {
c
} else {
'_'
}
})
.collect()
}
/// Sanitize endpoint names to be valid Rust module identifiers
#[allow(dead_code)]
fn sanitize_endpoint_name(endpoint: &str) -> String {
// Replace any characters that aren't valid in Rust identifiers
let mut result = String::new();
// First, replace common problematic patterns
let s = endpoint.replace("{", "").replace("}", "");
let s = s.replace("/", "_").replace("-", "_");
// Then ensure we only have valid Rust identifier characters
for c in s.chars() {
if c.is_alphanumeric() || c == '_' {
result.push(c);
} else {
result.push('_');
}
}
// Ensure it starts with a letter or underscore (valid Rust identifier)
if !result.is_empty()
&& !result.chars().next().unwrap().is_alphabetic()
&& !result.starts_with('_')
{
result = format!("m_{}", result);
}
// Handle empty string case
if result.is_empty() {
result = "root".to_string();
}
result
}
/// Sanitizes Markdown for Rust doc comments and Swagger UI.
#[allow(dead_code)]
pub fn sanitize_markdown(input: &str) -> String {
use regex::Regex;
// Regex for problematic Unicode (e.g., smart quotes, em-dash)
let unicode_re = Regex::new(r"[\u2018\u2019\u201C\u201D\u2014]").unwrap();
// Regex to collapse any whitespace sequence into a single space
let ws_re = Regex::new(r"\s+").unwrap();
input
.lines()
.map(|line| {
let mut line = line.replace('\t', " ");
// Remove problematic Unicode
line = unicode_re
.replace_all(&line, |caps: ®ex::Captures| match &caps[0] {
"\u{2018}" | "\u{2019}" => "'",
"\u{201C}" | "\u{201D}" => "\"",
"\u{2014}" => "-",
_ => "",
})
.to_string();
// Trim edges and collapse inner whitespace
let mut trimmed = ws_re.replace_all(line.trim(), " ").to_string();
// Remove spaces around hyphens
trimmed = trimmed
.replace(" - ", "-")
.replace("- ", "-")
.replace(" -", "-");
// Escape backslashes and quotes
let mut safe = trimmed.replace('\\', "\\\\").replace('"', "\\\"");
// Escape braces and brackets
safe = safe
.replace("{", "{")
.replace("}", "}")
.replace("[", "[")
.replace("]", "]");
safe
})
.filter(|l| !l.is_empty())
.collect::<Vec<_>>()
.join(" ")
}
/// Extract properties from a schema, resolving $ref if necessary
fn extract_schema_properties(
&self,
schema: &JsonValue,
) -> crate::core::error::Result<(JsonValue, Option<String>)> {
// Handle null or non-object schemas
let schema_obj = match schema.as_object() {
Some(obj) => obj,
None => return Ok((JsonValue::Null, None)),
};
// Direct inline object schema with properties
if schema_obj.get("properties").is_some()
|| schema_obj.get("additionalProperties").is_some()
{
let props = schema_obj
.get("properties")
.cloned()
.unwrap_or(JsonValue::Null);
return Ok((props, None));
}
// Primitive types: return no properties
if let Some(typ) = schema_obj.get("type").and_then(JsonValue::as_str) {
if typ != "object" && typ != "array" {
return Ok((JsonValue::Null, None));
}
}
// Handle $ref
let ref_str = match schema_obj.get("$ref").and_then(JsonValue::as_str) {
Some(r) => r,
None => {
// Check for array items ref
if let Some(items) = schema_obj.get("items").and_then(JsonValue::as_object) {
if let Some(r) = items.get("$ref").and_then(JsonValue::as_str) {
r
} else {
return Ok((JsonValue::Null, None));
}
} else {
return Ok((JsonValue::Null, None));
}
}
};
// Resolve the reference
let key = "#/components/schemas/";
if !ref_str.starts_with(key) {
return Err(Error::openapi(format!(
"Unexpected schema ref '{}'",
ref_str
)));
}
let schema_name = &ref_str[key.len()..];
let schemas = self
.json
.get("components")
.and_then(JsonValue::as_object)
.and_then(|m| m.get("schemas"))
.and_then(JsonValue::as_object)
.ok_or_else(|| Error::openapi("No components.schemas section"))?;
let def = schemas
.get(schema_name)
.ok_or_else(|| Error::openapi(format!("Schema '{}' not found", schema_name)))?;
let props = def.get("properties").cloned().unwrap_or(JsonValue::Null);
Ok((props, Some(schema_name.to_string())))
}
/// Extract request body properties from an operation
///
/// Returns a tuple of (properties_json, schema_name) where:
/// - properties_json: The schema properties as a JSON object
/// - schema_name: The name of the schema if it could be determined
pub fn extract_request_body_properties(
&self,
operation: &OpenApiOperation,
) -> crate::core::error::Result<(JsonValue, Option<String>)> {
// If there's no request body, return empty properties
let Some(request_body) = &operation.request_body else {
return Ok((serde_json::json!({}), None));
};
// Extract content from request body
let content = request_body
.get("content")
.and_then(JsonValue::as_object)
.ok_or_else(|| Error::openapi("Request body has no content"))?;
// Look for application/json content
let json_content = content
.get("application/json")
.and_then(JsonValue::as_object)
.ok_or_else(|| Error::openapi("Request body has no application/json content"))?;
// Extract schema
let schema = json_content
.get("schema")
.ok_or_else(|| Error::openapi("Request body content has no schema"))?;
// Use the generic schema extraction method
self.extract_schema_properties(schema)
}
}
/// Parsed OpenAPI operation for template rendering
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OpenApiOperation {
/// Unique string used to identify the operation. The id MUST be unique among all operations described in the API.
#[serde(rename = "operationId")]
pub id: String,
/// The path where this operation is defined (e.g., "/pet/findByStatus")
pub path: String,
/// A list of tags for API documentation control. Tags can be used for logical grouping of operations.
#[serde(rename = "tags")]
pub tags: Option<Vec<String>>,
/// A short summary of what the operation does.
pub summary: Option<String>,
/// A verbose explanation of the operation behavior. CommonMark syntax MAY be used for rich text representation.
pub description: Option<String>,
/// Additional external documentation for this operation.
#[serde(rename = "externalDocs")]
pub external_docs: Option<serde_json::Value>,
/// A list of parameters that are applicable for this operation. If a parameter is already defined at the Path Item, the new definition will override it but can never remove it.
pub parameters: Option<Vec<OpenApiParameter>>,
/// The request body applicable for this operation.
#[serde(rename = "requestBody")]
pub request_body: Option<serde_json::Value>,
/// The list of possible responses as they are returned from executing this operation.
pub responses: std::collections::HashMap<String, OpenApiResponse>,
/// A map of possible out-of band callbacks related to the parent operation.
pub callbacks: Option<serde_json::Value>,
/// Declares this operation to be deprecated. Consumers SHOULD refrain from usage of the declared operation.
pub deprecated: Option<bool>,
/// A declaration of which security mechanisms can be used for this operation.
pub security: Option<Vec<serde_json::Value>>,
/// An alternative server array to service this operation.
pub servers: Option<Vec<serde_json::Value>>,
/// Specification extensions (fields starting with `x-`).
#[serde(flatten)]
pub vendor_extensions: std::collections::HashMap<String, serde_json::Value>,
}
/// Info about a single OpenAPI parameter
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OpenApiParameterInfo {
/// Name of the parameter as defined in the OpenAPI spec
pub name: String,
/// Optional description of the parameter
pub description: Option<String>,
/// Optional example value for the parameter
pub example: Option<JsonValue>,
// Note: Language-specific type info (e.g., rust_type) is injected by the builder, not stored here.
}
/// Info about a single response property
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OpenApiPropertyInfo {
/// Name of the property as defined in the OpenAPI schema
pub name: String,
/// Optional title metadata for the property
pub title: Option<String>,
/// Optional description of the property
pub description: Option<String>,
/// Optional example value for the property
pub example: Option<JsonValue>,
}
/// Information about a single parameter in an OpenAPI operation.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct OpenApiParameter {
/// The name of the parameter. Parameter names are case sensitive.
pub name: String,
/// The location of the parameter. Possible values: "query", "header", "path", or "cookie".
#[serde(rename = "in")]
pub in_: String,
/// A brief description of the parameter. This could contain examples of use. CommonMark syntax MAY be used for rich text representation.
pub description: Option<String>,
/// Determines whether this parameter is mandatory. If the parameter location is "path", this property is REQUIRED and its value MUST be true. Otherwise, the property MAY be included and its default value is false.
pub required: Option<bool>,
/// Specifies that a parameter is deprecated and SHOULD be transitioned out of usage.
pub deprecated: Option<bool>,
/// Sets the ability to pass empty-valued parameters. This is valid only for query parameters and allows sending a parameter with an empty value. Default value is false.
#[serde(rename = "allowEmptyValue")]
pub allow_empty_value: Option<bool>,
/// Describes how the parameter value will be serialized depending on the type of the parameter value. Default values (based on value of in): for query - form; for path - simple; for header - simple; for cookie - form.
pub style: Option<String>,
/// When this is true, parameter values of type array or object generate separate parameters for each value of the array or key-value pair of the map. Default value is false.
pub explode: Option<bool>,
/// Determines whether the parameter value SHOULD allow reserved characters, as defined by RFC3986, to appear unescaped in the parameter value. Default value is false.
#[serde(rename = "allowReserved")]
pub allow_reserved: Option<bool>,
/// The schema defining the type used for the parameter.
pub schema: Option<serde_json::Value>,
/// Example of the parameter's potential value. The example SHOULD match the specified schema and encoding properties if present.
pub example: Option<serde_json::Value>,
/// Examples of the parameter's potential value. Each example SHOULD contain a value in the correct format as specified in the parameter encoding.
pub examples: Option<std::collections::HashMap<String, serde_json::Value>>,
/// A map containing the representations for the parameter. The key is the media type and the value describes it.
pub content: Option<std::collections::HashMap<String, serde_json::Value>>,
/// Specification extensions (fields starting with `x-`).
#[serde(flatten)]
pub vendor_extensions: std::collections::HashMap<String, serde_json::Value>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct OpenApiResponse {
/// A short description of the response. CommonMark syntax MAY be used for rich text representation.
pub description: Option<String>,
/// Maps a header name to its definition. The key is the name of the header, and the value describes it.
pub headers: Option<std::collections::HashMap<String, serde_json::Value>>,
/// A map containing descriptions of potential response payloads. The key is a media type, and the value describes it.
pub content: Option<std::collections::HashMap<String, serde_json::Value>>,
/// A map of operations links that can be followed from the response. The key is the link name, the value describes the link.
pub links: Option<std::collections::HashMap<String, serde_json::Value>>,
/// Specification extensions (fields starting with `x-`).
#[serde(flatten)]
pub vendor_extensions: std::collections::HashMap<String, serde_json::Value>,
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use tempfile::tempdir;
impl OpenApiContext {
/// Extract operation metadata for testing
fn extract_operation_metadata(path_item: &JsonValue) -> (String, String, Vec<String>) {
let get_item = path_item.get("get").and_then(JsonValue::as_object);
let summary = get_item
.and_then(|g| g.get("summary"))
.and_then(JsonValue::as_str)
.unwrap_or("")
.trim()
.replace(['\n', '\t'], " ")
.split_whitespace()
.collect::<Vec<_>>()
.join(" ");
let description = get_item
.and_then(|g| g.get("description"))
.and_then(JsonValue::as_str)
.unwrap_or("")
.trim()
.replace(['\n', '\t'], " ")
.split_whitespace()
.collect::<Vec<_>>()
.join(" ");
let tags = get_item
.and_then(|g| g.get("tags"))
.and_then(JsonValue::as_array)
.map(|arr| {
arr.iter()
.filter_map(JsonValue::as_str)
.map(String::from)
.collect()
})
.unwrap_or_default();
(summary, description, tags)
}
/// Extract parameters for handler testing
fn extract_parameters_for_handler(&self, path_item: &JsonValue) -> Vec<JsonValue> {
let get_item = path_item.get("get").and_then(JsonValue::as_object);
let mut params = get_item
.and_then(|g| g.get("parameters"))
.and_then(JsonValue::as_array)
.cloned()
.unwrap_or_default();
// Sort parameters: path params first, then others
params.sort_by_key(|p| match p.get("in").and_then(JsonValue::as_str) {
Some("path") => 0,
_ => 1,
});
params
}
/// Build response schema for testing
fn build_response_schema(schema_name: &str) -> JsonValue {
json!({
"$ref": format!("#/components/schemas/{}", schema_name)
})
}
}
#[tokio::test]
async fn test_from_file() -> crate::core::error::Result<()> {
let dir = tempdir()?;
let file_path = dir.path().join("openapi_async.json");
let json_content = r#"
{
"openapi": "3.0.0",
"info": {
"title": "Test API Async",
"version": "2.0.0"
},
"servers": [
{
"url": "https://api.example.com/v2"
}
]
}
"#;
tokio::fs::write(&file_path, json_content).await?;
let spec = OpenApiContext::from_file(&file_path).await?;
assert_eq!(spec.title(), Some("Test API Async"));
assert_eq!(spec.version(), Some("2.0.0"));
assert_eq!(
spec.base_path(),
Some("https://api.example.com/v2".to_string())
);
Ok(())
}
#[test]
fn test_extract_operation_metadata() {
let path_item =
json!({"get": {"summary": "sum", "description": "desc", "tags": ["a","b"]}});
let (sum, desc, tags) = OpenApiContext::extract_operation_metadata(&path_item);
assert_eq!(sum, "sum");
assert_eq!(desc, "desc");
assert_eq!(tags, vec!["a".to_string(), "b".to_string()]);
}
#[test]
fn test_extract_parameters_for_handler() {
let spec = OpenApiContext { json: json!({}) };
let path_item = json!({"get": {"parameters": [{"name": "p", "in": "query"}]}});
let params = spec.extract_parameters_for_handler(&path_item);
assert_eq!(params, vec![json!({"name": "p", "in": "query"})]);
}
#[test]
fn test_build_response_schema() {
let schema = OpenApiContext::build_response_schema("X");
assert_eq!(schema, json!({"$ref": "#/components/schemas/X"}));
}
#[test]
fn test_extract_row_properties() {
let props = json!({"data": {"properties": {"k": 1, "m": 2}}});
let rows = OpenApiContext::extract_row_properties(&props);
let names: Vec<_> = rows
.iter()
.filter_map(|r| r.get("name").and_then(JsonValue::as_str))
.collect();
assert_eq!(names, vec!["k", "m"]);
}
#[test]
fn test_extract_row_properties_direct() {
let props = json!({"x": {"type": "string"}, "y": {"type": "integer"}});
let rows = OpenApiContext::extract_row_properties(&props);
let mut names: Vec<_> = rows
.iter()
.filter_map(|r| r.get("name").and_then(JsonValue::as_str))
.collect();
names.sort();
assert_eq!(names, vec!["x", "y"]);
}
#[test]
fn test_sanitize_markdown_basic() {
let raw = "Line one\n\nLine two";
assert_eq!(OpenApiContext::sanitize_markdown(raw), "Line one Line two");
}
#[test]
fn test_sanitize_markdown_escape_and_unicode() {
let raw = "\"hi\" {x} “quote” — dash";
let out = OpenApiContext::sanitize_markdown(raw);
// Check escapes
assert!(out.contains("\\\"hi\\\""));
assert!(out.contains("{x}"));
// Unicode replaced
assert!(!out.contains("“"));
assert!(!out.contains("—"));
}
#[test]
fn test_extract_operation_metadata_trims_and_sanitizes() {
let path_item = json!({"get": {"summary": " sum \n next", "description": " desc-\tline", "tags": ["t"]}});
let (s, d, tags) = OpenApiContext::extract_operation_metadata(&path_item);
assert_eq!(s, "sum next");
assert_eq!(d, "desc- line");
assert_eq!(tags, vec!["t".to_string()]);
}
#[test]
fn test_extract_parameters_ordering() {
let spec = OpenApiContext { json: json!({}) };
let path_item = json!({"get": {"parameters": [
{"name": "q", "in": "query"},
{"name": "p", "in": "path"}
]}});
let names: Vec<String> = spec
.extract_parameters_for_handler(&path_item)
.into_iter()
.filter_map(|p| p.get("name").and_then(JsonValue::as_str).map(String::from))
.collect();
assert_eq!(names, vec!["p".to_string(), "q".to_string()]);
}
}