Skip to content

Commit

Permalink
Breaking change: Rename gen module to generators in order to be compa…
Browse files Browse the repository at this point in the history
…tible with rust 2024 edition
  • Loading branch information
havasd authored and David Havas committed Feb 23, 2025
1 parent e0c2c31 commit 6ef6e17
Show file tree
Hide file tree
Showing 53 changed files with 213 additions and 206 deletions.
2 changes: 1 addition & 1 deletion docs/1.1-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ Validator docs: [required](https://github.com/Keats/validator#required) / [requi
`#[schemars(schema_with = "some::function")]`
</h3>

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::generator::SchemaGenerator) -> schemars::schema::Schema`.

<h3 id="title-description">

Expand Down
4 changes: 2 additions & 2 deletions docs/2-implementing.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ impl JsonSchema for NonGenericType {
## json_schema

```rust
fn json_schema(gen: &mut gen::SchemaGenerator) -> Schema;
fn json_schema(generator: &mut generator::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::<T>()` method instead of `<T>::json_schema(gen)`, as `subschema_for` can add `T`'s schema to the root schema's `definitions` 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::<T>()` method instead of `<T>::json_schema(generator)`, as `subschema_for` can add `T`'s schema to the root schema's `definitions` so that it does not need to be duplicated when used more than once.

`json_schema` should not return a `$ref` schema.

Expand Down
6 changes: 3 additions & 3 deletions docs/3-generating.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ let my_schema = schema_for!(MyStruct);

This will create a schema that conforms to [JSON Schema Draft 7](https://json-schema.org/specification-links.html#draft-7), 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:
* [`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.
If you want more control over how the schema is generated, you can use the [`generator` module](https://docs.rs/schemars/latest/schemars/generator/). There are two main types in this module:
* [`SchemaSettings`](https://docs.rs/schemars/latest/schemars/generator/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/generator/struct.SchemaGenerator.html), which manages the generation of a schema document.

See the API documentation for more info on how to use those types for custom schema generation.

Expand Down
6 changes: 3 additions & 3 deletions docs/_includes/examples/custom_serialization.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use schemars::schema::{Schema, SchemaObject};
use schemars::{gen::SchemaGenerator, schema_for, JsonSchema};
use schemars::{generator::SchemaGenerator, schema_for, JsonSchema};
use serde::{Deserialize, Serialize};

// `int_as_string` and `bool_as_string` use the schema for `String`.
Expand All @@ -20,8 +20,8 @@ pub struct MyStruct {
pub bool_normal: bool,
}

fn make_custom_schema(gen: &mut SchemaGenerator) -> Schema {
let mut schema: SchemaObject = <String>::json_schema(gen).into();
fn make_custom_schema(generator: &mut SchemaGenerator) -> Schema {
let mut schema: SchemaObject = <String>::json_schema(generator).into();
schema.format = Some("boolean".to_owned());
schema.into()
}
Expand Down
6 changes: 3 additions & 3 deletions docs/_includes/examples/custom_settings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use schemars::{gen::SchemaSettings, JsonSchema};
use schemars::{generator::SchemaSettings, JsonSchema};

#[derive(JsonSchema)]
pub struct MyStruct {
Expand All @@ -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::<MyStruct>();
let generator = settings.into_generator();
let schema = generator.into_root_schema_for::<MyStruct>();
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
}
2 changes: 1 addition & 1 deletion docs/examples/4-custom_settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ summary: Generating a schema using custom settings which changes how Option<T> i

# Custom Schema Settings

The `gen` module allows you to customise how schemas are generated. For example, the default behaviour for `Option<T>` is to include `null` in the schema's `type`s, but we can instead add a `nullable` property to its schema:
The `generator` module allows you to customise how schemas are generated. For example, the default behaviour for `Option<T>` 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" %}
6 changes: 3 additions & 3 deletions schemars/examples/custom_serialization.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use schemars::schema::{Schema, SchemaObject};
use schemars::{gen::SchemaGenerator, schema_for, JsonSchema};
use schemars::{generator::SchemaGenerator, schema_for, JsonSchema};
use serde::{Deserialize, Serialize};

// `int_as_string` and `bool_as_string` use the schema for `String`.
Expand All @@ -20,8 +20,8 @@ pub struct MyStruct {
pub bool_normal: bool,
}

fn make_custom_schema(gen: &mut SchemaGenerator) -> Schema {
let mut schema: SchemaObject = <String>::json_schema(gen).into();
fn make_custom_schema(generator: &mut SchemaGenerator) -> Schema {
let mut schema: SchemaObject = <String>::json_schema(generator).into();
schema.format = Some("boolean".to_owned());
schema.into()
}
Expand Down
6 changes: 3 additions & 3 deletions schemars/examples/custom_settings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use schemars::{gen::SchemaSettings, JsonSchema};
use schemars::{generator::SchemaSettings, JsonSchema};

#[derive(JsonSchema)]
pub struct MyStruct {
Expand All @@ -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::<MyStruct>();
let generator = settings.into_generator();
let schema = generator.into_root_schema_for::<MyStruct>();
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
}
6 changes: 3 additions & 3 deletions schemars/src/_private.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::gen::SchemaGenerator;
use crate::generator::SchemaGenerator;
use crate::schema::{InstanceType, ObjectValidation, Schema, SchemaObject};
use crate::{JsonSchema, Map, Set};
use serde::Serialize;
use serde_json::Value;

// Helper for generating schemas for flattened `Option` fields.
pub fn json_schema_for_flatten<T: ?Sized + JsonSchema>(
gen: &mut SchemaGenerator,
r#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(r#generator);

if T::_schemars_private_is_option() && !required {
if let Schema::Object(SchemaObject {
Expand Down
44 changes: 22 additions & 22 deletions schemars/src/gen.rs → schemars/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ impl SchemaSettings {
///
/// # Example
/// ```
/// use schemars::gen::{SchemaGenerator, SchemaSettings};
/// use schemars::generator::{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);
Expand All @@ -137,15 +137,15 @@ impl SchemaSettings {
///
/// # Example
/// ```
/// use schemars::{JsonSchema, gen::SchemaGenerator};
/// use schemars::{JsonSchema, generator::SchemaGenerator};
///
/// #[derive(JsonSchema)]
/// struct MyStruct {
/// foo: i32,
/// }
///
/// let gen = SchemaGenerator::default();
/// let schema = gen.into_root_schema_for::<MyStruct>();
/// let generator = SchemaGenerator::default();
/// let schema = generator.into_root_schema_for::<MyStruct>();
/// ```
#[derive(Debug, Default)]
pub struct SchemaGenerator {
Expand Down Expand Up @@ -187,10 +187,10 @@ impl SchemaGenerator {
///
/// # Example
/// ```
/// use schemars::gen::SchemaGenerator;
/// use schemars::generator::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);
/// ```
Expand Down Expand Up @@ -352,7 +352,7 @@ impl SchemaGenerator {
) -> Result<RootSchema, serde_json::Error> {
let mut schema = value
.serialize(crate::ser::Serializer {
gen: self,
generator: self,
include_title: true,
})?
.into_object();
Expand Down Expand Up @@ -384,7 +384,7 @@ impl SchemaGenerator {
) -> Result<RootSchema, serde_json::Error> {
let mut schema = value
.serialize(crate::ser::Serializer {
gen: &mut self,
generator: &mut self,
include_title: true,
})?
.into_object();
Expand Down Expand Up @@ -413,23 +413,23 @@ impl SchemaGenerator {
///
/// # Example
/// ```
/// use schemars::{JsonSchema, gen::SchemaGenerator};
/// use schemars::{JsonSchema, generator::SchemaGenerator};
///
/// #[derive(JsonSchema)]
/// struct MyStruct {
/// foo: i32,
/// }
///
/// let mut gen = SchemaGenerator::default();
/// let ref_schema = gen.subschema_for::<MyStruct>();
/// let mut generator = SchemaGenerator::default();
/// let ref_schema = generator.subschema_for::<MyStruct>();
///
/// assert!(ref_schema.is_ref());
///
/// let dereferenced = gen.dereference(&ref_schema);
/// let dereferenced = generator.dereference(&ref_schema);
///
/// assert!(dereferenced.is_some());
/// assert!(!dereferenced.unwrap().is_ref());
/// assert_eq!(dereferenced, gen.definitions().get("MyStruct"));
/// assert_eq!(dereferenced, generator.definitions().get("MyStruct"));
/// ```
pub fn dereference<'a>(&'a self, schema: &Schema) -> Option<&'a Schema> {
match schema {
Expand All @@ -451,28 +451,28 @@ impl SchemaGenerator {

fn json_schema_internal<T: ?Sized + JsonSchema>(&mut self, id: Cow<'static, str>) -> Schema {
struct PendingSchemaState<'a> {
gen: &'a mut SchemaGenerator,
generator: &'a mut SchemaGenerator,
id: Cow<'static, str>,
did_add: bool,
}

impl<'a> PendingSchemaState<'a> {
fn new(gen: &'a mut SchemaGenerator, id: Cow<'static, str>) -> Self {
let did_add = gen.pending_schema_ids.insert(id.clone());
Self { gen, id, did_add }
fn new(generator: &'a mut SchemaGenerator, id: Cow<'static, str>) -> 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)
}
}

Expand All @@ -487,7 +487,7 @@ impl SchemaGenerator {
/// # Example
/// ```
/// use schemars::visit::Visitor;
/// use schemars::gen::GenVisitor;
/// use schemars::generator::GenVisitor;
///
/// #[derive(Debug, Clone)]
/// struct MyVisitor;
Expand Down
6 changes: 3 additions & 3 deletions schemars/src/json_schema_impls/array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::gen::SchemaGenerator;
use crate::generator::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use std::borrow::Cow;
Expand Down Expand Up @@ -43,11 +43,11 @@ macro_rules! array_impls {
format!("[{}; {}]", $len, T::schema_id()))
}

fn json_schema(gen: &mut SchemaGenerator) -> Schema {
fn json_schema(generator: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::Array.into()),
array: Some(Box::new(ArrayValidation {
items: Some(gen.subschema_for::<T>().into()),
items: Some(generator.subschema_for::<T>().into()),
max_items: Some($len),
min_items: Some($len),
..Default::default()
Expand Down
6 changes: 3 additions & 3 deletions schemars/src/json_schema_impls/arrayvec05.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::gen::SchemaGenerator;
use crate::generator::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use arrayvec05::{Array, ArrayString, ArrayVec};
Expand All @@ -22,11 +22,11 @@ where
)
}

fn json_schema(gen: &mut SchemaGenerator) -> Schema {
fn json_schema(generator: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::Array.into()),
array: Some(Box::new(ArrayValidation {
items: Some(gen.subschema_for::<A::Item>().into()),
items: Some(generator.subschema_for::<A::Item>().into()),
max_items: A::CAPACITY.try_into().ok(),
..Default::default()
})),
Expand Down
6 changes: 3 additions & 3 deletions schemars/src/json_schema_impls/arrayvec07.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::gen::SchemaGenerator;
use crate::generator::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use arrayvec07::{ArrayString, ArrayVec};
Expand All @@ -18,11 +18,11 @@ where
format!("Array_up_to_size_{}_of_{}", CAP, T::schema_name())
}

fn json_schema(gen: &mut SchemaGenerator) -> Schema {
fn json_schema(generator: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::Array.into()),
array: Some(Box::new(ArrayValidation {
items: Some(gen.subschema_for::<T>().into()),
items: Some(generator.subschema_for::<T>().into()),
max_items: CAP.try_into().ok(),
..Default::default()
})),
Expand Down
2 changes: 1 addition & 1 deletion schemars/src/json_schema_impls/atomic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::gen::SchemaGenerator;
use crate::generator::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use std::sync::atomic::*;
Expand Down
2 changes: 1 addition & 1 deletion schemars/src/json_schema_impls/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::gen::SchemaGenerator;
use crate::generator::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use bytes::{Bytes, BytesMut};
Expand Down
2 changes: 1 addition & 1 deletion schemars/src/json_schema_impls/chrono.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::gen::SchemaGenerator;
use crate::generator::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use chrono::prelude::*;
Expand Down
Loading

0 comments on commit 6ef6e17

Please sign in to comment.