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
#![forbid(unsafe_code)]

mod oasgen;
mod okapi3;

pub use oasgen::*;

#[cfg(test)]
mod tests {
    use crate::*;
    use openapiv3::OpenAPI;
    use schemars::JsonSchema;
    use serde::Serialize;

    #[test]
    fn it_works() {
        let mut oasb = Oas3Builder::default();

        #[derive(Serialize, JsonSchema)]
        pub struct CollectionWrapper<T> {
            collection: Vec<T>,
        }

        #[derive(Serialize, JsonSchema)]
        pub struct TestEvent {
            pub title: String,
        }

        #[derive(Serialize, JsonSchema)]
        pub struct TestEventForm {
            pub title: String,
        }

        let limit_param = QueryParamBuilder::new::<u64>("limit".to_owned(), Some(std::u64::MAX));
        let categories_param = QueryParamBuilder::new::<Vec<String>>(
            "categories".to_owned(),
            Some(vec![
                "Financial Education".to_owned(),
                "Work safety training".to_owned(),
            ]),
        );
        let categories_param = categories_param.explode(false);
        let qpbs = vec![limit_param, categories_param];
        // list events
        let list_path = ApiPath::with_queries(
            Some("api".to_owned()),
            vec![ApiId::new("organizers", "{oid}")],
            Some("events".to_owned()),
            qpbs,
        );
        oasb.list::<CollectionWrapper<TestEvent>>(&list_path, "Events".to_owned(), None);

        // fetch event
        let fetch_path = ApiPath::new(
            Some("api".to_owned()),
            vec![
                ApiId::new("organizers", "{oid}"),
                ApiId::new("events", "{eid}"),
            ],
            None,
        );
        oasb.fetch::<TestEvent>(&fetch_path, "Events".to_owned(), None);

        // create event
        let create_path = ApiPath::new(
            Some("api".to_owned()),
            vec![ApiId::new("organizers", "{oid}")],
            Some("events".to_owned()),
        );
        oasb.create::<TestEventForm, TestEvent>(&create_path, "Events".to_owned(), None);

        // update event
        oasb.update::<TestEventForm, TestEvent>(&fetch_path, "Events".to_owned(), None);

        // replace event
        oasb.replace::<TestEventForm, TestEvent>(&fetch_path, "Events".to_owned(), None);

        // delete event
        oasb.delete::<TestEventForm, TestEvent>(&fetch_path, "Events".to_owned(), None);

        // delete event
        oasb.delete_by_key::<TestEvent>(&create_path, "Events".to_owned(), None);

        // any operation: find event by title
        let title_param = QueryParamBuilder::new::<String>(
            "title".to_owned(),
            Some("Hackaton 2020 01 23".to_owned()),
        );
        let qpbs = vec![title_param];
        let find_path = ApiPath::with_queries(
            Some("api".to_owned()),
            vec![ApiId::new("organizers", "{oid}")],
            Some("events/find".to_owned()),
            qpbs,
        );
        oasb.any::<(), TestEvent>(
            &find_path,
            http::Method::GET,
            "Events".to_owned(),
            "Find".to_owned(),
            Some("Find and event by it's title".to_owned()),
        );

        let json_str = serde_json::to_string_pretty(&oasb.build("1.0.0".to_owned()));
        let json_str = json_str.unwrap_or_default();

        let _openapi_json: OpenAPI =
            serde_json::from_str(&json_str).expect("Could not deserialize input");

        let _res = std::fs::write("openapi_test.json", json_str.clone());

        // assert_eq!("", json_str);
    }
}