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
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
use crate::types::json_path::JsonPath;
use crate::types::primitive::OpenApiPrimitives;
use crate::types::Operation;
use crate::{NAME_FIELD, PARAMETERS_FIELD, PATHS_FIELD, PATH_SEPARATOR, REF_FIELD, SCHEMA_FIELD};
use dashmap::{DashMap, Entry};
use serde_json::{Map, Value};
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::fmt::{Display, Formatter};
use std::hash::Hash;
use std::sync::Arc;
type TraverseSearchResult<'a> = Result<SearchResult<'a>, TraverserError<'a>>;
type TraverseOptionalSearchResult<'a> = Result<Option<SearchResult<'a>>, TraverserError<'a>>;
type TraverseTypeResult<'a, T> = Result<&'a T, TraverserError<'a>>;
type TraverseResult<'a> = Result<(), TraverserError<'a>>;
type FindOperationResult<'a> = Result<Arc<Operation>, TraverserError<'a>>;
/// Error types that can occur during OpenAPI specification traversal.
///
/// This enum represents various error conditions that may arise when parsing,
/// validating, or navigating through an OpenAPI specification document.
#[derive(Debug, PartialEq)]
pub enum TraverserError<'a> {
/// A required field was not found in the specification.
MissingField(Cow<'a, str>),
/// The found type does not match the expected type.
TypeMismatch {
expected: Cow<'a, str>,
found: Cow<'a, str>,
},
/// The structure of the specification is invalid or contains logical errors.
InvalidStructure(Cow<'a, str>),
/// A circular reference was detected in the specification.
CyclicReference(Cow<'a, str>),
/// A specified path does not exist in the specification.
PathNotFound(Cow<'a, str>),
}
impl<'a> TraverserError<'a> {
/// Creates a new `MissingField` error.
///
/// # Parameters
/// - `message`: The field name or description that was missing
#[inline]
pub(crate) fn missing_field(message: impl Into<Cow<'a, str>>) -> Self {
Self::MissingField(message.into())
}
/// Creates a new `TypeMismatch` error.
///
/// # Parameters
/// - `expected`: The expected type description
/// - `found`: The actual type that was found
#[inline]
pub(crate) fn type_mismatch(
expected: impl Into<Cow<'a, str>>,
found: impl Into<Cow<'a, str>>,
) -> Self {
Self::TypeMismatch {
expected: expected.into(),
found: found.into(),
}
}
/// Creates a new `InvalidStructure` error.
///
/// # Parameters
/// - `message`: Description of the structural issue
#[inline]
pub(crate) fn invalid_structure(message: impl Into<Cow<'a, str>>) -> Self {
Self::InvalidStructure(message.into())
}
/// Creates a new `CyclicReference` error.
///
/// # Parameters
/// - `message`: Description of the circular reference
#[inline]
pub(crate) fn cyclic_reference(message: impl Into<Cow<'a, str>>) -> Self {
Self::CyclicReference(message.into())
}
/// Creates a new `PathNotFound` error.
///
/// # Parameters
/// - `message`: The path that could not be found
#[inline]
pub(crate) fn path_not_found(message: impl Into<Cow<'a, str>>) -> Self {
Self::PathNotFound(message.into())
}
}
impl Display for TraverserError<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
TraverserError::MissingField(field) => {
write!(f, "Missing field: {}", field)
}
TraverserError::TypeMismatch { expected, found } => {
write!(f, "Type mismatch: expected {}, found {}", expected, found)
}
TraverserError::InvalidStructure(field) => {
write!(f, "Invalid structure: {}", field)
}
TraverserError::CyclicReference(field) => {
write!(f, "Cyclic reference: {}", field)
}
TraverserError::PathNotFound(field) => {
write!(f, "Path not found: {}", field)
}
}
}
}
impl std::error::Error for TraverserError<'_> {}
/// Represents the result of a search operation within the OpenAPI specification.
///
/// This enum encapsulates different types of references that can be returned
/// from traversal operations, either as cached Arc references or direct references
#[derive(Debug)]
pub enum SearchResult<'a> {
/// A search yielding a cached reference.
Arc(Arc<Value>),
/// A search result yielding a sub-node (no reference string)
Ref(&'a Value),
}
impl<'a> SearchResult<'a> {
/// Returns a reference to the underlying JSON value.
///
/// # Returns
/// A reference to the `Value` contained within this search result.
pub fn value(&'a self) -> &'a Value {
match self {
SearchResult::Arc(arc_val) => arc_val,
SearchResult::Ref(val) => val,
}
}
}
/// Represents a segment in an OpenAPI path specification.
///
/// Path segments can be either static (literal strings) or parameterized
/// (placeholders for dynamic values with associated schemas).
#[derive(Debug, Eq, Hash, PartialEq)]
enum PathSegment {
/// A static path segment in the path, e.g., "/users"
Static(String),
/// A parameter segment in the path, e.g., "/users/{id}"
Parameter { name: String, schema: Arc<Value> },
}
/// A node in the path routing tree structure.
///
/// Each node represents a segment in the API path hierarchy and can contain
/// child nodes and associated HTTP operations.
struct PathNode {
/// Child nodes keyed by their path segments
children: HashMap<PathSegment, PathNode>,
/// HTTP operations available at this path, keyed by method name
operations: HashMap<String, Arc<Operation>>,
}
impl PathNode {
/// Creates a new empty path node.
///
/// # Returns
/// A new `PathNode` with empty children and operations collections.
fn new() -> Self {
Self {
children: HashMap::new(),
operations: HashMap::new(),
}
}
}
/// Main traverser for OpenAPI specifications.
///
/// Provides functionality to parse, validate, and navigate OpenAPI specification
/// documents with caching support for resolved references and operations.
pub struct OpenApiTraverser {
specification: Value,
resolved_references: DashMap<String, Arc<Value>>,
resolved_operations: DashMap<(String, String), Arc<Operation>>,
path_router: PathNode,
}
impl OpenApiTraverser {
/// Creates a new OpenAPI traverser from a specification document.
///
/// # Parameters
/// - `specification`: The complete OpenAPI specification as a JSON Value
///
/// # Returns
/// A new `OpenApiTraverser` instance ready for navigation operations.
///
/// # Examples
/// ```rust
/// use serde_json::json;
/// use oasert::traverser::OpenApiTraverser;
///
/// let spec = json!({
/// "openapi": "3.0.0",
/// "info": {"title": "API", "version": "1.0.0"},
/// "paths": {
/// "/users": {
/// "get": {"summary": "Get users"}
/// }
/// }
/// });
///
/// let traverser = OpenApiTraverser::new(spec).unwrap();
/// ```
///
/// # Behavior
/// This constructor automatically crawls all paths in the specification to build
/// an internal routing tree for efficient operation lookup.
pub fn new<'a>(specification: Value) -> Result<Self, TraverserError<'a>> {
let mut traverser = Self {
specification,
resolved_references: DashMap::new(),
resolved_operations: DashMap::new(),
path_router: PathNode::new(),
};
if let Err(_) = traverser.crawl_paths() {
return Err(TraverserError::invalid_structure(
"Failed to build router paths from provided specification.",
));
}
Ok(traverser)
}
/// Crawls all paths in the specification to build the internal routing tree.
///
/// # Returns
/// `Ok(())` on successful crawling, or a `TraverserError` if parsing fails.
fn crawl_paths(&mut self) -> TraverseResult {
let spec_paths = Self::get_as_object(&self.specification, PATHS_FIELD)?;
for (spec_path, spec_methods) in spec_paths {
let operations = Self::require_object(spec_methods)?;
for (spec_method, spec_operation) in operations {
let spec_path_segments = Self::split_path_segments(spec_path);
// Start at the root of the router
let mut current_node = &mut self.path_router;
// Build the path tree
for segment in spec_path_segments {
let resolved_segment = if Self::is_parameter_segment(segment) {
let operation_params =
Self::get_as_array(spec_operation, PARAMETERS_FIELD)?;
let param_name = Self::extract_parameter_name(segment);
let param_schema = operation_params.iter().find(|param| {
if let Ok(name) = Self::get_as_str(param, NAME_FIELD) {
return name == param_name;
}
false
});
let param_schema = match param_schema {
None => continue,
Some(found) => match found.get(SCHEMA_FIELD) {
None => continue,
Some(schema) => schema,
},
};
let param_schema = Arc::new(param_schema.clone());
PathSegment::Parameter {
name: param_name.to_string(),
schema: param_schema,
}
} else {
PathSegment::Static(segment.to_string())
};
current_node = current_node
.children
.entry(resolved_segment)
.or_insert_with(PathNode::new);
}
// Store the operation at this path node
let mut json_path = JsonPath::new();
json_path.add(PATHS_FIELD).add(spec_path).add(spec_method);
let operation = Arc::new(Operation {
data: spec_operation.clone(),
path: json_path,
});
current_node
.operations
.insert(spec_method.to_string(), operation);
}
}
Ok(())
}
/// Filter function for removing empty path segments.
const EMPTY_SEGMENT_FILTER: fn(&&str) -> bool = |s| !s.is_empty();
/// Splits a path string into individual segments.
///
/// # Parameters
/// - `path`: The path string to split (e.g., "/users/{id}/posts")
///
/// # Returns
/// A vector of path segments with empty segments filtered out.
fn split_path_segments(path: &str) -> Vec<&str> {
path.split(PATH_SEPARATOR)
.filter(Self::EMPTY_SEGMENT_FILTER)
.collect()
}
/// Resolves a value using caching to avoid redundant computations.
///
/// # Parameters
/// - `cache`: The cache to use for storing/retrieving values
/// - `key`: The key to cache the result under
/// - `resolver`: Function to compute the value if not cached
///
/// # Returns
/// The resolved value wrapped in Arc, either from cache or newly computed.
fn resolve_with_cache<'a, K, V, F>(
cache: &DashMap<K, Arc<V>>,
key: K,
resolver: F,
) -> Result<Arc<V>, TraverserError<'a>>
where
K: Eq + Hash,
F: FnOnce() -> Result<Arc<V>, TraverserError<'a>>,
{
let entry = cache.entry(key);
match entry {
Entry::Occupied(e) => Ok(e.get().clone()),
Entry::Vacant(e) => {
let result = resolver()?;
e.insert(result.clone());
Ok(result)
}
}
}
/// Checks if a path segment is a parameter placeholder.
///
/// # Parameters
/// - `segment`: The path segment to check
///
/// # Returns
/// `true` if the segment is surrounded by curly braces, `false` otherwise.
fn is_parameter_segment(segment: &str) -> bool {
segment.starts_with('{') && segment.ends_with('}')
}
/// Extracts the parameter name from a parameter segment.
///
/// # Parameters
/// - `segment`: The parameter segment (e.g., "{id}")
///
/// # Returns
/// The parameter name without the curly braces (e.g., "id").
fn extract_parameter_name(segment: &str) -> &str {
&segment[1..segment.len() - 1]
}
/// Returns a reference to the underlying OpenAPI specification.
///
/// # Returns
/// A reference to the complete specification JSON value.
pub fn specification(&self) -> &Value {
&self.specification
}
/// Finds and returns an operation matching the given path and HTTP method.
///
/// # Parameters
/// - `request_path`: The API path to match (e.g., "/users/123/posts")
/// - `request_method`: The HTTP method (case-insensitive, e.g., "GET", "post")
///
/// # Returns
/// An `Arc<Operation>` containing the matching operation definition.
///
/// # Examples
/// ```rust
/// use serde_json::json;
/// use oasert::traverser::OpenApiTraverser;
///
/// let spec = json!({
/// "openapi": "3.0.0",
/// "info": {"title": "API", "version": "1.0.0"},
/// "paths": {
/// "/users/{id}": {
/// "get": {
/// "summary": "Get user by ID",
/// "parameters": [
/// {
/// "name": "id",
/// "in": "path",
/// "required": true,
/// "schema": {
/// "type": "integer"
/// }
/// }
/// ]
/// }
/// }
/// }
/// });
/// let traverser = OpenApiTraverser::new(spec).unwrap();
/// let operation = traverser.get_operation_from_path_and_method("/users/123", "GET").unwrap();
/// println!("Found operation: {:?}", operation);
/// ```
///
/// # Behavior
/// This method performs path matching with parameter validation, including type
/// conversion and schema validation for path parameters. Results are cached for
/// performance. Returns `PathNotFound` error if no matching operation exists.
pub fn get_operation_from_path_and_method<'a>(
&self,
request_path: &'a str,
request_method: &str,
) -> FindOperationResult<'a> {
let binding = request_method.to_lowercase();
let request_method = binding.as_str();
let segments = Self::split_path_segments(request_path);
Self::resolve_with_cache(
&self.resolved_operations,
(String::from(request_path), String::from(request_method)),
|| {
let result =
self.find_matching_operation(&segments, 0, &self.path_router, request_method);
match result {
Some(operation) => Ok(operation),
None => Err(TraverserError::path_not_found(request_path)),
}
},
)
}
/// Recursively searches for a matching operation in the path tree.
///
/// # Parameters
/// - `segments`: Array of path segments to match
/// - `current_index`: Current position in the path segments array
/// - `current_node`: Current node in the path tree
/// - `method`: HTTP method to find
///
/// # Returns
/// `Some(Arc<Operation>)` if a match is found, `None` otherwise.
fn find_matching_operation(
&self,
segments: &[&str],
current_index: usize,
current_node: &PathNode,
method: &str,
) -> Option<Arc<Operation>> {
// If we've processed all segments, check for an operation matching the method
if current_index >= segments.len() {
current_node.operations.get(method).cloned()
} else {
let current_segment = segments[current_index];
for (segment, child) in ¤t_node.children {
match segment {
PathSegment::Static(s) if s == current_segment => {
return self.find_matching_operation(
segments,
current_index + 1,
child,
method,
);
}
PathSegment::Parameter { name, schema } => {
if let Ok(converted_value) =
OpenApiPrimitives::convert_string_to_schema_type(
schema,
current_segment,
)
{
if jsonschema::validate(schema, &converted_value).is_ok() {
return self.find_matching_operation(
segments,
current_index + 1,
child,
method,
);
}
}
}
_ => continue,
}
}
None
}
}
/// Retrieves an optional field from a JSON node, returning None if missing.
///
/// # Parameters
/// - `node`: The JSON node to search in
/// - `field`: The field name to look for
///
/// # Returns
/// `Some(SearchResult)` if the field exists, `None` if missing, or an error for other issues.
///
/// # Examples
/// ```rust
/// use serde_json::json;
/// use oasert::traverser::OpenApiTraverser;
///
/// let spec = json!({
/// "openapi": "3.0.0",
/// "info": {"title": "API", "version": "1.0.0"},
/// "paths": {
/// "/users/{id}": {
/// "get": {
/// "summary": "Get user by ID",
/// "parameters": [
/// {
/// "name": "id",
/// "in": "path",
/// "required": true,
/// "schema": {
/// "type": "integer"
/// }
/// }
/// ]
/// }
/// }
/// }
/// });
///
/// let traverser = OpenApiTraverser::new(spec).unwrap();
/// let node = &json!({"optional_field": "value"});
/// let result = traverser.get_optional(node, "optional_field").unwrap();
/// assert!(result.is_some());
///
/// let missing = traverser.get_optional(node, "missing_field").unwrap();
/// assert!(missing.is_none());
/// ```
///
/// # Behavior
/// This method resolves references automatically and distinguishes between
/// missing fields (returns None) and other errors (propagates the error).
pub fn get_optional<'node>(
&'node self,
node: &'node Value,
field: &'node str,
) -> TraverseOptionalSearchResult<'node> {
match self.get_required(node, field) {
Ok(security) => Ok(Some(security)),
Err(e) => match e {
TraverserError::MissingField(_) => Ok(None),
_ => Err(e),
},
}
}
/// Retrieves a required field from a JSON node, failing if missing.
///
/// # Parameters
/// - `node`: The JSON node to search in
/// - `field`: The field name that must exist
///
/// # Returns
/// A `SearchResult` containing the field value, or an error if missing or invalid.
///
/// # Examples
/// ```rust
/// use serde_json::json;
/// use oasert::traverser::OpenApiTraverser;
/// let spec = json!({
/// "openapi": "3.0.0",
/// "info": {"title": "API", "version": "1.0.0"},
/// "paths": {
/// "/users/{id}": {
/// "get": {
/// "summary": "Get user by ID",
/// "parameters": [
/// {
/// "name": "id",
/// "in": "path",
/// "required": true,
/// "schema": {
/// "type": "integer"
/// }
/// }
/// ]
/// }
/// }
/// }
/// });
/// let traverser = OpenApiTraverser::new(spec).unwrap();
/// let node = &json!({"required_field": "value"});
/// let result = traverser.get_required(node, "required_field").unwrap();
/// assert_eq!(result.value().as_str().unwrap(), "value");
/// ```
///
/// # Behavior
/// Automatically resolves JSON references ($ref) before field lookup. Returns
/// `MissingField` error if the field doesn't exist after reference resolution.
pub fn get_required<'node>(
&'node self,
node: &'node Value,
field: &'node str,
) -> TraverseSearchResult<'node> {
let ref_result = self.resolve_possible_ref(node)?;
match ref_result {
SearchResult::Arc(val) => match val.get(field) {
None => Err(TraverserError::missing_field(field)),
Some(v) => Ok(SearchResult::Arc(Arc::new(v.clone()))),
},
SearchResult::Ref(val) => match val.get(field) {
None => Err(TraverserError::missing_field(field)),
Some(v) => Ok(SearchResult::Ref(v)),
},
}
}
/// Resolves a JSON node that might contain a $ref reference.
///
/// # Parameters
/// - `node`: The JSON node to potentially resolve
///
/// # Returns
/// A `SearchResult` containing either the original node or the resolved reference.
fn resolve_possible_ref<'node>(&'node self, node: &'node Value) -> TraverseSearchResult<'node> {
if let Ok(ref_string) = Self::get_as_str(node, REF_FIELD) {
let result = Self::resolve_with_cache(
&self.resolved_references,
String::from(ref_string),
|| {
let mut seen_references = HashSet::new();
let result = self.get_reference_path(ref_string, &mut seen_references)?;
Ok(match result {
SearchResult::Arc(val) => val,
SearchResult::Ref(val) => Arc::new(val.clone()),
})
},
)?;
return Ok(SearchResult::Arc(result));
}
Ok(SearchResult::Ref(node))
}
/// Resolves a reference path with circular reference detection.
///
/// # Parameters
/// - `ref_string`: The reference string to resolve (e.g., "#/components/schemas/User")
/// - `seen_references`: Set of already seen references for cycle detection
///
/// # Returns
/// The resolved JSON node or an error if the reference is invalid or circular.
fn get_reference_path<'node, 'sub_node>(
&'node self,
ref_string: &'node str,
seen_references: &mut HashSet<&'node str>,
) -> TraverseSearchResult<'sub_node>
where
'node: 'sub_node,
{
if seen_references.contains(ref_string) {
return Err(TraverserError::cyclic_reference(ref_string));
}
seen_references.insert(ref_string);
let mut complete_path = String::from("/");
let path = ref_string
.split(PATH_SEPARATOR)
.filter(|node| !(*node).is_empty() && (*node != "#"))
.collect::<Vec<&str>>()
.join("/");
complete_path.push_str(&path);
let current_schema = match &self.specification.pointer(&complete_path) {
None => return Err(TraverserError::missing_field(ref_string)),
Some(v) => self.resolve_possible_ref(v)?,
};
Ok(current_schema)
}
/// Generic helper for extracting typed values from JSON nodes.
///
/// # Parameters
/// - `node`: The JSON node containing the field
/// - `field`: The field name to extract
/// - `converter`: Function to convert the raw Value to the desired type
///
/// # Returns
/// A reference to the converted type or a TraverserError.
fn get_as_type<'n, 's, T, F>(
node: &'n Value,
field: &'n str,
converter: F,
) -> TraverseTypeResult<'s, T>
where
'n: 's,
T: ?Sized,
F: Fn(&'n Value) -> TraverseTypeResult<'s, T>,
{
match node.get(field) {
None => Err(TraverserError::missing_field(field)),
Some(found) => converter(found),
}
}
/// Generic helper for requiring specific types with proper error messages.
///
/// # Parameters
/// - `node`: The JSON node to convert
/// - `converter`: Function that attempts the type conversion
/// - `type_name`: Name of the expected type for error reporting
///
/// # Returns
/// The converted value or a type mismatch error.
fn require_type<'n, 's, T, F>(
node: &'n Value,
converter: F,
type_name: &'static str,
) -> Result<T, TraverserError<'s>>
where
'n: 's,
F: Fn(&'n Value) -> Option<T>,
{
converter(node).ok_or(TraverserError::type_mismatch(
type_name,
format!("{}", node),
))
}
/// Extracts a string field from a JSON node.
///
/// # Parameters
/// - `node`: The JSON node containing the field
/// - `field`: The field name to extract as a string
///
/// # Returns
/// A reference to the string value or a TraverserError.
pub(crate) fn get_as_str<'n, 's>(node: &'n Value, field: &'n str) -> TraverseTypeResult<'s, str>
where
'n: 's,
{
Self::get_as_type(node, field, Self::require_str)
}
/// Extracts an object field from a JSON node.
///
/// # Parameters
/// - `node`: The JSON node containing the field
/// - `field`: The field name to extract as an object
///
/// # Returns
/// A reference to the object (Map) or a TraverserError.
pub(crate) fn get_as_object<'n, 's>(
node: &'n Value,
field: &'n str,
) -> TraverseTypeResult<'s, Map<String, Value>>
where
'n: 's,
{
Self::get_as_type(node, field, Self::require_object)
}
/// Extracts an array field from a JSON node.
///
/// # Parameters
/// - `node`: The JSON node containing the field
/// - `field`: The field name to extract as an array
///
/// # Returns
/// A reference to the array (Vec) or a TraverserError.
pub(crate) fn get_as_array<'n, 's>(
node: &'n Value,
field: &'n str,
) -> TraverseTypeResult<'s, Vec<Value>>
where
'n: 's,
{
Self::get_as_type(node, field, Self::require_array)
}
/// Requires a JSON value to be a boolean.
///
/// # Parameters
/// - `node`: The JSON value to convert
///
/// # Returns
/// The boolean value or a type mismatch error.
pub(crate) fn require_bool<'n, 's>(node: &'n Value) -> Result<bool, TraverserError<'s>>
where
'n: 's,
{
Self::require_type(node, Value::as_bool, "bool")
}
/// Requires a JSON value to be a string.
///
/// # Parameters
/// - `node`: The JSON value to convert
///
/// # Returns
/// A reference to the string or a type mismatch error.
pub(crate) fn require_str<'n, 's>(node: &'n Value) -> TraverseTypeResult<'s, str>
where
'n: 's,
{
Self::require_type(node, Value::as_str, "string")
}
/// Requires a JSON value to be an object.
///
/// # Parameters
/// - `node`: The JSON value to convert
///
/// # Returns
/// A reference to the object map or a type mismatch error.
pub(crate) fn require_object<'n, 's>(
node: &'n Value,
) -> TraverseTypeResult<'s, Map<String, Value>>
where
'n: 's,
{
Self::require_type(node, Value::as_object, "object")
}
/// Requires a JSON value to be an array.
///
/// # Parameters
/// - `node`: The JSON value to convert
///
/// # Returns
/// A reference to the array vector or a type mismatch error.
pub(crate) fn require_array<'n, 's>(node: &'n Value) -> TraverseTypeResult<'s, Vec<Value>>
where
'n: 's,
{
Self::require_type(node, Value::as_array, "array")
}
}
#[cfg(test)]
mod tests {
use crate::traverser::{OpenApiTraverser, TraverserError};
use serde_json::json;
use std::sync::Arc;
#[test]
fn test_get_operation_from_path_and_method_with_valid_path_and_method() {
let spec = json!({
"paths": {
"/pets": {
"get": {
"operationId": "getPets",
"responses": {
"200": {
"description": "successful operation"
}
}
}
}
}
});
let traverser = OpenApiTraverser::new(spec).unwrap();
let result = traverser.get_operation_from_path_and_method("/pets", "GET");
assert!(result.is_ok());
let operation = result.unwrap();
assert_eq!(operation.data["operationId"], "getPets");
}
#[test]
fn test_get_operation_from_path_and_method_with_invalid_path() {
let spec = json!({
"paths": {
"/pets": {
"get": {
"operationId": "getPets",
"responses": {
"200": {
"description": "successful operation"
}
}
}
}
}
});
let traverser = OpenApiTraverser::new(spec).unwrap();
let result = traverser.get_operation_from_path_and_method("/invalid_path", "GET");
assert!(result.is_err());
}
#[test]
fn test_get_operation_from_path_and_method_with_invalid_method() {
let spec = json!({
"paths": {
"/pets": {
"get": {
"operationId": "getPets",
"responses": {
"200": {
"description": "successful operation"
}
}
}
}
}
});
let traverser = OpenApiTraverser::new(spec).unwrap();
let result = traverser.get_operation_from_path_and_method("/pets", "POST");
assert!(result.is_err());
}
#[test]
fn test_get_operation_from_path_and_method_with_cached_result() {
let spec = json!({
"paths": {
"/pets": {
"get": {
"operationId": "getPets",
"responses": {
"200": {
"description": "successful operation"
}
}
}
}
}
});
let traverser = OpenApiTraverser::new(spec).unwrap();
// Call the function the first time (caches the result)
let first_result = traverser.get_operation_from_path_and_method("/pets", "GET");
assert!(first_result.is_ok());
let second_result = traverser.get_operation_from_path_and_method("/pets", "GET");
assert!(second_result.is_ok());
let first_result = first_result.unwrap();
let second_result = second_result.unwrap();
assert!(Arc::ptr_eq(&first_result, &second_result));
}
#[test]
fn test_get_operation_from_path_and_method_with_parametrized_path() {
let spec = json!({
"paths": {
"/pets/{id}": {
"get": {
"operationId": "getPetById",
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"schema": { "type": "string" }
}
],
"responses": {
"200": {
"description": "successful operation"
}
}
}
}
}
});
let traverser = OpenApiTraverser::new(spec).unwrap();
let result = traverser.get_operation_from_path_and_method("/pets/123", "GET");
assert!(result.is_ok());
let operation = result.unwrap();
assert_eq!(operation.data["operationId"], "getPetById");
}
#[test]
fn test_get_operation_from_path_and_method_with_non_matching_param() {
let spec = json!({
"paths": {
"/pets/{id}": {
"get": {
"operationId": "getPetById",
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"schema": { "type": "integer" }
}
],
"responses": {
"200": {
"description": "successful operation"
}
}
}
}
}
});
let traverser = OpenApiTraverser::new(spec).unwrap();
let result = traverser.get_operation_from_path_and_method("/pets/id_as_string", "GET");
assert!(result.is_err());
}
#[test]
fn test_require_object_with_valid_object() {
let object_value = json!({"name": "test", "age": 30});
let result = OpenApiTraverser::require_object(&object_value);
assert!(result.is_ok());
let obj = result.unwrap();
assert_eq!(obj.len(), 2);
assert_eq!(obj.get("name").unwrap(), &json!("test"));
assert_eq!(obj.get("age").unwrap(), &json!(30));
}
#[test]
fn test_require_object_with_non_object_type() {
let string_value = json!("not an object");
let result = OpenApiTraverser::require_object(&string_value);
assert!(result.is_err());
match result {
Err(TraverserError::TypeMismatch { expected, .. }) => {
assert_eq!(expected, "object");
}
_ => panic!("Expected UnexpectedType error"),
}
}
#[test]
fn test_require_array_with_valid_array() {
let array_value = json!([1, 2, 3, "test"]);
let result = OpenApiTraverser::require_array(&array_value);
assert!(result.is_ok());
let array = result.unwrap();
assert_eq!(array.len(), 4);
assert_eq!(array[0], json!(1));
assert_eq!(array[1], json!(2));
assert_eq!(array[2], json!(3));
assert_eq!(array[3], json!("test"));
}
#[test]
fn test_require_array_with_non_array_type() {
let object_value = json!({"key": "value"});
let result = OpenApiTraverser::require_array(&object_value);
assert!(result.is_err());
match result {
Err(TraverserError::TypeMismatch { expected, .. }) => {
assert_eq!(expected, "array");
}
_ => panic!("Expected UnexpectedType error"),
}
}
}