From 1f0be034be9c7ea94750e6fd5735364436030427 Mon Sep 17 00:00:00 2001 From: Dominic Date: Mon, 30 Mar 2020 22:30:04 +0200 Subject: [PATCH] start moving openapi into its own crate --- Cargo.toml | 2 + example/Cargo.toml | 8 +--- gotham_restful/Cargo.toml | 3 +- gotham_restful/src/lib.rs | 15 ------ gotham_restful/src/openapi/mod.rs | 3 -- gotham_restful/src/routing.rs | 36 -------------- gotham_restful/src/types.rs | 2 - gotham_restful_openapi/Cargo.toml | 33 +++++++++++++ gotham_restful_openapi/LICENSE-Apache | 1 + gotham_restful_openapi/LICENSE-EPL | 1 + gotham_restful_openapi/LICENSE.md | 1 + gotham_restful_openapi/README.md | 1 + gotham_restful_openapi/src/lib.rs | 14 ++++++ .../src}/router.rs | 47 +++++++++++++++---- .../src}/types.rs | 0 15 files changed, 93 insertions(+), 74 deletions(-) delete mode 100644 gotham_restful/src/openapi/mod.rs create mode 100644 gotham_restful_openapi/Cargo.toml create mode 120000 gotham_restful_openapi/LICENSE-Apache create mode 120000 gotham_restful_openapi/LICENSE-EPL create mode 120000 gotham_restful_openapi/LICENSE.md create mode 120000 gotham_restful_openapi/README.md create mode 100644 gotham_restful_openapi/src/lib.rs rename {gotham_restful/src/openapi => gotham_restful_openapi/src}/router.rs (94%) rename {gotham_restful/src/openapi => gotham_restful_openapi/src}/types.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 418c362..3d6d6e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,9 +4,11 @@ members = [ "gotham_restful", "gotham_restful_derive", + "gotham_restful_openapi", "example" ] [patch.crates-io] gotham_restful = { path = "./gotham_restful" } gotham_restful_derive = { path = "./gotham_restful_derive" } +gotham_restful_openapi = { path = "./gotham_restful_openapi" } diff --git a/example/Cargo.toml b/example/Cargo.toml index a1ff5df..bb65402 100644 --- a/example/Cargo.toml +++ b/example/Cargo.toml @@ -17,13 +17,9 @@ gitlab = { repository = "msrd0/gotham-restful", branch = "master" } fake = "2.2" gotham = "0.4" gotham_derive = "0.4" -gotham_restful = { version = "0.0.3", features = ["auth", "openapi"] } +gotham_restful = { version = "0.0.3", features = ["auth"] } +gotham_restful_openapi = "0.0.1" hyper = "0.12" log = "0.4" log4rs = { version = "0.8", features = ["console_appender"], default-features = false } serde = "1" - -[dev-dependencies] -fake = "2.2" -log = "0.4" -log4rs = { version = "0.8", features = ["console_appender"], default-features = false } diff --git a/gotham_restful/Cargo.toml b/gotham_restful/Cargo.toml index 29fc087..184afa8 100644 --- a/gotham_restful/Cargo.toml +++ b/gotham_restful/Cargo.toml @@ -23,7 +23,7 @@ futures = "0.1.29" gotham = "0.4" gotham_derive = "0.4" gotham_middleware_diesel = { version = "0.1", optional = true } -gotham_restful_derive = { version = "0.0.2" } +gotham_restful_derive = "0.0.2" hyper = "0.12.35" indexmap = { version = "1.3.0", optional = true } jsonwebtoken = { version = "6.0.1", optional = true } @@ -42,7 +42,6 @@ default = [] auth = ["gotham_restful_derive/auth", "base64", "cookie", "jsonwebtoken"] errorlog = [] database = ["gotham_restful_derive/database", "gotham_middleware_diesel"] -openapi = ["gotham_restful_derive/openapi", "indexmap", "log", "openapiv3"] [package.metadata.docs.rs] all-features = true diff --git a/gotham_restful/src/lib.rs b/gotham_restful/src/lib.rs index f78109a..9710c53 100644 --- a/gotham_restful/src/lib.rs +++ b/gotham_restful/src/lib.rs @@ -128,11 +128,6 @@ pub mod export #[cfg(feature = "database")] pub use gotham_middleware_diesel::Repo; - - #[cfg(feature = "openapi")] - pub use indexmap::IndexMap; - #[cfg(feature = "openapi")] - pub use openapiv3 as openapi; } #[cfg(feature = "auth")] @@ -147,14 +142,6 @@ pub use auth::{ StaticAuthHandler }; -#[cfg(feature = "openapi")] -mod openapi; -#[cfg(feature = "openapi")] -pub use openapi::{ - router::GetOpenapi, - types::{OpenapiSchema, OpenapiType} -}; - mod resource; pub use resource::{ Resource, @@ -181,8 +168,6 @@ pub use result::{ mod routing; pub use routing::{DrawResources, DrawResourceRoutes}; -#[cfg(feature = "openapi")] -pub use routing::WithOpenapi; mod types; pub use types::*; diff --git a/gotham_restful/src/openapi/mod.rs b/gotham_restful/src/openapi/mod.rs deleted file mode 100644 index 5c19494..0000000 --- a/gotham_restful/src/openapi/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ - -pub mod router; -pub mod types; diff --git a/gotham_restful/src/routing.rs b/gotham_restful/src/routing.rs index 61c8c97..0faf847 100644 --- a/gotham_restful/src/routing.rs +++ b/gotham_restful/src/routing.rs @@ -49,23 +49,6 @@ struct PathExtractor id : ID } -/// This trait adds the `with_openapi` method to gotham's routing. It turns the default -/// router into one that will only allow RESTful resources, but record them and generate -/// an OpenAPI specification on request. -#[cfg(feature = "openapi")] -pub trait WithOpenapi -where - C : PipelineHandleChain

