mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-02-23 04:52:28 +00:00
add some tests for OpenapiBuilder
This commit is contained in:
parent
101e94b900
commit
f7157dcf62
4 changed files with 82 additions and 11 deletions
|
@ -134,7 +134,12 @@ fn main()
|
||||||
);
|
);
|
||||||
|
|
||||||
gotham::start(ADDR, build_router(chain, pipelines, |route| {
|
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::<Users>("users");
|
||||||
route.resource::<Auth>("auth");
|
route.resource::<Auth>("auth");
|
||||||
route.get_openapi("openapi");
|
route.get_openapi("openapi");
|
||||||
|
|
|
@ -160,6 +160,7 @@ pub mod matcher;
|
||||||
mod openapi;
|
mod openapi;
|
||||||
#[cfg(feature = "openapi")]
|
#[cfg(feature = "openapi")]
|
||||||
pub use openapi::{
|
pub use openapi::{
|
||||||
|
builder::OpenapiInfo,
|
||||||
router::GetOpenapi,
|
router::GetOpenapi,
|
||||||
types::{OpenapiSchema, OpenapiType}
|
types::{OpenapiSchema, OpenapiType}
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,6 +6,14 @@ use openapiv3::{
|
||||||
};
|
};
|
||||||
use std::sync::{Arc, RwLock};
|
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 struct OpenapiBuilder
|
||||||
{
|
{
|
||||||
pub openapi : Arc<RwLock<OpenAPI>>
|
pub openapi : Arc<RwLock<OpenAPI>>
|
||||||
|
@ -13,19 +21,19 @@ pub struct OpenapiBuilder
|
||||||
|
|
||||||
impl OpenapiBuilder
|
impl OpenapiBuilder
|
||||||
{
|
{
|
||||||
pub fn new(title : String, version : String, url : String) -> Self
|
pub fn new(info : OpenapiInfo) -> Self
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
openapi: Arc::new(RwLock::new(OpenAPI {
|
openapi: Arc::new(RwLock::new(OpenAPI {
|
||||||
openapi: "3.0.2".to_string(),
|
openapi: "3.0.2".to_string(),
|
||||||
info: openapiv3::Info {
|
info: openapiv3::Info {
|
||||||
title, version,
|
title: info.title,
|
||||||
|
version: info.version,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
servers: vec![Server {
|
servers: info.urls.into_iter()
|
||||||
url,
|
.map(|url| Server { url, ..Default::default() })
|
||||||
..Default::default()
|
.collect(),
|
||||||
}],
|
|
||||||
..Default::default()
|
..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()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ use crate::{
|
||||||
};
|
};
|
||||||
#[cfg(feature = "openapi")]
|
#[cfg(feature = "openapi")]
|
||||||
use crate::openapi::{
|
use crate::openapi::{
|
||||||
builder::OpenapiBuilder,
|
builder::{OpenapiBuilder, OpenapiInfo},
|
||||||
router::OpenapiRouter
|
router::OpenapiRouter
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -51,7 +51,7 @@ struct PathExtractor<ID : RefUnwindSafe + Send + 'static>
|
||||||
#[cfg(feature = "openapi")]
|
#[cfg(feature = "openapi")]
|
||||||
pub trait WithOpenapi<D>
|
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
|
where
|
||||||
F : FnOnce(OpenapiRouter<D>);
|
F : FnOnce(OpenapiRouter<D>);
|
||||||
}
|
}
|
||||||
|
@ -318,13 +318,13 @@ macro_rules! implDrawResourceRoutes {
|
||||||
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
||||||
P : RefUnwindSafe + 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
|
where
|
||||||
F : FnOnce(OpenapiRouter<$implType<'a, C, P>>)
|
F : FnOnce(OpenapiRouter<$implType<'a, C, P>>)
|
||||||
{
|
{
|
||||||
let router = OpenapiRouter {
|
let router = OpenapiRouter {
|
||||||
router: self,
|
router: self,
|
||||||
openapi_builder: &mut OpenapiBuilder::new(title, version, server_url)
|
openapi_builder: &mut OpenapiBuilder::new(info)
|
||||||
};
|
};
|
||||||
block(router);
|
block(router);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue