diff --git a/gotham_restful/src/openapi/router.rs b/gotham_restful/src/openapi/router.rs index 24ead43..33f4cba 100644 --- a/gotham_restful/src/openapi/router.rs +++ b/gotham_restful/src/openapi/router.rs @@ -16,21 +16,27 @@ pub trait GetOpenapi fn get_openapi(&mut self, path : &str); } +pub struct OpenapiRouter<'a, D> +{ + pub router : &'a mut D, + pub openapi_builder : &'a mut OpenapiBuilder +} + macro_rules! implOpenapiRouter { ($implType:ident) => { - impl<'a, C, P> GetOpenapi for (&mut $implType<'a, C, P>, &mut OpenapiBuilder) + impl<'a, 'b, C, P> GetOpenapi for OpenapiRouter<'a, $implType<'b, C, P>> where C : PipelineHandleChain

+ Copy + Send + Sync + 'static, P : RefUnwindSafe + Send + Sync + 'static { fn get_openapi(&mut self, path : &str) { - self.0.get(path).to_new_handler(OpenapiHandler::new(self.1.openapi.clone())); + self.router.get(path).to_new_handler(OpenapiHandler::new(self.openapi_builder.openapi.clone())); } } - impl<'a, C, P> DrawResources for (&mut $implType<'a, C, P>, &mut OpenapiBuilder) + impl<'a, 'b, C, P> DrawResources for OpenapiRouter<'a, $implType<'b, C, P>> where C : PipelineHandleChain

+ Copy + Send + Sync + 'static, P : RefUnwindSafe + Send + Sync + 'static @@ -41,46 +47,46 @@ macro_rules! implOpenapiRouter { } } - impl<'a, C, P> DrawResourceRoutes for (&mut (&mut $implType<'a, C, P>, &mut OpenapiBuilder), &str) + impl<'a, 'b, C, P> DrawResourceRoutes for (&mut OpenapiRouter<'a, $implType<'b, C, P>>, &str) where C : PipelineHandleChain

