From 5f29f28cfb97bd50d0a7a9c33ad4a6e422b53506 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 25 Jan 2020 19:20:43 +0900 Subject: [PATCH] Make #[derive(Schema)] no-op when openapi is disabled --- examples/futures.rs | 2 +- macros/src/lib.rs | 3 +++ src/lib.rs | 2 +- src/openapi/entity.rs | 4 +++- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/examples/futures.rs b/examples/futures.rs index 2b2b01c6..c46e4537 100644 --- a/examples/futures.rs +++ b/examples/futures.rs @@ -18,7 +18,7 @@ async fn sleepy(seconds: Seconds) -> Result { } /// A newtype to enforce our maximum allowed seconds. -#[cfg_attr(feature = "openapi", derive(Schema))] +#[derive(Schema)] struct Seconds(u64); impl FromStr for Seconds { diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 9d1612f9..1ce626e1 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -84,6 +84,9 @@ pub fn router( /// See documentation of Entity for details and examples. #[proc_macro_derive(Schema, attributes(schema))] pub fn derive_schema(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + if !cfg!(feature = "openapi") { + return "".parse().unwrap(); + } let input = syn::parse::(input).expect("failed to parse derive input"); openapi::derive_schema(input).into() } diff --git a/src/lib.rs b/src/lib.rs index ee6615fe..7093186f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -293,7 +293,7 @@ //! } //! //! -//! #[cfg_attr(feature = "openapi", derive(Schema))] +//! #[derive(Schema)] //! struct User { //! id: String, //! } diff --git a/src/openapi/entity.rs b/src/openapi/entity.rs index a442adcc..c8d8645b 100644 --- a/src/openapi/entity.rs +++ b/src/openapi/entity.rs @@ -15,7 +15,9 @@ pub type Responses = BTreeMap, Response>; /// /// # `#[derive(Schema)]` /// -/// It implements [Entity] for the struct or enum. +/// It implements [Entity] for the struct or enum. Note that it's recommended to +/// use `derive(Schema)` even when you are not using openapi, as it is noop when +/// cargo feature openapi is disabled. /// /// ## Overriding description ///