schemadoc_diff/checker/
removed_operation_check.rs

1use std::cell::RefCell;
2
3use crate::core::DiffResult;
4use crate::path_pointer::PathPointer;
5
6use crate::checker::{ValidationIssue, ValidationIssuer};
7use crate::schema_diff::OperationDiff;
8use crate::visitor::DiffVisitor;
9
10pub struct RemovedOperationCheck {
11    pointers: RefCell<Vec<PathPointer>>,
12}
13
14impl<'s> DiffVisitor<'s> for RemovedOperationCheck {
15    fn visit_operation(
16        &self,
17        pointer: &PathPointer,
18        _method: &str,
19        operation_diff_result: &'s DiffResult<OperationDiff>,
20    ) -> bool {
21        if let DiffResult::Removed(_) = operation_diff_result {
22            self.pointers.borrow_mut().push(pointer.clone())
23        }
24        false
25    }
26}
27
28impl Default for RemovedOperationCheck {
29    fn default() -> Self {
30        Self {
31            pointers: RefCell::new(vec![]),
32        }
33    }
34}
35
36impl<'s> ValidationIssuer<'s> for RemovedOperationCheck {
37    fn id(&self) -> &'static str {
38        "removed-operation"
39    }
40
41    fn visitor(&self) -> &dyn DiffVisitor<'s> {
42        self
43    }
44
45    fn issues(&self) -> Option<Vec<ValidationIssue>> {
46        let pointers = std::mem::take(&mut *self.pointers.borrow_mut());
47
48        let issues = pointers
49            .into_iter()
50            .map(|path| ValidationIssue::new(path, self.id(), true))
51            .collect::<Vec<ValidationIssue>>();
52
53        Some(issues)
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use crate::checker::removed_operation_check::RemovedOperationCheck;
60    use crate::checker::ValidationIssuer;
61    use crate::get_schema_diff;
62    use crate::schema::HttpSchema;
63    use crate::schemas::openapi303::schema::OpenApi303;
64
65    #[test]
66    fn test_removed_operation_check() {
67        let src_schema: HttpSchema =
68            serde_json::from_str::<OpenApi303>(include_str!(
69            "../../data/checks/removed-operation/schema-with-operations.json"
70        ))
71            .unwrap()
72            .into();
73
74        let tgt_schema: HttpSchema = serde_json::from_str::<OpenApi303>(include_str!(
75            "../../data/checks/removed-operation/schema-with-operations-altered.json"
76        ))
77        .unwrap()
78        .into();
79
80        let diff = get_schema_diff(src_schema, tgt_schema);
81
82        let checker = RemovedOperationCheck::default();
83        crate::visitor::dispatch_visitor(diff.get().unwrap(), &checker);
84        let issues = checker.issues().unwrap();
85
86        assert_eq!(issues.len(), 2);
87        // replaced with `delete`
88        assert_eq!(issues.get(0).unwrap().path.get_path(), "paths//test/put",);
89        //removed from paths
90        assert_eq!(
91            issues.get(1).unwrap().path.get_path(),
92            "paths//test2/post",
93        );
94    }
95}