From 421d01c85f6fe3a32a0b0c4515366a12a5b08049 Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Wed, 21 Aug 2024 14:21:19 +0100 Subject: [PATCH] Remove usages of `gen` identifier `gen` is a reserved keyword in rust 2024, making it very awkward to use as a module variable name. --- docs/1.1-attributes.md | 2 +- docs/2-implementing.md | 4 +- docs/3-generating.md | 10 ++-- .../examples/custom_serialization.rs | 4 +- docs/_includes/examples/custom_settings.rs | 6 +-- docs/examples/4-custom_settings.md | 2 +- docs/index.md | 2 +- schemars/examples/custom_serialization.rs | 4 +- schemars/examples/custom_settings.rs | 6 +-- schemars/src/_private.rs | 4 +- schemars/src/{gen.rs => generate.rs} | 38 ++++++++------- schemars/src/json_schema_impls/array.rs | 6 +-- schemars/src/json_schema_impls/arrayvec07.rs | 6 +-- schemars/src/json_schema_impls/chrono04.rs | 2 +- schemars/src/json_schema_impls/core.rs | 34 +++++++------- schemars/src/json_schema_impls/decimal.rs | 2 +- schemars/src/json_schema_impls/either1.rs | 6 +-- schemars/src/json_schema_impls/ffi.rs | 8 ++-- schemars/src/json_schema_impls/maps.rs | 6 +-- schemars/src/json_schema_impls/mod.rs | 8 ++-- .../src/json_schema_impls/nonzero_signed.rs | 6 +-- .../src/json_schema_impls/nonzero_unsigned.rs | 8 ++-- schemars/src/json_schema_impls/primitives.rs | 2 +- schemars/src/json_schema_impls/semver1.rs | 2 +- schemars/src/json_schema_impls/sequences.rs | 10 ++-- schemars/src/json_schema_impls/serdejson.rs | 2 +- schemars/src/json_schema_impls/std_time.rs | 14 +++--- schemars/src/json_schema_impls/tuple.rs | 6 +-- schemars/src/json_schema_impls/url2.rs | 2 +- schemars/src/json_schema_impls/uuid1.rs | 2 +- schemars/src/lib.rs | 21 +++++++-- schemars/src/schema.rs | 2 +- schemars/src/ser.rs | 46 +++++++++---------- schemars/tests/docs.rs | 2 +- schemars/tests/from_value.rs | 18 ++++---- schemars/tests/inline_subschemas.rs | 2 +- schemars/tests/schema_settings.rs | 2 +- schemars/tests/schema_with_enum.rs | 4 +- schemars/tests/schema_with_struct.rs | 4 +- schemars/tests/util/mod.rs | 2 +- schemars_derive/src/lib.rs | 10 ++-- schemars_derive/src/schema_exprs.rs | 30 ++++++------ 42 files changed, 186 insertions(+), 171 deletions(-) rename schemars/src/{gen.rs => generate.rs} (95%) diff --git a/docs/1.1-attributes.md b/docs/1.1-attributes.md index 52ddc8a2..498eff90 100644 --- a/docs/1.1-attributes.md +++ b/docs/1.1-attributes.md @@ -267,7 +267,7 @@ Validator docs: [required](https://github.com/Keats/validator#required) / [requi -Set on a variant or field to generate this field's schema using the given function. This function must be callable as `fn(&mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema`. +Set on a variant or field to generate this field's schema using the given function. This function must be callable as `fn(&mut schemars::SchemaGenerator) -> schemars::schema::Schema`.

diff --git a/docs/2-implementing.md b/docs/2-implementing.md index cb0f18c2..06dfa0ea 100644 --- a/docs/2-implementing.md +++ b/docs/2-implementing.md @@ -60,10 +60,10 @@ The default implementation of this function returns `Self::schema_name()`. ## json_schema ```rust -fn json_schema(gen: &mut gen::SchemaGenerator) -> Schema; +fn json_schema(generator: &mut SchemaGenerator) -> Schema; ``` -This function creates the JSON schema itself. The `gen` argument can be used to check the schema generation settings, or to get schemas for other types. If you do need schemas for other types, you should call the `gen.subschema_for::()` method instead of `::json_schema(gen)`, as `subschema_for` can add `T`'s schema to the root schema's `$defs` so that it does not need to be duplicated when used more than once. +This function creates the JSON schema itself. The `generator` argument can be used to check the schema generation settings, or to get schemas for other types. If you do need schemas for other types, you should call the `generator.subschema_for::()` method instead of `::json_schema(generator)`, as `subschema_for` can add `T`'s schema to the root schema's `$defs` so that it does not need to be duplicated when used more than once. `json_schema` should not return a `$ref` schema. diff --git a/docs/3-generating.md b/docs/3-generating.md index b8cd9705..64901697 100644 --- a/docs/3-generating.md +++ b/docs/3-generating.md @@ -6,7 +6,7 @@ permalink: /generating/ # Generating Schemas -The easiest way to generate a schema for a type that implements is to use the [`schema_for!` macro](https://docs.rs/schemars/latest/schemars/macro.schema_for.html), like so: +The easiest way to generate a schema for a type that implements is to use the [`schema_for!` macro](https://docs.rs/schemars/1.0.0--latest/schemars/macro.schema_for.html), like so: ```rust let my_schema = schema_for!(MyStruct); @@ -14,10 +14,10 @@ let my_schema = schema_for!(MyStruct); This will create a schema that conforms to [JSON Schema 2020-12](https://json-schema.org/specification-links#2020-12), but this is liable to change in a future version of Schemars if support for other JSON Schema versions is added. -If you want more control over how the schema is generated, you can use the [`gen` module](https://docs.rs/schemars/latest/schemars/gen/). There are two main types in this module: +If you want more control over how the schema is generated, you can use the [`generate` module](https://docs.rs/schemars/1.0.0--latest/schemars/generate/). There are two main types in this module: -- [`SchemaSettings`](https://docs.rs/schemars/latest/schemars/gen/struct.SchemaSettings.html), which defines what JSON Schema features should be used when generating schemas (for example, how `Option`s should be represented). -- [`SchemaGenerator`](https://docs.rs/schemars/latest/schemars/gen/struct.SchemaGenerator.html), which manages the generation of a schema document. +- [`SchemaSettings`](https://docs.rs/schemars/1.0.0--latest/schemars/generate/struct.SchemaSettings.html), which defines what JSON Schema features should be used when generating schemas (for example, how `Option`s should be represented). +- [`SchemaGenerator`](https://docs.rs/schemars/1.0.0--latest/schemars/generate/struct.SchemaGenerator.html), which manages the generation of a schema document. For example, to generate a schema that conforms to [JSON Schema Draft 7](https://json-schema.org/specification-links.html#draft-7): @@ -30,7 +30,7 @@ See the API documentation for more info on how to use those types for custom sch ## Schema from Example Value -If you want a schema for a type that can't/doesn't implement `JsonSchema`, but does implement `serde::Serialize`, then you can generate a JSON schema from a value of that type using the [`schema_for_value!` macro](https://docs.rs/schemars/latest/schemars/macro.schema_for_value.html). However, this schema will generally be less precise than if the type implemented `JsonSchema` - particularly when it involves enums, since schemars will not make any assumptions about the structure of an enum based on a single variant. +If you want a schema for a type that can't/doesn't implement `JsonSchema`, but does implement `serde::Serialize`, then you can generate a JSON schema from a value of that type using the [`schema_for_value!` macro](https://docs.rs/schemars/1.0.0--latest/schemars/macro.schema_for_value.html). However, this schema will generally be less precise than if the type implemented `JsonSchema` - particularly when it involves enums, since schemars will not make any assumptions about the structure of an enum based on a single variant. ```rust let value = MyStruct { foo = 123 }; diff --git a/docs/_includes/examples/custom_serialization.rs b/docs/_includes/examples/custom_serialization.rs index c32203f0..8e2a695a 100644 --- a/docs/_includes/examples/custom_serialization.rs +++ b/docs/_includes/examples/custom_serialization.rs @@ -19,8 +19,8 @@ pub struct MyStruct { pub bool_normal: bool, } -fn make_custom_schema(gen: &mut SchemaGenerator) -> Schema { - let mut schema = String::json_schema(gen); +fn make_custom_schema(generator: &mut SchemaGenerator) -> Schema { + let mut schema = String::json_schema(generator); schema .ensure_object() .insert("format".into(), "boolean".into()); diff --git a/docs/_includes/examples/custom_settings.rs b/docs/_includes/examples/custom_settings.rs index 335daf3c..7858720f 100644 --- a/docs/_includes/examples/custom_settings.rs +++ b/docs/_includes/examples/custom_settings.rs @@ -1,4 +1,4 @@ -use schemars::{gen::SchemaSettings, JsonSchema}; +use schemars::{generate::SchemaSettings, JsonSchema}; #[derive(JsonSchema)] pub struct MyStruct { @@ -18,7 +18,7 @@ fn main() { s.option_nullable = true; s.option_add_null_type = false; }); - let gen = settings.into_generator(); - let schema = gen.into_root_schema_for::(); + let generator = settings.into_generator(); + let schema = generator.into_root_schema_for::(); println!("{}", serde_json::to_string_pretty(&schema).unwrap()); } diff --git a/docs/examples/4-custom_settings.md b/docs/examples/4-custom_settings.md index 7b85f650..f14cbe35 100644 --- a/docs/examples/4-custom_settings.md +++ b/docs/examples/4-custom_settings.md @@ -7,6 +7,6 @@ summary: Generating a schema using custom settings which changes how Option i # Custom Schema Settings -The `gen` module allows you to customise how schemas are generated. For example, the default behaviour for `Option` is to include `null` in the schema's `type`s, but we can instead add a `nullable` property to its schema: +The `generate` module allows you to customise how schemas are generated. For example, the default behaviour for `Option` is to include `null` in the schema's `type`s, but we can instead add a `nullable` property to its schema: {% include example.md name="custom_settings" %} diff --git a/docs/index.md b/docs/index.md index ad71e780..0601c216 100644 --- a/docs/index.md +++ b/docs/index.md @@ -7,7 +7,7 @@ nav_order: 1 Schemars is a library to generate JSON Schema documents from Rust data structures. -This is built on Rust's trait system - any type which implements the [`JsonSchema`](https://docs.rs/schemars/latest/schemars/trait.JsonSchema.html) trait can have a JSON Schema generated describing that type. Schemars implements this on many standard library types, and provides a derive macro to automatically implement it on custom types. +This is built on Rust's trait system - any type which implements the [`JsonSchema`](https://docs.rs/schemars/1.0.0--latest/schemars/trait.JsonSchema.html) trait can have a JSON Schema generated describing that type. Schemars implements this on many standard library types, and provides a derive macro to automatically implement it on custom types. One of the main aims of this library is compatibility with [Serde](https://github.com/serde-rs/serde). Any generated schema _should_ match how [serde_json](https://github.com/serde-rs/json) would serialize/deserialize to/from JSON. To support this, Schemars will check for any `#[serde(...)]` attributes on types that derive `JsonSchema`, and adjust the generated schema accordingly. diff --git a/schemars/examples/custom_serialization.rs b/schemars/examples/custom_serialization.rs index c32203f0..8e2a695a 100644 --- a/schemars/examples/custom_serialization.rs +++ b/schemars/examples/custom_serialization.rs @@ -19,8 +19,8 @@ pub struct MyStruct { pub bool_normal: bool, } -fn make_custom_schema(gen: &mut SchemaGenerator) -> Schema { - let mut schema = String::json_schema(gen); +fn make_custom_schema(generator: &mut SchemaGenerator) -> Schema { + let mut schema = String::json_schema(generator); schema .ensure_object() .insert("format".into(), "boolean".into()); diff --git a/schemars/examples/custom_settings.rs b/schemars/examples/custom_settings.rs index 335daf3c..7858720f 100644 --- a/schemars/examples/custom_settings.rs +++ b/schemars/examples/custom_settings.rs @@ -1,4 +1,4 @@ -use schemars::{gen::SchemaSettings, JsonSchema}; +use schemars::{generate::SchemaSettings, JsonSchema}; #[derive(JsonSchema)] pub struct MyStruct { @@ -18,7 +18,7 @@ fn main() { s.option_nullable = true; s.option_add_null_type = false; }); - let gen = settings.into_generator(); - let schema = gen.into_root_schema_for::(); + let generator = settings.into_generator(); + let schema = generator.into_root_schema_for::(); println!("{}", serde_json::to_string_pretty(&schema).unwrap()); } diff --git a/schemars/src/_private.rs b/schemars/src/_private.rs index 4d70bf35..db0df0e6 100644 --- a/schemars/src/_private.rs +++ b/schemars/src/_private.rs @@ -5,10 +5,10 @@ use serde_json::{json, map::Entry, Map, Value}; // Helper for generating schemas for flattened `Option` fields. pub fn json_schema_for_flatten( - gen: &mut SchemaGenerator, + generator: &mut SchemaGenerator, required: bool, ) -> Schema { - let mut schema = T::_schemars_private_non_optional_json_schema(gen); + let mut schema = T::_schemars_private_non_optional_json_schema(generator); if T::_schemars_private_is_option() && !required { if let Some(object) = schema.as_object_mut() { diff --git a/schemars/src/gen.rs b/schemars/src/generate.rs similarity index 95% rename from schemars/src/gen.rs rename to schemars/src/generate.rs index 98dd7b3c..a229f59b 100644 --- a/schemars/src/gen.rs +++ b/schemars/src/generate.rs @@ -127,13 +127,13 @@ impl SchemaSettings { /// /// # Example /// ``` - /// use schemars::gen::{SchemaGenerator, SchemaSettings}; + /// use schemars::generate::{SchemaGenerator, SchemaSettings}; /// /// let settings = SchemaSettings::default().with(|s| { /// s.option_nullable = true; /// s.option_add_null_type = false; /// }); - /// let gen = settings.into_generator(); + /// let generator = settings.into_generator(); /// ``` pub fn with(mut self, configure_fn: impl FnOnce(&mut Self)) -> Self { configure_fn(&mut self); @@ -163,8 +163,8 @@ impl SchemaSettings { /// foo: i32, /// } /// -/// let gen = SchemaGenerator::default(); -/// let schema = gen.into_root_schema_for::(); +/// let generator = SchemaGenerator::default(); +/// let schema = generator.into_root_schema_for::(); /// ``` #[derive(Debug, Default)] pub struct SchemaGenerator { @@ -208,8 +208,8 @@ impl SchemaGenerator { /// ``` /// use schemars::SchemaGenerator; /// - /// let gen = SchemaGenerator::default(); - /// let settings = gen.settings(); + /// let generator = SchemaGenerator::default(); + /// let settings = generator.settings(); /// /// assert_eq!(settings.option_add_null_type, true); /// ``` @@ -361,7 +361,7 @@ impl SchemaGenerator { value: &T, ) -> Result { let mut schema = value.serialize(crate::ser::Serializer { - gen: self, + generator: self, include_title: true, })?; @@ -392,7 +392,7 @@ impl SchemaGenerator { value: &T, ) -> Result { let mut schema = value.serialize(crate::ser::Serializer { - gen: &mut self, + generator: &mut self, include_title: true, })?; @@ -415,28 +415,32 @@ impl SchemaGenerator { fn json_schema_internal(&mut self, id: CowStr) -> Schema { struct PendingSchemaState<'a> { - gen: &'a mut SchemaGenerator, + generator: &'a mut SchemaGenerator, id: CowStr, did_add: bool, } impl<'a> PendingSchemaState<'a> { - fn new(gen: &'a mut SchemaGenerator, id: CowStr) -> Self { - let did_add = gen.pending_schema_ids.insert(id.clone()); - Self { gen, id, did_add } + fn new(generator: &'a mut SchemaGenerator, id: CowStr) -> Self { + let did_add = generator.pending_schema_ids.insert(id.clone()); + Self { + generator, + id, + did_add, + } } } impl Drop for PendingSchemaState<'_> { fn drop(&mut self) { if self.did_add { - self.gen.pending_schema_ids.remove(&self.id); + self.generator.pending_schema_ids.remove(&self.id); } } } let pss = PendingSchemaState::new(self, id); - T::json_schema(pss.gen) + T::json_schema(pss.generator) } fn add_definitions( @@ -514,7 +518,7 @@ fn json_pointer_mut<'a>( /// # Example /// ``` /// use schemars::transform::Transform; -/// use schemars::gen::GenTransform; +/// use schemars::generate::GenTransform; /// /// #[derive(Debug, Clone)] /// struct MyTransform; @@ -534,7 +538,7 @@ pub trait GenTransform: Transform + DynClone + Any + Send { /// # Example /// To remove a specific transform from an instance of `SchemaSettings`: /// ``` - /// use schemars::gen::SchemaSettings; + /// use schemars::generate::SchemaSettings; /// use schemars::transform::ReplaceBoolSchemas; /// /// let mut settings = SchemaSettings::openapi3(); @@ -553,7 +557,7 @@ pub trait GenTransform: Transform + DynClone + Any + Send { /// # Example /// To modify a specific transform in an instance of `SchemaSettings`: /// ``` - /// use schemars::gen::SchemaSettings; + /// use schemars::generate::SchemaSettings; /// use schemars::transform::ReplaceBoolSchemas; /// /// let mut settings = SchemaSettings::openapi3(); diff --git a/schemars/src/json_schema_impls/array.rs b/schemars/src/json_schema_impls/array.rs index 18e8b768..c17fbba1 100644 --- a/schemars/src/json_schema_impls/array.rs +++ b/schemars/src/json_schema_impls/array.rs @@ -1,5 +1,5 @@ +use crate::SchemaGenerator; use crate::_alloc_prelude::*; -use crate::gen::SchemaGenerator; use crate::{json_schema, JsonSchema, Schema}; use alloc::borrow::Cow; @@ -37,10 +37,10 @@ macro_rules! array_impls { format!("[{}; {}]", $len, T::schema_id()).into() } - fn json_schema(gen: &mut SchemaGenerator) -> Schema { + fn json_schema(generator: &mut SchemaGenerator) -> Schema { json_schema!({ "type": "array", - "items": serde_json::Value::from(gen.subschema_for::()), + "items": serde_json::Value::from(generator.subschema_for::()), "minItems": $len, "maxItems": $len, }) diff --git a/schemars/src/json_schema_impls/arrayvec07.rs b/schemars/src/json_schema_impls/arrayvec07.rs index a5511abc..0d7270ff 100644 --- a/schemars/src/json_schema_impls/arrayvec07.rs +++ b/schemars/src/json_schema_impls/arrayvec07.rs @@ -1,5 +1,5 @@ +use crate::SchemaGenerator; use crate::_alloc_prelude::*; -use crate::gen::SchemaGenerator; use crate::{json_schema, JsonSchema, Schema}; use arrayvec07::{ArrayString, ArrayVec}; @@ -17,10 +17,10 @@ where format!("Array_up_to_size_{}_of_{}", CAP, T::schema_name()).into() } - fn json_schema(gen: &mut SchemaGenerator) -> Schema { + fn json_schema(generator: &mut SchemaGenerator) -> Schema { json_schema!({ "type": "array", - "items": gen.subschema_for::(), + "items": generator.subschema_for::(), "maxItems": CAP }) } diff --git a/schemars/src/json_schema_impls/chrono04.rs b/schemars/src/json_schema_impls/chrono04.rs index 994d4b88..a153aca3 100644 --- a/schemars/src/json_schema_impls/chrono04.rs +++ b/schemars/src/json_schema_impls/chrono04.rs @@ -1,4 +1,4 @@ -use crate::gen::SchemaGenerator; +use crate::SchemaGenerator; use crate::{json_schema, JsonSchema, Schema}; use alloc::borrow::Cow; use chrono04::prelude::*; diff --git a/schemars/src/json_schema_impls/core.rs b/schemars/src/json_schema_impls/core.rs index 4e2c9d6f..07fbce53 100644 --- a/schemars/src/json_schema_impls/core.rs +++ b/schemars/src/json_schema_impls/core.rs @@ -1,5 +1,5 @@ +use crate::SchemaGenerator; use crate::_alloc_prelude::*; -use crate::gen::SchemaGenerator; use crate::{json_schema, JsonSchema, Schema}; use alloc::borrow::Cow; use core::ops::{Bound, Range, RangeInclusive}; @@ -16,10 +16,10 @@ impl JsonSchema for Option { format!("Option<{}>", T::schema_id()).into() } - fn json_schema(gen: &mut SchemaGenerator) -> Schema { - let mut schema = gen.subschema_for::(); + fn json_schema(generator: &mut SchemaGenerator) -> Schema { + let mut schema = generator.subschema_for::(); - if gen.settings().option_add_null_type { + if generator.settings().option_add_null_type { schema = match schema.try_to_object() { Ok(mut obj) => { let instance_type = obj.get_mut("type"); @@ -43,17 +43,17 @@ impl JsonSchema for Option { _ => json_schema!({ "anyOf": [ obj, - <()>::json_schema(gen) + <()>::json_schema(generator) ] }), } } Err(true) => true.into(), - Err(false) => <()>::json_schema(gen), + Err(false) => <()>::json_schema(generator), } } - if gen.settings().option_nullable { + if generator.settings().option_nullable { schema .ensure_object() .insert("nullable".into(), true.into()); @@ -62,8 +62,8 @@ impl JsonSchema for Option { schema } - fn _schemars_private_non_optional_json_schema(gen: &mut SchemaGenerator) -> Schema { - T::_schemars_private_non_optional_json_schema(gen) + fn _schemars_private_non_optional_json_schema(generator: &mut SchemaGenerator) -> Schema { + T::_schemars_private_non_optional_json_schema(generator) } fn _schemars_private_is_option() -> bool { @@ -80,20 +80,20 @@ impl JsonSchema for Result { format!("Result<{}, {}>", T::schema_id(), E::schema_id()).into() } - fn json_schema(gen: &mut SchemaGenerator) -> Schema { + fn json_schema(generator: &mut SchemaGenerator) -> Schema { json_schema!({ "oneOf": [ { "type": "object", "properties": { - "Ok": gen.subschema_for::() + "Ok": generator.subschema_for::() }, "required": ["Ok"] }, { "type": "object", "properties": { - "Err": gen.subschema_for::() + "Err": generator.subschema_for::() }, "required": ["Err"] } @@ -111,20 +111,20 @@ impl JsonSchema for Bound { format!("Bound<{}>", T::schema_id()).into() } - fn json_schema(gen: &mut SchemaGenerator) -> Schema { + fn json_schema(generator: &mut SchemaGenerator) -> Schema { json_schema!({ "oneOf": [ { "type": "object", "properties": { - "Included": gen.subschema_for::() + "Included": generator.subschema_for::() }, "required": ["Included"] }, { "type": "object", "properties": { - "Excluded": gen.subschema_for::() + "Excluded": generator.subschema_for::() }, "required": ["Excluded"] }, @@ -146,8 +146,8 @@ impl JsonSchema for Range { format!("Range<{}>", T::schema_id()).into() } - fn json_schema(gen: &mut SchemaGenerator) -> Schema { - let subschema = gen.subschema_for::(); + fn json_schema(generator: &mut SchemaGenerator) -> Schema { + let subschema = generator.subschema_for::(); json_schema!({ "type": "object", "properties": { diff --git a/schemars/src/json_schema_impls/decimal.rs b/schemars/src/json_schema_impls/decimal.rs index 0f55ba9c..afba4abe 100644 --- a/schemars/src/json_schema_impls/decimal.rs +++ b/schemars/src/json_schema_impls/decimal.rs @@ -1,4 +1,4 @@ -use crate::gen::SchemaGenerator; +use crate::SchemaGenerator; use crate::{json_schema, JsonSchema, Schema}; use alloc::borrow::Cow; diff --git a/schemars/src/json_schema_impls/either1.rs b/schemars/src/json_schema_impls/either1.rs index 3822faaf..ba8a7064 100644 --- a/schemars/src/json_schema_impls/either1.rs +++ b/schemars/src/json_schema_impls/either1.rs @@ -1,5 +1,5 @@ use crate::_alloc_prelude::*; -use crate::gen::SchemaGenerator; +use crate::SchemaGenerator; use crate::{json_schema, JsonSchema, Schema}; use alloc::borrow::Cow; use either1::Either; @@ -15,9 +15,9 @@ impl JsonSchema for Either { format!("either::Either<{}, {}>", L::schema_id(), R::schema_id()).into() } - fn json_schema(gen: &mut SchemaGenerator) -> Schema { + fn json_schema(generator: &mut SchemaGenerator) -> Schema { json_schema!({ - "anyOf": [gen.subschema_for::(), gen.subschema_for::()], + "anyOf": [generator.subschema_for::(), generator.subschema_for::()], }) } } diff --git a/schemars/src/json_schema_impls/ffi.rs b/schemars/src/json_schema_impls/ffi.rs index 524c49f3..cf2efe5a 100644 --- a/schemars/src/json_schema_impls/ffi.rs +++ b/schemars/src/json_schema_impls/ffi.rs @@ -1,5 +1,5 @@ +use crate::SchemaGenerator; use crate::_alloc_prelude::*; -use crate::gen::SchemaGenerator; use crate::{json_schema, JsonSchema, Schema}; use alloc::borrow::Cow; use std::ffi::{CStr, CString, OsStr, OsString}; @@ -13,20 +13,20 @@ impl JsonSchema for OsString { "std::ffi::OsString".into() } - fn json_schema(gen: &mut SchemaGenerator) -> Schema { + fn json_schema(generator: &mut SchemaGenerator) -> Schema { json_schema!({ "oneOf": [ { "type": "object", "properties": { - "Unix": >::json_schema(gen) + "Unix": >::json_schema(generator) }, "required": ["Unix"] }, { "type": "object", "properties": { - "Windows": >::json_schema(gen) + "Windows": >::json_schema(generator) }, "required": ["Windows"] }, diff --git a/schemars/src/json_schema_impls/maps.rs b/schemars/src/json_schema_impls/maps.rs index af43a438..f199f4a6 100644 --- a/schemars/src/json_schema_impls/maps.rs +++ b/schemars/src/json_schema_impls/maps.rs @@ -1,5 +1,5 @@ use crate::_alloc_prelude::*; -use crate::gen::SchemaGenerator; +use crate::SchemaGenerator; use crate::{json_schema, JsonSchema, Schema}; use alloc::borrow::Cow; @@ -19,10 +19,10 @@ macro_rules! map_impl { format!("Map<{}>", V::schema_id()).into() } - fn json_schema(gen: &mut SchemaGenerator) -> Schema { + fn json_schema(generator: &mut SchemaGenerator) -> Schema { json_schema!({ "type": "object", - "additionalProperties": gen.subschema_for::(), + "additionalProperties": generator.subschema_for::(), }) } } diff --git a/schemars/src/json_schema_impls/mod.rs b/schemars/src/json_schema_impls/mod.rs index a0287f7c..1921a6ae 100644 --- a/schemars/src/json_schema_impls/mod.rs +++ b/schemars/src/json_schema_impls/mod.rs @@ -21,12 +21,12 @@ macro_rules! forward_impl { <$target>::schema_id() } - fn json_schema(gen: &mut $crate::gen::SchemaGenerator) -> $crate::Schema { - <$target>::json_schema(gen) + fn json_schema(generator: &mut $crate::SchemaGenerator) -> $crate::Schema { + <$target>::json_schema(generator) } - fn _schemars_private_non_optional_json_schema(gen: &mut $crate::gen::SchemaGenerator) -> $crate::Schema { - <$target>::_schemars_private_non_optional_json_schema(gen) + fn _schemars_private_non_optional_json_schema(generator: &mut $crate::SchemaGenerator) -> $crate::Schema { + <$target>::_schemars_private_non_optional_json_schema(generator) } fn _schemars_private_is_option() -> bool { diff --git a/schemars/src/json_schema_impls/nonzero_signed.rs b/schemars/src/json_schema_impls/nonzero_signed.rs index 37fef863..c5b3e84c 100644 --- a/schemars/src/json_schema_impls/nonzero_signed.rs +++ b/schemars/src/json_schema_impls/nonzero_signed.rs @@ -1,5 +1,5 @@ +use crate::SchemaGenerator; use crate::_alloc_prelude::*; -use crate::gen::SchemaGenerator; use crate::{JsonSchema, Schema}; use alloc::borrow::Cow; use core::num::*; @@ -17,8 +17,8 @@ macro_rules! nonzero_unsigned_impl { stringify!(std::num::$type).into() } - fn json_schema(gen: &mut SchemaGenerator) -> Schema { - let mut schema = <$primitive>::json_schema(gen); + fn json_schema(generator: &mut SchemaGenerator) -> Schema { + let mut schema = <$primitive>::json_schema(generator); let object = schema.ensure_object(); object.insert("not".to_owned(), serde_json::json!({ "const": 0 diff --git a/schemars/src/json_schema_impls/nonzero_unsigned.rs b/schemars/src/json_schema_impls/nonzero_unsigned.rs index 8c07fb3a..c6711abc 100644 --- a/schemars/src/json_schema_impls/nonzero_unsigned.rs +++ b/schemars/src/json_schema_impls/nonzero_unsigned.rs @@ -1,7 +1,7 @@ -use crate::_alloc_prelude::*; -use crate::gen::SchemaGenerator; use crate::JsonSchema; use crate::Schema; +use crate::SchemaGenerator; +use crate::_alloc_prelude::*; use alloc::borrow::Cow; use core::num::*; @@ -18,8 +18,8 @@ macro_rules! nonzero_unsigned_impl { stringify!(std::num::$type).into() } - fn json_schema(gen: &mut SchemaGenerator) -> Schema { - let mut schema = <$primitive>::json_schema(gen); + fn json_schema(generator: &mut SchemaGenerator) -> Schema { + let mut schema = <$primitive>::json_schema(generator); let object = schema.ensure_object(); object.insert("minimum".to_owned(), 1.into()); schema diff --git a/schemars/src/json_schema_impls/primitives.rs b/schemars/src/json_schema_impls/primitives.rs index 4b87961e..c8d509cc 100644 --- a/schemars/src/json_schema_impls/primitives.rs +++ b/schemars/src/json_schema_impls/primitives.rs @@ -1,5 +1,5 @@ use crate::_alloc_prelude::*; -use crate::gen::SchemaGenerator; +use crate::SchemaGenerator; use crate::{json_schema, JsonSchema, Schema}; use alloc::borrow::Cow; diff --git a/schemars/src/json_schema_impls/semver1.rs b/schemars/src/json_schema_impls/semver1.rs index 43e0f3a8..1869fa92 100644 --- a/schemars/src/json_schema_impls/semver1.rs +++ b/schemars/src/json_schema_impls/semver1.rs @@ -1,4 +1,4 @@ -use crate::gen::SchemaGenerator; +use crate::SchemaGenerator; use crate::{json_schema, JsonSchema, Schema}; use alloc::borrow::Cow; use semver1::Version; diff --git a/schemars/src/json_schema_impls/sequences.rs b/schemars/src/json_schema_impls/sequences.rs index 8241dac9..4af62a2a 100644 --- a/schemars/src/json_schema_impls/sequences.rs +++ b/schemars/src/json_schema_impls/sequences.rs @@ -1,5 +1,5 @@ +use crate::SchemaGenerator; use crate::_alloc_prelude::*; -use crate::gen::SchemaGenerator; use crate::{json_schema, JsonSchema, Schema}; use alloc::borrow::Cow; @@ -19,10 +19,10 @@ macro_rules! seq_impl { format!("[{}]", T::schema_id()).into() } - fn json_schema(gen: &mut SchemaGenerator) -> Schema { + fn json_schema(generator: &mut SchemaGenerator) -> Schema { json_schema!({ "type": "array", - "items": gen.subschema_for::(), + "items": generator.subschema_for::(), }) } } @@ -45,11 +45,11 @@ macro_rules! set_impl { format!("Set<{}>", T::schema_id()).into() } - fn json_schema(gen: &mut SchemaGenerator) -> Schema { + fn json_schema(generator: &mut SchemaGenerator) -> Schema { json_schema!({ "type": "array", "uniqueItems": true, - "items": gen.subschema_for::(), + "items": generator.subschema_for::(), }) } } diff --git a/schemars/src/json_schema_impls/serdejson.rs b/schemars/src/json_schema_impls/serdejson.rs index 25f6e785..e062b37b 100644 --- a/schemars/src/json_schema_impls/serdejson.rs +++ b/schemars/src/json_schema_impls/serdejson.rs @@ -1,5 +1,5 @@ use crate::_alloc_prelude::*; -use crate::gen::SchemaGenerator; +use crate::SchemaGenerator; use crate::{json_schema, JsonSchema, Schema}; use alloc::borrow::Cow; use alloc::collections::BTreeMap; diff --git a/schemars/src/json_schema_impls/std_time.rs b/schemars/src/json_schema_impls/std_time.rs index 1766e937..22b3fd9f 100644 --- a/schemars/src/json_schema_impls/std_time.rs +++ b/schemars/src/json_schema_impls/std_time.rs @@ -1,4 +1,4 @@ -use crate::gen::SchemaGenerator; +use crate::SchemaGenerator; use crate::{json_schema, JsonSchema, Schema}; use alloc::borrow::Cow; @@ -11,13 +11,13 @@ impl JsonSchema for core::time::Duration { "std::time::Duration".into() } - fn json_schema(gen: &mut SchemaGenerator) -> Schema { + fn json_schema(generator: &mut SchemaGenerator) -> Schema { json_schema!({ "type": "object", "required": ["secs", "nanos"], "properties": { - "secs": u64::json_schema(gen), - "nanos": u32::json_schema(gen), + "secs": u64::json_schema(generator), + "nanos": u32::json_schema(generator), } }) } @@ -33,13 +33,13 @@ impl JsonSchema for std::time::SystemTime { "std::time::SystemTime".into() } - fn json_schema(gen: &mut SchemaGenerator) -> Schema { + fn json_schema(generator: &mut SchemaGenerator) -> Schema { json_schema!({ "type": "object", "required": ["secs_since_epoch", "nanos_since_epoch"], "properties": { - "secs_since_epoch": u64::json_schema(gen), - "nanos_since_epoch": u32::json_schema(gen), + "secs_since_epoch": u64::json_schema(generator), + "nanos_since_epoch": u32::json_schema(generator), } }) } diff --git a/schemars/src/json_schema_impls/tuple.rs b/schemars/src/json_schema_impls/tuple.rs index bb57c0ea..88cc0fff 100644 --- a/schemars/src/json_schema_impls/tuple.rs +++ b/schemars/src/json_schema_impls/tuple.rs @@ -1,5 +1,5 @@ +use crate::SchemaGenerator; use crate::_alloc_prelude::*; -use crate::gen::SchemaGenerator; use crate::{json_schema, JsonSchema, Schema}; use alloc::borrow::Cow; @@ -23,11 +23,11 @@ macro_rules! tuple_impls { id.into() } - fn json_schema(gen: &mut SchemaGenerator) -> Schema { + fn json_schema(generator: &mut SchemaGenerator) -> Schema { json_schema!({ "type": "array", "prefixItems": [ - $(gen.subschema_for::<$name>()),+ + $(generator.subschema_for::<$name>()),+ ], "minItems": $len, "maxItems": $len, diff --git a/schemars/src/json_schema_impls/url2.rs b/schemars/src/json_schema_impls/url2.rs index d34e949d..6fcaceb9 100644 --- a/schemars/src/json_schema_impls/url2.rs +++ b/schemars/src/json_schema_impls/url2.rs @@ -1,4 +1,4 @@ -use crate::gen::SchemaGenerator; +use crate::SchemaGenerator; use crate::{json_schema, JsonSchema, Schema}; use alloc::borrow::Cow; use url2::Url; diff --git a/schemars/src/json_schema_impls/uuid1.rs b/schemars/src/json_schema_impls/uuid1.rs index 2628f00b..677c279d 100644 --- a/schemars/src/json_schema_impls/uuid1.rs +++ b/schemars/src/json_schema_impls/uuid1.rs @@ -1,4 +1,4 @@ -use crate::gen::SchemaGenerator; +use crate::SchemaGenerator; use crate::{json_schema, JsonSchema, Schema}; use alloc::borrow::Cow; use uuid1::Uuid; diff --git a/schemars/src/lib.rs b/schemars/src/lib.rs index a556182d..252609ca 100644 --- a/schemars/src/lib.rs +++ b/schemars/src/lib.rs @@ -24,7 +24,7 @@ mod macros; /// outside of `schemars`, and should not be considered part of the public API. #[doc(hidden)] pub mod _private; -pub mod gen; +pub mod generate; pub mod transform; #[cfg(feature = "schemars_derive")] @@ -40,7 +40,7 @@ pub extern crate alloc as _alloc; #[doc(hidden)] pub extern crate serde_json as _serde_json; -pub use gen::SchemaGenerator; +pub use generate::SchemaGenerator; pub use schema::Schema; mod _alloc_prelude { @@ -52,6 +52,17 @@ mod _alloc_prelude { pub use alloc::vec::Vec; } +#[deprecated = "Only included for backward-compatibility - use the `schemars::generate` module instead."] +#[doc(hidden)] +pub mod r#gen { + #[deprecated = "Only included for backward-compatibility - use `schemars::SchemaGenerator` or `schemars::generate::SchemaGenerator` instead."] + pub type SchemaGenerator = crate::generate::SchemaGenerator; + #[deprecated = "Only included for backward-compatibility - use `schemars::generate::SchemaSettings` instead."] + pub type SchemaSettings = crate::generate::SchemaSettings; + #[deprecated = "Only included for backward-compatibility - use `schemars::generate::GenTransform` instead."] + pub use crate::generate::GenTransform; +} + /// A type which can be described as a JSON Schema document. /// /// This is implemented for many Rust primitive and standard library types. @@ -166,12 +177,12 @@ pub trait JsonSchema { /// add them to the [`SchemaGenerator`]'s schema definitions. /// /// This should not return a `$ref` schema. - fn json_schema(gen: &mut SchemaGenerator) -> Schema; + fn json_schema(generator: &mut SchemaGenerator) -> Schema; // TODO document and bring into public API? #[doc(hidden)] - fn _schemars_private_non_optional_json_schema(gen: &mut SchemaGenerator) -> Schema { - Self::json_schema(gen) + fn _schemars_private_non_optional_json_schema(generator: &mut SchemaGenerator) -> Schema { + Self::json_schema(generator) } // TODO document and bring into public API? diff --git a/schemars/src/schema.rs b/schemars/src/schema.rs index 5b7dcd8c..96be6624 100644 --- a/schemars/src/schema.rs +++ b/schemars/src/schema.rs @@ -242,7 +242,7 @@ impl crate::JsonSchema for Schema { "schemars::Schema".into() } - fn json_schema(_: &mut crate::gen::SchemaGenerator) -> Schema { + fn json_schema(_: &mut crate::SchemaGenerator) -> Schema { crate::json_schema!({ "type": ["object", "boolean"] }) diff --git a/schemars/src/ser.rs b/schemars/src/ser.rs index 0aefc6b6..4b4117f2 100644 --- a/schemars/src/ser.rs +++ b/schemars/src/ser.rs @@ -4,23 +4,23 @@ use core::fmt::Display; use serde_json::{Error, Map, Value}; pub(crate) struct Serializer<'a> { - pub(crate) gen: &'a mut SchemaGenerator, + pub(crate) generator: &'a mut SchemaGenerator, pub(crate) include_title: bool, } pub(crate) struct SerializeSeq<'a> { - gen: &'a mut SchemaGenerator, + generator: &'a mut SchemaGenerator, items: Option, } pub(crate) struct SerializeTuple<'a> { - gen: &'a mut SchemaGenerator, + generator: &'a mut SchemaGenerator, items: Vec, title: &'static str, } pub(crate) struct SerializeMap<'a> { - gen: &'a mut SchemaGenerator, + generator: &'a mut SchemaGenerator, properties: Map, current_key: Option, title: &'static str, @@ -29,7 +29,7 @@ pub(crate) struct SerializeMap<'a> { macro_rules! forward_to_subschema_for { ($fn:ident, $ty:ty) => { fn $fn(self, _value: $ty) -> Result { - Ok(self.gen.subschema_for::<$ty>()) + Ok(self.generator.subschema_for::<$ty>()) } }; } @@ -78,7 +78,7 @@ impl<'a> serde::Serializer for Serializer<'a> { where T: Display, { - Ok(self.gen.subschema_for::<&str>()) + Ok(self.generator.subschema_for::<&str>()) } fn collect_map(self, iter: I) -> Result @@ -95,7 +95,7 @@ impl<'a> serde::Serializer for Serializer<'a> { } let schema = v.serialize(Serializer { - gen: self.gen, + generator: self.generator, include_title: false, })?; Ok(match &acc { @@ -113,7 +113,7 @@ impl<'a> serde::Serializer for Serializer<'a> { } fn serialize_none(self) -> Result { - Ok(self.gen.subschema_for::>()) + Ok(self.generator.subschema_for::>()) } fn serialize_unit(self) -> Result { @@ -125,11 +125,11 @@ impl<'a> serde::Serializer for Serializer<'a> { T: serde::Serialize, { let mut schema = value.serialize(Serializer { - gen: self.gen, + generator: self.generator, include_title: false, })?; - if self.gen.settings().option_add_null_type { + if self.generator.settings().option_add_null_type { schema = match schema.try_to_object() { Ok(mut obj) => { let value = obj.get_mut("type"); @@ -153,17 +153,17 @@ impl<'a> serde::Serializer for Serializer<'a> { _ => json_schema!({ "anyOf": [ obj, - <()>::json_schema(self.gen) + <()>::json_schema(self.generator) ] }), } } Err(true) => true.into(), - Err(false) => <()>::json_schema(self.gen), + Err(false) => <()>::json_schema(self.generator), } } - if self.gen.settings().option_nullable { + if self.generator.settings().option_nullable { schema .ensure_object() .insert("nullable".into(), true.into()); @@ -173,7 +173,7 @@ impl<'a> serde::Serializer for Serializer<'a> { } fn serialize_unit_struct(self, _name: &'static str) -> Result { - Ok(self.gen.subschema_for::<()>()) + Ok(self.generator.subschema_for::<()>()) } fn serialize_unit_variant( @@ -218,14 +218,14 @@ impl<'a> serde::Serializer for Serializer<'a> { fn serialize_seq(self, _len: Option) -> Result { Ok(SerializeSeq { - gen: self.gen, + generator: self.generator, items: None, }) } fn serialize_tuple(self, len: usize) -> Result { Ok(SerializeTuple { - gen: self.gen, + generator: self.generator, items: Vec::with_capacity(len), title: "", }) @@ -238,7 +238,7 @@ impl<'a> serde::Serializer for Serializer<'a> { ) -> Result { let title = if self.include_title { name } else { "" }; Ok(SerializeTuple { - gen: self.gen, + generator: self.generator, items: Vec::with_capacity(len), title, }) @@ -256,7 +256,7 @@ impl<'a> serde::Serializer for Serializer<'a> { fn serialize_map(self, _len: Option) -> Result { Ok(SerializeMap { - gen: self.gen, + generator: self.generator, properties: Map::new(), current_key: None, title: "", @@ -270,7 +270,7 @@ impl<'a> serde::Serializer for Serializer<'a> { ) -> Result { let title = if self.include_title { name } else { "" }; Ok(SerializeMap { - gen: self.gen, + generator: self.generator, properties: Map::new(), current_key: None, title, @@ -334,7 +334,7 @@ impl serde::ser::SerializeSeq for SerializeSeq<'_> { { if self.items != Some(true.into()) { let schema = value.serialize(Serializer { - gen: self.gen, + generator: self.generator, include_title: false, })?; match &self.items { @@ -369,7 +369,7 @@ impl serde::ser::SerializeTuple for SerializeTuple<'_> { T: serde::Serialize, { let schema = value.serialize(Serializer { - gen: self.gen, + generator: self.generator, include_title: false, })?; self.items.push(schema); @@ -437,7 +437,7 @@ impl serde::ser::SerializeMap for SerializeMap<'_> { { let key = self.current_key.take().unwrap_or_default(); let schema = value.serialize(Serializer { - gen: self.gen, + generator: self.generator, include_title: false, })?; self.properties.insert(key, schema.into()); @@ -474,7 +474,7 @@ impl serde::ser::SerializeStruct for SerializeMap<'_> { T: serde::Serialize, { let prop_schema = value.serialize(Serializer { - gen: self.gen, + generator: self.generator, include_title: false, })?; self.properties.insert(key.to_string(), prop_schema.into()); diff --git a/schemars/tests/docs.rs b/schemars/tests/docs.rs index 7d1d6d89..c46cb896 100644 --- a/schemars/tests/docs.rs +++ b/schemars/tests/docs.rs @@ -1,5 +1,5 @@ mod util; -use schemars::{gen::SchemaSettings, JsonSchema}; +use schemars::{generate::SchemaSettings, JsonSchema}; use util::*; #[allow(dead_code)] diff --git a/schemars/tests/from_value.rs b/schemars/tests/from_value.rs index 3009933b..0934cfd5 100644 --- a/schemars/tests/from_value.rs +++ b/schemars/tests/from_value.rs @@ -1,5 +1,5 @@ mod util; -use schemars::gen::{SchemaGenerator, SchemaSettings}; +use schemars::generate::{SchemaGenerator, SchemaSettings}; use serde::Serialize; use std::collections::HashMap; use util::*; @@ -53,32 +53,32 @@ fn make_value() -> MyStruct { #[test] fn schema_from_value_matches_draft07() -> TestResult { - let gen = SchemaSettings::draft07().into_generator(); - let actual = gen.into_root_schema_for_value(&make_value())?; + let generator = SchemaSettings::draft07().into_generator(); + let actual = generator.into_root_schema_for_value(&make_value())?; test_schema(&actual, "from_value_draft07") } #[test] fn schema_from_value_matches_2019_09() -> TestResult { - let gen = SchemaSettings::draft2019_09().into_generator(); - let actual = gen.into_root_schema_for_value(&make_value())?; + let generator = SchemaSettings::draft2019_09().into_generator(); + let actual = generator.into_root_schema_for_value(&make_value())?; test_schema(&actual, "from_value_2019_09") } #[test] fn schema_from_value_matches_openapi3() -> TestResult { - let gen = SchemaSettings::openapi3().into_generator(); - let actual = gen.into_root_schema_for_value(&make_value())?; + let generator = SchemaSettings::openapi3().into_generator(); + let actual = generator.into_root_schema_for_value(&make_value())?; test_schema(&actual, "from_value_openapi3") } #[test] fn schema_from_json_value() -> TestResult { - let gen = SchemaGenerator::default(); - let actual = gen.into_root_schema_for_value(&serde_json::json!({ + let generator = SchemaGenerator::default(); + let actual = generator.into_root_schema_for_value(&serde_json::json!({ "zero": 0, "one": 1, "minusOne": -1, diff --git a/schemars/tests/inline_subschemas.rs b/schemars/tests/inline_subschemas.rs index 40bae8fc..a377847f 100644 --- a/schemars/tests/inline_subschemas.rs +++ b/schemars/tests/inline_subschemas.rs @@ -1,5 +1,5 @@ mod util; -use schemars::gen::SchemaSettings; +use schemars::generate::SchemaSettings; use schemars::JsonSchema; use util::*; diff --git a/schemars/tests/schema_settings.rs b/schemars/tests/schema_settings.rs index 0c8741ce..877a77c3 100644 --- a/schemars/tests/schema_settings.rs +++ b/schemars/tests/schema_settings.rs @@ -1,5 +1,5 @@ mod util; -use schemars::gen::SchemaSettings; +use schemars::generate::SchemaSettings; use schemars::{JsonSchema, Schema}; use serde_json::Value; use std::collections::BTreeMap; diff --git a/schemars/tests/schema_with_enum.rs b/schemars/tests/schema_with_enum.rs index 5cf419c4..df9ab090 100644 --- a/schemars/tests/schema_with_enum.rs +++ b/schemars/tests/schema_with_enum.rs @@ -2,8 +2,8 @@ mod util; use schemars::JsonSchema; use util::*; -fn schema_fn(gen: &mut schemars::SchemaGenerator) -> schemars::Schema { - ::json_schema(gen) +fn schema_fn(generator: &mut schemars::SchemaGenerator) -> schemars::Schema { + ::json_schema(generator) } #[derive(Debug)] diff --git a/schemars/tests/schema_with_struct.rs b/schemars/tests/schema_with_struct.rs index 5fc8c370..86776271 100644 --- a/schemars/tests/schema_with_struct.rs +++ b/schemars/tests/schema_with_struct.rs @@ -2,8 +2,8 @@ mod util; use schemars::JsonSchema; use util::*; -fn schema_fn(gen: &mut schemars::SchemaGenerator) -> schemars::Schema { - ::json_schema(gen) +fn schema_fn(generator: &mut schemars::SchemaGenerator) -> schemars::Schema { + ::json_schema(generator) } struct DoesntImplementJsonSchema; diff --git a/schemars/tests/util/mod.rs b/schemars/tests/util/mod.rs index 2c735ef7..ff67df65 100644 --- a/schemars/tests/util/mod.rs +++ b/schemars/tests/util/mod.rs @@ -1,5 +1,5 @@ use pretty_assertions::assert_eq; -use schemars::{gen::SchemaSettings, schema_for, JsonSchema, Schema}; +use schemars::{generate::SchemaSettings, schema_for, JsonSchema, Schema}; use std::error::Error; use std::format; use std::fs; diff --git a/schemars_derive/src/lib.rs b/schemars_derive/src/lib.rs index 1e9de01b..32a5d9f2 100644 --- a/schemars_derive/src/lib.rs +++ b/schemars_derive/src/lib.rs @@ -68,12 +68,12 @@ fn derive_json_schema(mut input: syn::DeriveInput, repr: bool) -> syn::Result::schema_id() } - fn json_schema(gen: &mut schemars::SchemaGenerator) -> schemars::Schema { - <#ty as schemars::JsonSchema>::json_schema(gen) + fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema { + <#ty as schemars::JsonSchema>::json_schema(generator) } - fn _schemars_private_non_optional_json_schema(gen: &mut schemars::SchemaGenerator) -> schemars::Schema { - <#ty as schemars::JsonSchema>::_schemars_private_non_optional_json_schema(gen) + fn _schemars_private_non_optional_json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema { + <#ty as schemars::JsonSchema>::_schemars_private_non_optional_json_schema(generator) } fn _schemars_private_is_option() -> bool { @@ -186,7 +186,7 @@ fn derive_json_schema(mut input: syn::DeriveInput, repr: bool) -> syn::Result schemars::Schema { + fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema { #schema_expr } }; diff --git a/schemars_derive/src/schema_exprs.rs b/schemars_derive/src/schema_exprs.rs index 58aadedc..5b1eb977 100644 --- a/schemars_derive/src/schema_exprs.rs +++ b/schemars_derive/src/schema_exprs.rs @@ -70,19 +70,19 @@ pub fn expr_for_repr(cont: &Container) -> Result { fn expr_for_field(field: &Field, allow_ref: bool) -> TokenStream { let (ty, type_def) = type_for_field_schema(field); let span = field.original.span(); - let gen = quote!(gen); + let generator = quote!(generator); let mut schema_expr = if field.validation_attrs.required() { quote_spanned! {span=> - <#ty as schemars::JsonSchema>::_schemars_private_non_optional_json_schema(#gen) + <#ty as schemars::JsonSchema>::_schemars_private_non_optional_json_schema(#generator) } } else if allow_ref { quote_spanned! {span=> - #gen.subschema_for::<#ty>() + #generator.subschema_for::<#ty>() } } else { quote_spanned! {span=> - <#ty as schemars::JsonSchema>::json_schema(#gen) + <#ty as schemars::JsonSchema>::json_schema(#generator) } }; @@ -127,8 +127,8 @@ fn type_for_schema(with_attr: &WithAttr) -> (syn::Type, Option) { )) } - fn json_schema(gen: &mut schemars::SchemaGenerator) -> schemars::Schema { - #fun(gen) + fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema { + #fun(generator) } } }; @@ -361,9 +361,9 @@ fn variant_subschemas(unique: bool, schemas: Vec) -> TokenStream { fn expr_for_untagged_enum_variant(variant: &Variant, deny_unknown_fields: bool) -> TokenStream { if let Some(with_attr) = &variant.attrs.with { let (ty, type_def) = type_for_schema(with_attr); - let gen = quote!(gen); + let generator = quote!(generator); let mut schema_expr = quote_spanned! {variant.original.span()=> - #gen.subschema_for::<#ty>() + #generator.subschema_for::<#ty>() }; prepend_type_def(type_def, &mut schema_expr); @@ -384,9 +384,9 @@ fn expr_for_internal_tagged_enum_variant( ) -> TokenStream { if let Some(with_attr) = &variant.attrs.with { let (ty, type_def) = type_for_schema(with_attr); - let gen = quote!(gen); + let generator = quote!(generator); let mut schema_expr = quote_spanned! {variant.original.span()=> - <#ty as schemars::JsonSchema>::json_schema(#gen) + <#ty as schemars::JsonSchema>::json_schema(#generator) }; prepend_type_def(type_def, &mut schema_expr); @@ -403,7 +403,7 @@ fn expr_for_internal_tagged_enum_variant( fn expr_for_unit_struct() -> TokenStream { quote! { - gen.subschema_for::<()>() + generator.subschema_for::<()>() } } @@ -449,7 +449,7 @@ fn expr_for_struct( let required = field.validation_attrs.required(); - let args = quote!(gen, #required); + let args = quote!(generator, #required); let mut schema_expr = quote_spanned! {ty.span()=> schemars::_private::json_schema_for_flatten::<#ty>(#args) }; @@ -475,14 +475,14 @@ fn expr_for_struct( ..field.attrs.as_metadata() }; - let gen = quote!(gen); + let generator = quote!(generator); let mut schema_expr = if field.validation_attrs.required() { quote_spanned! {ty.span()=> - <#ty as schemars::JsonSchema>::_schemars_private_non_optional_json_schema(#gen) + <#ty as schemars::JsonSchema>::_schemars_private_non_optional_json_schema(#generator) } } else { quote_spanned! {ty.span()=> - #gen.subschema_for::<#ty>() + #generator.subschema_for::<#ty>() } };