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
use schemars::JsonSchema;
use serde::Serialize;
use serde_json::Value;

use crate::apipath::ApiPath;
use crate::oasgen::Oas3Builder;
use crate::okapi3::{Map, Operation, OperationInfo, Parameter, RefOr, Responses};
use crate::xtests::Test;

impl Oas3Builder {
    pub fn any<I: JsonSchema + Serialize, O: JsonSchema + Serialize, E: JsonSchema + Serialize>(
        &mut self,
        web_path: &ApiPath,
        method: http::Method,
        document_name: String,
        operation_name: &str,
        operation_description: Option<String>,
    ) {
        self.any_with_tests::<I, O, E>(
            web_path,
            method,
            document_name,
            operation_name,
            operation_description,
            &[],
        )
    }

    pub fn any_with_tests<
        I: JsonSchema + Serialize,
        O: JsonSchema + Serialize,
        E: JsonSchema + Serialize,
    >(
        &mut self,
        web_path: &ApiPath,
        method: http::Method,
        document_name: String,
        operation_name: &str,
        operation_description: Option<String>,
        tests: &[Test],
    ) {
        let operation_id = format!("{}{}", operation_name, document_name);

        let mut resps = Responses::default();

        let status = "200".to_owned();
        let resp = self.create_response::<O>(document_name);
        resps.responses.insert(status, resp.into());

        self.add_error_responses::<E>(&mut resps);

        let request_body = self.create_request_body::<I>();

        let mut parameters: Vec<RefOr<Parameter>> = vec![];
        self.add_path_params(web_path.clone(), &mut parameters);

        let mut extensions: Map<String, Value> = Map::default();
        extensions.insert("x-tests".to_owned(), serde_json::to_value(tests).unwrap());

        self.generator.add_operation(OperationInfo {
            path: web_path.to_string(),
            method,
            operation: Operation {
                operation_id: Some(operation_id),
                description: operation_description,
                responses: resps,
                request_body,
                parameters,
                extensions,
                ..Operation::default()
            },
        })
    }
}