+ Copy + Send + Sync + 'static, P : RefUnwindSafe + Send + Sync + 'static { fn read_all(&mut self) { - let schema = (self.0).1.add_schema::(); + let schema = (self.0).openapi_builder.add_schema::(); let path = format!("/{}", &self.1); - let mut item = (self.0).1.remove_path(&path); + let mut item = (self.0).openapi_builder.remove_path(&path); item.get = Some(OperationDescription::new::(schema).into_operation()); - (self.0).1.add_path(path, item); + (self.0).openapi_builder.add_path(path, item); - (&mut *(self.0).0, self.1).read_all::() + (&mut *(self.0).router, self.1).read_all::() } fn read(&mut self) { - let schema = (self.0).1.add_schema::(); - let id_schema = (self.0).1.add_schema::(); + let schema = (self.0).openapi_builder.add_schema::(); + let id_schema = (self.0).openapi_builder.add_schema::(); let path = format!("/{}/{{id}}", &self.1); - let mut item = (self.0).1.remove_path(&path); + let mut item = (self.0).openapi_builder.remove_path(&path); item.get = Some(OperationDescription::new::(schema).add_path_param("id", id_schema).into_operation()); - (self.0).1.add_path(path, item); + (self.0).openapi_builder.add_path(path, item); - (&mut *(self.0).0, self.1).read::() + (&mut *(self.0).router, self.1).read::() } fn search(&mut self) { - let schema = (self.0).1.add_schema::(); + let schema = (self.0).openapi_builder.add_schema::(); let path = format!("/{}/search", &self.1); - let mut item = (self.0).1.remove_path(&self.1); + let mut item = (self.0).openapi_builder.remove_path(&self.1); item.get = Some(OperationDescription::new::(schema).with_query_params(Handler::Query::schema()).into_operation()); - (self.0).1.add_path(path, item); + (self.0).openapi_builder.add_path(path, item); - (&mut *(self.0).0, self.1).search::() + (&mut *(self.0).router, self.1).search::() } fn create(&mut self) @@ -88,15 +94,15 @@ macro_rules! implOpenapiRouter { Handler::Res : 'static, Handler::Body : 'static { - let schema = (self.0).1.add_schema::(); - let body_schema = (self.0).1.add_schema::(); + let schema = (self.0).openapi_builder.add_schema::(); + let body_schema = (self.0).openapi_builder.add_schema::(); let path = format!("/{}", &self.1); - let mut item = (self.0).1.remove_path(&path); + let mut item = (self.0).openapi_builder.remove_path(&path); item.post = Some(OperationDescription::new::(schema).with_body::(body_schema).into_operation()); - (self.0).1.add_path(path, item); + (self.0).openapi_builder.add_path(path, item); - (&mut *(self.0).0, self.1).create::() + (&mut *(self.0).router, self.1).create::() } fn update_all(&mut self) @@ -104,15 +110,15 @@ macro_rules! implOpenapiRouter { Handler::Res : 'static, Handler::Body : 'static { - let schema = (self.0).1.add_schema::(); - let body_schema = (self.0).1.add_schema::(); + let schema = (self.0).openapi_builder.add_schema::(); + let body_schema = (self.0).openapi_builder.add_schema::(); let path = format!("/{}", &self.1); - let mut item = (self.0).1.remove_path(&path); + let mut item = (self.0).openapi_builder.remove_path(&path); item.put = Some(OperationDescription::new::(schema).with_body::(body_schema).into_operation()); - (self.0).1.add_path(path, item); + (self.0).openapi_builder.add_path(path, item); - (&mut *(self.0).0, self.1).update_all::() + (&mut *(self.0).router, self.1).update_all::() } fn update(&mut self) @@ -120,41 +126,41 @@ macro_rules! implOpenapiRouter { Handler::Res : 'static, Handler::Body : 'static { - let schema = (self.0).1.add_schema::(); - let id_schema = (self.0).1.add_schema::(); - let body_schema = (self.0).1.add_schema::(); + let schema = (self.0).openapi_builder.add_schema::(); + let id_schema = (self.0).openapi_builder.add_schema::(); + let body_schema = (self.0).openapi_builder.add_schema::(); let path = format!("/{}/{{id}}", &self.1); - let mut item = (self.0).1.remove_path(&path); + let mut item = (self.0).openapi_builder.remove_path(&path); item.put = Some(OperationDescription::new::(schema).add_path_param("id", id_schema).with_body::(body_schema).into_operation()); - (self.0).1.add_path(path, item); + (self.0).openapi_builder.add_path(path, item); - (&mut *(self.0).0, self.1).update::() + (&mut *(self.0).router, self.1).update::() } fn delete_all(&mut self) { - let schema = (self.0).1.add_schema::(); + let schema = (self.0).openapi_builder.add_schema::(); let path = format!("/{}", &self.1); - let mut item = (self.0).1.remove_path(&path); + let mut item = (self.0).openapi_builder.remove_path(&path); item.delete = Some(OperationDescription::new::(schema).into_operation()); - (self.0).1.add_path(path, item); + (self.0).openapi_builder.add_path(path, item); - (&mut *(self.0).0, self.1).delete_all::() + (&mut *(self.0).router, self.1).delete_all::() } fn delete(&mut self) { - let schema = (self.0).1.add_schema::(); - let id_schema = (self.0).1.add_schema::(); + let schema = (self.0).openapi_builder.add_schema::(); + let id_schema = (self.0).openapi_builder.add_schema::(); let path = format!("/{}/{{id}}", &self.1); - let mut item = (self.0).1.remove_path(&path); + let mut item = (self.0).openapi_builder.remove_path(&path); item.delete = Some(OperationDescription::new::(schema).add_path_param("id", id_schema).into_operation()); - (self.0).1.add_path(path, item); + (self.0).openapi_builder.add_path(path, item); - (&mut *(self.0).0, self.1).delete::() + (&mut *(self.0).router, self.1).delete::() } } diff --git a/gotham_restful/src/routing.rs b/gotham_restful/src/routing.rs index b87089b..f213248 100644 --- a/gotham_restful/src/routing.rs +++ b/gotham_restful/src/routing.rs @@ -1,5 +1,6 @@ use crate::{ matcher::{AcceptHeaderMatcher, ContentTypeMatcher}, + openapi::router::OpenapiRouter, resource::*, result::{ResourceError, ResourceResult, Response}, RequestBody, @@ -49,7 +50,7 @@ pub trait WithOpenapi { fn with_openapi(&mut self, title : String, version : String, server_url : String, block : F) where - F : FnOnce((&mut D, &mut OpenapiBuilder)); + F : FnOnce(OpenapiRouter); } /// This trait adds the `resource` method to gotham's routing. It allows you to register @@ -316,10 +317,13 @@ macro_rules! implDrawResourceRoutes { { fn with_openapi(&mut self, title : String, version : String, server_url : String, block : F) where - F : FnOnce((&mut Self, &mut OpenapiBuilder)) + F : FnOnce(OpenapiRouter<$implType<'a, C, P>>) { - let mut router = OpenapiBuilder::new(title, version, server_url); - block((self, &mut router)); + let router = OpenapiRouter { + router: self, + openapi_builder: &mut OpenapiBuilder::new(title, version, server_url) + }; + block(router); } }