+ Copy + Send + Sync + 'static, - P : RefUnwindSafe + Send + Sync + 'static -{ - fn with_openapi(&mut self, title : Title, version : Version, server_url : Url, block : F) - where - F : FnOnce(OpenapiRouter), - Title : ToString, - Version : ToString, - Url : ToString; -} - /// This trait adds the `resource` method to gotham's routing. It allows you to register /// any RESTful `Resource` with a path. pub trait DrawResources @@ -346,25 +329,6 @@ impl From>> for MaybeMatchContentTypeHeader } } -#[cfg(feature = "openapi")] -impl WithOpenapi for T -where - C : PipelineHandleChain

+ Copy + Send + Sync + 'static, - P : RefUnwindSafe + Send + Sync + 'static, - T : DrawRoutes -{ - fn with_openapi(&mut self, title : Title, version : Version, server_url : Url, block : F) - where - F : FnOnce(OpenapiRouter), - Title : ToString, - Version : ToString, - Url : ToString - { - let router = OpenapiRouter::new(self, OpenapiBuilder::new(title, version, server_url)); - block(router); - } -} - impl DrawResources for T where C : PipelineHandleChain

+ Copy + Send + Sync + 'static, diff --git a/gotham_restful/src/types.rs b/gotham_restful/src/types.rs index 6764595..a5ebde8 100644 --- a/gotham_restful/src/types.rs +++ b/gotham_restful/src/types.rs @@ -1,5 +1,3 @@ -#[cfg(feature = "openapi")] -use crate::OpenapiType; use crate::result::ResourceError; use hyper::Chunk; diff --git a/gotham_restful_openapi/Cargo.toml b/gotham_restful_openapi/Cargo.toml new file mode 100644 index 0000000..e510664 --- /dev/null +++ b/gotham_restful_openapi/Cargo.toml @@ -0,0 +1,33 @@ +# -*- eval: (cargo-minor-mode 1) -*- + +[package] +name = "gotham_restful_openapi" +version = "0.0.1" +authors = ["Dominic Meiser "] +edition = "2018" +description = "RESTful additions for Gotham - OpenAPI Support" +keywords = ["gotham", "rest", "restful", "openapi", "swagger"] +license = "EPL-2.0 OR Apache-2.0" +readme = "README.md" +repository = "https://gitlab.com/msrd0/gotham-restful" + +[badges] +gitlab = { repository = "msrd0/gotham-restful", branch = "master" } +codecov = { repository = "msrd0/gotham-restful", branch = "master", service = "gitlab" } + +[dependencies] +futures = "0.1.29" +gotham = "0.4" +gotham_derive = "0.4" +gotham_restful = "0.0.3" +gotham_restful_derive = { version = "0.0.2", features = ["openapi"] } +hyper = "0.12.35" +indexmap = "1.3.0" +log = "0.4.8" +mime = "0.3.16" +openapiv3 = "0.3" +serde = { version = "1.0.104", features = ["derive"] } +serde_json = "1.0.45" + +[dev-dependencies] +thiserror = "1" diff --git a/gotham_restful_openapi/LICENSE-Apache b/gotham_restful_openapi/LICENSE-Apache new file mode 120000 index 0000000..0cd69a3 --- /dev/null +++ b/gotham_restful_openapi/LICENSE-Apache @@ -0,0 +1 @@ +../LICENSE-Apache \ No newline at end of file diff --git a/gotham_restful_openapi/LICENSE-EPL b/gotham_restful_openapi/LICENSE-EPL new file mode 120000 index 0000000..2004d06 --- /dev/null +++ b/gotham_restful_openapi/LICENSE-EPL @@ -0,0 +1 @@ +../LICENSE-EPL \ No newline at end of file diff --git a/gotham_restful_openapi/LICENSE.md b/gotham_restful_openapi/LICENSE.md new file mode 120000 index 0000000..7eabdb1 --- /dev/null +++ b/gotham_restful_openapi/LICENSE.md @@ -0,0 +1 @@ +../LICENSE.md \ No newline at end of file diff --git a/gotham_restful_openapi/README.md b/gotham_restful_openapi/README.md new file mode 120000 index 0000000..32d46ee --- /dev/null +++ b/gotham_restful_openapi/README.md @@ -0,0 +1 @@ +../README.md \ No newline at end of file diff --git a/gotham_restful_openapi/src/lib.rs b/gotham_restful_openapi/src/lib.rs new file mode 100644 index 0000000..ccda90a --- /dev/null +++ b/gotham_restful_openapi/src/lib.rs @@ -0,0 +1,14 @@ + +mod router; +pub use router::GetOpenapi; + +mod types; +pub use types::{OpenapiType, OpenapiSchema}; + +/// Not public API +#[doc(hidden)] +pub mod export +{ + pub use indexmap::IndexMap; + pub use openapiv3 as openapi; +} diff --git a/gotham_restful/src/openapi/router.rs b/gotham_restful_openapi/src/router.rs similarity index 94% rename from gotham_restful/src/openapi/router.rs rename to gotham_restful_openapi/src/router.rs index 6e9be42..624d61f 100644 --- a/gotham_restful/src/openapi/router.rs +++ b/gotham_restful_openapi/src/router.rs @@ -1,12 +1,4 @@ -use crate::{ - resource::*, - result::*, - routing::*, - OpenapiSchema, - OpenapiType, - RequestBody, - ResourceType -}; +use crate::types::*; use futures::future::ok; use gotham::{ extractor::QueryStringExtractor, @@ -22,6 +14,7 @@ use gotham::{ }, state::State }; +use gotham_restful::*; use hyper::Body; use indexmap::IndexMap; use log::error; @@ -29,11 +22,45 @@ use mime::{Mime, APPLICATION_JSON, TEXT_PLAIN}; use openapiv3::{ APIKeyLocation, Components, MediaType, OpenAPI, Operation, Parameter, ParameterData, ParameterSchemaOrContent, PathItem, Paths, ReferenceOr, ReferenceOr::Item, ReferenceOr::Reference, RequestBody as OARequestBody, Response, Responses, Schema, - SchemaKind, SecurityScheme, Server, StatusCode, Type + SchemaKind, SecurityRequirement, SecurityScheme, Server, StatusCode, Type }; use serde::de::DeserializeOwned; use std::panic::RefUnwindSafe; +/// This trait adds the `with_openapi` method to gotham's routing. It turns the default +/// router into one that will only allow RESTful resources, but record them and generate +/// an OpenAPI specification on request. +pub trait WithOpenapi +where + C : PipelineHandleChain

+ Copy + Send + Sync + 'static, + P : RefUnwindSafe + Send + Sync + 'static +{ + fn with_openapi(&mut self, title : Title, version : Version, server_url : Url, block : F) + where + F : FnOnce(OpenapiRouter), + Title : ToString, + Version : ToString, + Url : ToString; +} + +impl WithOpenapi for T +where + C : PipelineHandleChain

+ Copy + Send + Sync + 'static, + P : RefUnwindSafe + Send + Sync + 'static, + T : DrawRoutes +{ + fn with_openapi(&mut self, title : Title, version : Version, server_url : Url, block : F) + where + F : FnOnce(OpenapiRouter), + Title : ToString, + Version : ToString, + Url : ToString + { + let router = OpenapiRouter::new(self, OpenapiBuilder::new(title, version, server_url)); + block(router); + } +} + /// A helper struct for `OpenapiRouter` to build the OpenAPI specification. pub struct OpenapiBuilder(OpenAPI); diff --git a/gotham_restful/src/openapi/types.rs b/gotham_restful_openapi/src/types.rs similarity index 100% rename from gotham_restful/src/openapi/types.rs rename to gotham_restful_openapi/src/types.rs