diff --git a/CHANGELOG.md b/CHANGELOG.md index 8de8b10..63e5d80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [0.6.1] - 2019-12-09 +### Fixed: +- Fix a compile error that can occur when deriving `JsonSchema` from a project that doesn't reference serde_json + ## [0.6.0] - 2019-12-09 ### Added: - When deriving `JsonSchema`, the schema's `title` and `description` are now set from `#[doc]` comments (https://github.com/GREsau/schemars/issues/7) diff --git a/schemars/Cargo.toml b/schemars/Cargo.toml index 66c1991..4f4f082 100644 --- a/schemars/Cargo.toml +++ b/schemars/Cargo.toml @@ -2,7 +2,7 @@ name = "schemars" description = "Generate JSON Schemas from Rust code" repository = "https://github.com/GREsau/schemars" -version = "0.6.0" +version = "0.6.1" authors = ["Graham Esau "] edition = "2018" license = "MIT" @@ -12,7 +12,7 @@ categories = ["encoding"] build = "build.rs" [dependencies] -schemars_derive = { version = "0.6.0", path = "../schemars_derive" } +schemars_derive = { version = "=0.6.1", path = "../schemars_derive" } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" chrono = { version = "0.4", default-features = false, optional = true } diff --git a/schemars/src/lib.rs b/schemars/src/lib.rs index 672ff96..14f4558 100644 --- a/schemars/src/lib.rs +++ b/schemars/src/lib.rs @@ -195,6 +195,10 @@ pub mod schema; pub use schemars_derive::*; +// Export serde_json so schemars_derive can use it +#[doc(hidden)] +pub use serde_json as _serde_json; + use schema::Schema; /// A type which can be described as a JSON Schema document. @@ -215,64 +219,64 @@ use schema::Schema; /// let my_schema = schema_for!(MyStruct); /// ``` pub trait JsonSchema { - /// Whether JSON Schemas generated for this type should be re-used where possible using the `$ref` keyword. - /// - /// For trivial types (such as primitives), this should return `false`. For more complex types, it should return `true`. - /// For recursive types, this *must* return `true` to prevent infinite cycles when generating schemas. - /// - /// By default, this returns `true`. - fn is_referenceable() -> bool { - true - } + /// Whether JSON Schemas generated for this type should be re-used where possible using the `$ref` keyword. + /// + /// For trivial types (such as primitives), this should return `false`. For more complex types, it should return `true`. + /// For recursive types, this *must* return `true` to prevent infinite cycles when generating schemas. + /// + /// By default, this returns `true`. + fn is_referenceable() -> bool { + true + } - /// The name of the generated JSON Schema. - /// - /// This is used as the title for root schemas, and the key within the `definitions` property for subschemas. - fn schema_name() -> String; + /// The name of the generated JSON Schema. + /// + /// This is used as the title for root schemas, and the key within the `definitions` property for subschemas. + fn schema_name() -> String; - /// Generates a JSON Schema for this type. - /// - /// If the returned schema depends on any [referenceable](JsonSchema::is_referenceable) schemas, then this method will - /// add them to the [`SchemaGenerator`](gen::SchemaGenerator)'s schema definitions. - /// - /// This should not return a `$ref` schema. - fn json_schema(gen: &mut gen::SchemaGenerator) -> Schema; + /// Generates a JSON Schema for this type. + /// + /// If the returned schema depends on any [referenceable](JsonSchema::is_referenceable) schemas, then this method will + /// add them to the [`SchemaGenerator`](gen::SchemaGenerator)'s schema definitions. + /// + /// This should not return a `$ref` schema. + fn json_schema(gen: &mut gen::SchemaGenerator) -> Schema; - /// Helper for generating schemas for flattened `Option` fields. - /// - /// This should not need to be called or implemented by code outside of `schemars`. - #[doc(hidden)] - fn json_schema_optional(gen: &mut gen::SchemaGenerator) -> Schema { - Self::json_schema(gen) - } + /// Helper for generating schemas for flattened `Option` fields. + /// + /// This should not need to be called or implemented by code outside of `schemars`. + #[doc(hidden)] + fn json_schema_optional(gen: &mut gen::SchemaGenerator) -> Schema { + Self::json_schema(gen) + } } #[cfg(test)] pub mod tests { - use super::*; + use super::*; - pub fn schema_object_for() -> schema::SchemaObject { - schema_object(schema_for::()) - } + pub fn schema_object_for() -> schema::SchemaObject { + schema_object(schema_for::()) + } - pub fn custom_schema_object_for( - settings: gen::SchemaSettings, - ) -> schema::SchemaObject { - schema_object(custom_schema_for::(settings)) - } + pub fn custom_schema_object_for( + settings: gen::SchemaSettings, + ) -> schema::SchemaObject { + schema_object(custom_schema_for::(settings)) + } - pub fn schema_for() -> schema::Schema { - custom_schema_for::(Default::default()) - } + pub fn schema_for() -> schema::Schema { + custom_schema_for::(Default::default()) + } - pub fn custom_schema_for(settings: gen::SchemaSettings) -> schema::Schema { - T::json_schema(&mut gen::SchemaGenerator::new(settings)) - } + pub fn custom_schema_for(settings: gen::SchemaSettings) -> schema::Schema { + T::json_schema(&mut gen::SchemaGenerator::new(settings)) + } - pub fn schema_object(schema: schema::Schema) -> schema::SchemaObject { - match schema { - schema::Schema::Object(o) => o, - s => panic!("Schema was not an object: {:?}", s), - } + pub fn schema_object(schema: schema::Schema) -> schema::SchemaObject { + match schema { + schema::Schema::Object(o) => o, + s => panic!("Schema was not an object: {:?}", s), } + } } diff --git a/schemars_derive/Cargo.toml b/schemars_derive/Cargo.toml index b31667c..8bb716b 100644 --- a/schemars_derive/Cargo.toml +++ b/schemars_derive/Cargo.toml @@ -2,7 +2,7 @@ name = "schemars_derive" description = "Macros for #[derive(JsonSchema)], for use with schemars" repository = "https://github.com/GREsau/schemars" -version = "0.6.0" +version = "0.6.1" authors = ["Graham Esau "] edition = "2018" license = "MIT" diff --git a/schemars_derive/src/metadata.rs b/schemars_derive/src/metadata.rs index f5bc9e9..b88f3f2 100644 --- a/schemars_derive/src/metadata.rs +++ b/schemars_derive/src/metadata.rs @@ -59,12 +59,12 @@ pub fn set_metadata_on_schema(schema_expr: TokenStream, metadata: &SchemaMetadat { let default = #default; if !#skip_if(&default) { - metadata.default = serde_json::value::to_value(default).ok(); + metadata.default = schemars::_serde_json::value::to_value(default).ok(); } } }), (Some(default), None) => setters.push(quote! { - metadata.default = serde_json::value::to_value(#default).ok(); + metadata.default = schemars::_serde_json::value::to_value(#default).ok(); }), _ => {} }