From f7157dcf629e5b0f8c61b026ca75ccb0482da959 Mon Sep 17 00:00:00 2001 From: Dominic Date: Sun, 3 May 2020 19:17:55 +0200 Subject: [PATCH] add some tests for OpenapiBuilder --- example/src/main.rs | 7 ++- gotham_restful/src/lib.rs | 1 + gotham_restful/src/openapi/builder.rs | 77 ++++++++++++++++++++++++--- gotham_restful/src/routing.rs | 8 +-- 4 files changed, 82 insertions(+), 11 deletions(-) diff --git a/example/src/main.rs b/example/src/main.rs index f1d822b..73d892c 100644 --- a/example/src/main.rs +++ b/example/src/main.rs @@ -134,7 +134,12 @@ fn main() ); gotham::start(ADDR, build_router(chain, pipelines, |route| { - route.with_openapi("Users Example".to_owned(), "0.0.1".to_owned(), format!("http://{}", ADDR), |mut route| { + let info = OpenapiInfo { + title: "Users Example".to_owned(), + version: "0.0.1".to_owned(), + urls: vec![format!("http://{}", ADDR)] + }; + route.with_openapi(info, |mut route| { route.resource::("users"); route.resource::("auth"); route.get_openapi("openapi"); diff --git a/gotham_restful/src/lib.rs b/gotham_restful/src/lib.rs index 1142f29..6591105 100644 --- a/gotham_restful/src/lib.rs +++ b/gotham_restful/src/lib.rs @@ -160,6 +160,7 @@ pub mod matcher; mod openapi; #[cfg(feature = "openapi")] pub use openapi::{ + builder::OpenapiInfo, router::GetOpenapi, types::{OpenapiSchema, OpenapiType} }; diff --git a/gotham_restful/src/openapi/builder.rs b/gotham_restful/src/openapi/builder.rs index 88c0ba4..6c3d12b 100644 --- a/gotham_restful/src/openapi/builder.rs +++ b/gotham_restful/src/openapi/builder.rs @@ -6,6 +6,14 @@ use openapiv3::{ }; use std::sync::{Arc, RwLock}; +#[derive(Clone, Debug)] +pub struct OpenapiInfo +{ + pub title : String, + pub version : String, + pub urls : Vec +} + pub struct OpenapiBuilder { pub openapi : Arc> @@ -13,19 +21,19 @@ pub struct OpenapiBuilder impl OpenapiBuilder { - pub fn new(title : String, version : String, url : String) -> Self + pub fn new(info : OpenapiInfo) -> Self { Self { openapi: Arc::new(RwLock::new(OpenAPI { openapi: "3.0.2".to_string(), info: openapiv3::Info { - title, version, + title: info.title, + version: info.version, ..Default::default() }, - servers: vec![Server { - url, - ..Default::default() - }], + servers: info.urls.into_iter() + .map(|url| Server { url, ..Default::default() }) + .collect(), ..Default::default() })) } @@ -94,3 +102,60 @@ impl OpenapiBuilder } } } + + +#[cfg(test)] +#[allow(dead_code)] +mod test +{ + use super::*; + + #[derive(OpenapiType)] + struct Message + { + msg : String + } + + #[derive(OpenapiType)] + struct Messages + { + msgs : Vec + } + + fn info() -> OpenapiInfo + { + OpenapiInfo { + title: "TEST CASE".to_owned(), + version: "1.2.3".to_owned(), + urls: vec!["http://localhost:1234".to_owned(), "https://example.org".to_owned()] + } + } + + fn openapi(builder : OpenapiBuilder) -> OpenAPI + { + Arc::try_unwrap(builder.openapi).unwrap().into_inner().unwrap() + } + + #[test] + fn new_builder() + { + let info = info(); + let builder = OpenapiBuilder::new(info.clone()); + let openapi = openapi(builder); + + assert_eq!(info.title, openapi.info.title); + assert_eq!(info.version, openapi.info.version); + assert_eq!(info.urls.len(), openapi.servers.len()); + } + + #[test] + fn add_schema() + { + let mut builder = OpenapiBuilder::new(info()); + builder.add_schema::>(); + let openapi = openapi(builder); + + assert_eq!(openapi.components.clone().unwrap_or_default().schemas["Message"] , ReferenceOr::Item(Message ::schema().into_schema())); + assert_eq!(openapi.components.clone().unwrap_or_default().schemas["Messages"], ReferenceOr::Item(Messages::schema().into_schema())); + } +} diff --git a/gotham_restful/src/routing.rs b/gotham_restful/src/routing.rs index ffc9e58..cd8ea13 100644 --- a/gotham_restful/src/routing.rs +++ b/gotham_restful/src/routing.rs @@ -8,7 +8,7 @@ use crate::{ }; #[cfg(feature = "openapi")] use crate::openapi::{ - builder::OpenapiBuilder, + builder::{OpenapiBuilder, OpenapiInfo}, router::OpenapiRouter }; @@ -51,7 +51,7 @@ struct PathExtractor #[cfg(feature = "openapi")] pub trait WithOpenapi { - fn with_openapi(&mut self, title : String, version : String, server_url : String, block : F) + fn with_openapi(&mut self, info : OpenapiInfo, block : F) where F : FnOnce(OpenapiRouter); } @@ -318,13 +318,13 @@ macro_rules! implDrawResourceRoutes { C : PipelineHandleChain

+ Copy + Send + Sync + 'static, P : RefUnwindSafe + Send + Sync + 'static { - fn with_openapi(&mut self, title : String, version : String, server_url : String, block : F) + fn with_openapi(&mut self, info : OpenapiInfo, block : F) where F : FnOnce(OpenapiRouter<$implType<'a, C, P>>) { let router = OpenapiRouter { router: self, - openapi_builder: &mut OpenapiBuilder::new(title, version, server_url) + openapi_builder: &mut OpenapiBuilder::new(info) }; block(router); }