Skip to content

Commit

Permalink
Make #[derive(Schema)] no-op when openapi is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Jan 25, 2020
1 parent e86c5ba commit 5f29f28
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn sleepy(seconds: Seconds) -> Result<impl rweb::Reply, Infallible> {
}

/// A newtype to enforce our maximum allowed seconds.
#[cfg_attr(feature = "openapi", derive(Schema))]
#[derive(Schema)]
struct Seconds(u64);

impl FromStr for Seconds {
Expand Down
3 changes: 3 additions & 0 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<syn::DeriveInput>(input).expect("failed to parse derive input");
openapi::derive_schema(input).into()
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@
//! }
//!
//!
//! #[cfg_attr(feature = "openapi", derive(Schema))]
//! #[derive(Schema)]
//! struct User {
//! id: String,
//! }
Expand Down
4 changes: 3 additions & 1 deletion src/openapi/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ pub type Responses = BTreeMap<Cow<'static, str>, 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
///
Expand Down

0 comments on commit 5f29f28

Please sign in to comment.