1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-02-22 20:52:27 +00:00

add some tests for OpenapiBuilder

This commit is contained in:
Dominic 2020-05-03 19:17:55 +02:00
parent 101e94b900
commit f7157dcf62
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
4 changed files with 82 additions and 11 deletions

View file

@ -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>("users");
route.resource::<Auth>("auth");
route.get_openapi("openapi");

View file

@ -160,6 +160,7 @@ pub mod matcher;
mod openapi;
#[cfg(feature = "openapi")]
pub use openapi::{
builder::OpenapiInfo,
router::GetOpenapi,
types::{OpenapiSchema, OpenapiType}
};

View file

@ -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<String>
}
pub struct OpenapiBuilder
{
pub openapi : Arc<RwLock<OpenAPI>>
@ -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<Message>
}
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::<Option<Messages>>();
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()));
}
}

View file

@ -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<ID : RefUnwindSafe + Send + 'static>
#[cfg(feature = "openapi")]
pub trait WithOpenapi<D>
{
fn with_openapi<F>(&mut self, title : String, version : String, server_url : String, block : F)
fn with_openapi<F>(&mut self, info : OpenapiInfo, block : F)
where
F : FnOnce(OpenapiRouter<D>);
}
@ -318,13 +318,13 @@ macro_rules! implDrawResourceRoutes {
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
P : RefUnwindSafe + Send + Sync + 'static
{
fn with_openapi<F>(&mut self, title : String, version : String, server_url : String, block : F)
fn with_openapi<F>(&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);
}