From 389740cd64d0fde28148ea01ae982f29d1ab513c Mon Sep 17 00:00:00 2001 From: Dominic Date: Tue, 1 Oct 2019 00:34:58 +0200 Subject: [PATCH] add openapi for other methods, so far without request bodies --- src/openapi/router.rs | 61 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/src/openapi/router.rs b/src/openapi/router.rs index 678ad5f..25dc3c0 100644 --- a/src/openapi/router.rs +++ b/src/openapi/router.rs @@ -17,8 +17,8 @@ use log::error; use mime::{APPLICATION_JSON, TEXT_PLAIN}; use openapiv3::{ Components, MediaType, OpenAPI, Operation, Parameter, ParameterData, ParameterSchemaOrContent, PathItem, - PathStyle, Paths, ReferenceOr, ReferenceOr::Item, ReferenceOr::Reference, Response, Responses, Schema, - SchemaData, Server, StatusCode + PathStyle, Paths, ReferenceOr, ReferenceOr::Item, ReferenceOr::Reference, RequestBody, Response, Responses, + Schema, SchemaData, Server, StatusCode }; use serde::de::DeserializeOwned; use std::panic::RefUnwindSafe; @@ -147,7 +147,7 @@ pub trait GetOpenapi fn get_openapi(&mut self, path : &str); } -fn new_operation(schema : &str, path_params : Vec<&str>) -> Operation +fn schema_to_content(schema : &str) -> IndexMap { let mut content : IndexMap = IndexMap::new(); content.insert(APPLICATION_JSON.to_string(), MediaType { @@ -158,12 +158,16 @@ fn new_operation(schema : &str, path_params : Vec<&str>) -> Operation examples: IndexMap::new(), encoding: IndexMap::new() }); - + content +} + +fn new_operation(schema : &str, path_params : Vec<&str>, body_schema : Option<&str>) -> Operation +{ let mut responses : IndexMap> = IndexMap::new(); responses.insert(StatusCode::Code(200), Item(Response { description: "OK".to_string(), headers: IndexMap::new(), - content, + content: schema_to_content(schema), links: IndexMap::new() })); @@ -186,6 +190,12 @@ fn new_operation(schema : &str, path_params : Vec<&str>) -> Operation style: PathStyle::default(), })); } + + let request_body = body_schema.map(|schema| Item(RequestBody { + description: None, + content: schema_to_content(schema), + required: true + })); Operation { tags: Vec::new(), @@ -194,7 +204,7 @@ fn new_operation(schema : &str, path_params : Vec<&str>) -> Operation external_documentation: None, operation_id: None, // TODO parameters: params, - request_body: None, + request_body, responses: Responses { default: None, responses @@ -244,7 +254,7 @@ macro_rules! implOpenapiRouter { let path = format!("/{}", &self.1); let mut item = (self.0).1.remove_path(&path); - item.get = Some(new_operation(&schema, vec![])); + item.get = Some(new_operation(&schema, vec![], None)); (self.0).1.add_path(path, item); (&mut *(self.0).0, self.1.to_string()).read_all::() @@ -260,7 +270,7 @@ macro_rules! implOpenapiRouter { let path = format!("/{}/{{id}}", &self.1); let mut item = (self.0).1.remove_path(&path); - item.get = Some(new_operation(&schema, vec!["id"])); + item.get = Some(new_operation(&schema, vec!["id"], None)); (self.0).1.add_path(path, item); (&mut *(self.0).0, self.1.to_string()).read::() @@ -272,6 +282,13 @@ macro_rules! implOpenapiRouter { Res : ResourceResult, Handler : ResourceCreate { + let schema = (self.0).1.add_schema::(&self.1, "create", "result_body"); + + let path = format!("/{}", &self.1); + let mut item = (self.0).1.remove_path(&path); + item.post = Some(new_operation(&schema, vec![], None)); + (self.0).1.add_path(path, item); + (&mut *(self.0).0, self.1.to_string()).create::() } @@ -281,6 +298,13 @@ macro_rules! implOpenapiRouter { Res : ResourceResult, Handler : ResourceUpdateAll { + let schema = (self.0).1.add_schema::(&self.1, "update_all", "result_body"); + + let path = format!("/{}", &self.1); + let mut item = (self.0).1.remove_path(&path); + item.put = Some(new_operation(&schema, vec![], None)); + (self.0).1.add_path(path, item); + (&mut *(self.0).0, self.1.to_string()).update_all::() } @@ -291,6 +315,13 @@ macro_rules! implOpenapiRouter { Res : ResourceResult, Handler : ResourceUpdate { + let schema = (self.0).1.add_schema::(&self.1, "update", "result_body"); + + let path = format!("/{}/{{id}}", &self.1); + let mut item = (self.0).1.remove_path(&path); + item.put = Some(new_operation(&schema, vec!["id"], None)); + (self.0).1.add_path(path, item); + (&mut *(self.0).0, self.1.to_string()).update::() } @@ -299,6 +330,13 @@ macro_rules! implOpenapiRouter { Res : ResourceResult, Handler : ResourceDeleteAll { + let schema = (self.0).1.add_schema::(&self.1, "delete_all", "result_body"); + + let path = format!("/{}", &self.1); + let mut item = (self.0).1.remove_path(&path); + item.delete = Some(new_operation(&schema, vec![], None)); + (self.0).1.add_path(path, item); + (&mut *(self.0).0, self.1.to_string()).delete_all::() } @@ -308,6 +346,13 @@ macro_rules! implOpenapiRouter { Res : ResourceResult, Handler : ResourceDelete { + let schema = (self.0).1.add_schema::(&self.1, "delete", "result_body"); + + let path = format!("/{}/{{id}}", &self.1); + let mut item = (self.0).1.remove_path(&path); + item.delete = Some(new_operation(&schema, vec!["id"], None)); + (self.0).1.add_path(path, item); + (&mut *(self.0).0, self.1.to_string()).delete::() } }