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

mod any;
mod apipath;
mod create;
mod delete;
mod fetch;
mod list;
mod queryparam;
mod replace;
mod update;

mod oasgen;
mod okapi3;
pub mod xtests;

pub use any::*;
pub use create::*;
pub use delete::*;
pub use fetch::*;
pub use list::*;
pub use replace::*;
pub use update::*;

pub use apipath::*;
pub use oasgen::*;
pub use okapi3::{RefOr, SecurityScheme};
pub use queryparam::*;

#[cfg(test)]
mod tests {
    use crate::{ApiId, ApiPath, Oas3Builder, QueryParamBuilder};
    use openapiv3::OpenAPI;
    use schemars::JsonSchema;
    use serde::Serialize;

    #[test]
    fn it_works() {
        #[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 mut oasb = Oas3Builder::default();

        let limit_param = QueryParamBuilder::new::<u64>("limit".to_owned(), Some(u64::max_value()));
        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>, String>(&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, String>(&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, String>(&create_path, "Events".to_owned(), None);

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

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

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

        // delete event
        oasb.delete_by_key::<TestEvent, String>(&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, String>(
            &find_path,
            http::Method::GET,
            "Events".to_owned(),
            "Find",
            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);

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