pub struct SchemaGenerator { /* private fields */ }Expand description
The main type used to generate JSON Schemas.
Example
use schemars::{JsonSchema, gen::SchemaGenerator};
#[derive(JsonSchema)]
struct MyStruct {
    foo: i32,
}
let gen = SchemaGenerator::default();
let schema = gen.into_root_schema_for::<MyStruct>();Implementations
sourceimpl SchemaGenerator
 
impl SchemaGenerator
sourcepub fn new(settings: SchemaSettings) -> SchemaGenerator
 
pub fn new(settings: SchemaSettings) -> SchemaGenerator
Creates a new SchemaGenerator using the given settings.
sourcepub fn settings(&self) -> &SchemaSettings
 
pub fn settings(&self) -> &SchemaSettings
Borrows the SchemaSettings being used by this SchemaGenerator.
Example
use schemars::gen::SchemaGenerator;
let gen = SchemaGenerator::default();
let settings = gen.settings();
assert_eq!(settings.option_add_null_type, true);sourcepub fn make_extensible(&self, schema: &mut SchemaObject)
 
pub fn make_extensible(&self, schema: &mut SchemaObject)
Modifies the given SchemaObject so that it may have validation, metadata or other properties set on it.
If schema is not a $ref schema, then this does not modify schema. Otherwise, depending on this generator’s settings,
this may wrap the $ref in another schema. This is required because in many JSON Schema implementations, a schema with $ref
set may not include other properties.
Example
use schemars::{gen::SchemaGenerator, schema::SchemaObject};
let gen = SchemaGenerator::default();
let ref_schema = SchemaObject::new_ref("foo".to_owned());
assert!(ref_schema.is_ref());
let mut extensible_schema = ref_schema.clone();
gen.make_extensible(&mut extensible_schema);
assert_ne!(ref_schema, extensible_schema);
assert!(!extensible_schema.is_ref());
let mut extensible_schema2 = extensible_schema.clone();
gen.make_extensible(&mut extensible_schema);
assert_eq!(extensible_schema, extensible_schema2);sourcepub fn schema_for_any(&self) -> Schema
 
pub fn schema_for_any(&self) -> Schema
Returns a Schema that matches everything, such as the empty schema {}.
The exact value returned depends on this generator’s BoolSchemas setting.
sourcepub fn schema_for_none(&self) -> Schema
 
pub fn schema_for_none(&self) -> Schema
Returns a Schema that matches nothing, such as the schema { "not":{} }.
The exact value returned depends on this generator’s BoolSchemas setting.
sourcepub fn subschema_for<T>(&mut self) -> Schema where
    T: JsonSchema + ?Sized, 
 
pub fn subschema_for<T>(&mut self) -> Schema where
    T: JsonSchema + ?Sized, 
Generates a JSON Schema for the type T, and returns either the schema itself or a $ref schema referencing T’s schema.
If T is referenceable, this will add T’s schema to this generator’s definitions, and
return a $ref schema referencing that schema. Otherwise, this method behaves identically to JsonSchema::json_schema.
If T’s schema depends on any referenceable schemas, then this method will
add them to the SchemaGenerator’s schema definitions.
sourcepub fn definitions(&self) -> &BTreeMap<String, Schema>
 
pub fn definitions(&self) -> &BTreeMap<String, Schema>
Returns the collection of all referenceable schemas that have been generated.
The keys of the returned Map are the schema names, and the values are the schemas
themselves.
sourcepub fn into_definitions(self) -> BTreeMap<String, Schema>
 
pub fn into_definitions(self) -> BTreeMap<String, Schema>
Consumes self and returns the collection of all referenceable schemas that have been generated.
The keys of the returned Map are the schema names, and the values are the schemas
themselves.
sourcepub fn root_schema_for<T>(&mut self) -> RootSchema where
    T: JsonSchema + ?Sized, 
 
pub fn root_schema_for<T>(&mut self) -> RootSchema where
    T: JsonSchema + ?Sized, 
Generates a root JSON Schema for the type T.
If T’s schema depends on any referenceable schemas, then this method will
add them to the SchemaGenerator’s schema definitions and include them in the returned SchemaObject’s
definitions
sourcepub fn into_root_schema_for<T>(self) -> RootSchema where
    T: JsonSchema + ?Sized, 
 
pub fn into_root_schema_for<T>(self) -> RootSchema where
    T: JsonSchema + ?Sized, 
Consumes self and generates a root JSON Schema for the type T.
If T’s schema depends on any referenceable schemas, then this method will
include them in the returned SchemaObject’s definitions
sourcepub fn dereference(&'a self, schema: &Schema) -> Option<&'a Schema>
 
pub fn dereference(&'a self, schema: &Schema) -> Option<&'a Schema>
Attemps to find the schema that the given schema is referencing.
If the given schema has a $ref property which refers
to another schema in self’s schema definitions, the referenced schema will be returned. Otherwise, returns None.
Example
use schemars::{JsonSchema, gen::SchemaGenerator};
#[derive(JsonSchema)]
struct MyStruct {
    foo: i32,
}
let mut gen = SchemaGenerator::default();
let ref_schema = gen.subschema_for::<MyStruct>();
assert!(ref_schema.is_ref());
let dereferenced = gen.dereference(&ref_schema);
assert!(dereferenced.is_some());
assert!(!dereferenced.unwrap().is_ref());
assert_eq!(dereferenced, gen.definitions().get("MyStruct"));Trait Implementations
sourceimpl Clone for SchemaGenerator
 
impl Clone for SchemaGenerator
sourcefn clone(&self) -> SchemaGenerator
 
fn clone(&self) -> SchemaGenerator
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
 
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source. Read more
sourceimpl Debug for SchemaGenerator
 
impl Debug for SchemaGenerator
sourceimpl Default for SchemaGenerator
 
impl Default for SchemaGenerator
sourcefn default() -> SchemaGenerator
 
fn default() -> SchemaGenerator
Returns the “default value” for a type. Read more
sourceimpl From<SchemaSettings> for SchemaGenerator
 
impl From<SchemaSettings> for SchemaGenerator
sourcefn from(settings: SchemaSettings) -> SchemaGenerator
 
fn from(settings: SchemaSettings) -> SchemaGenerator
Converts to this type from the input type.
Auto Trait Implementations
impl RefUnwindSafe for SchemaGenerator
impl Send for SchemaGenerator
impl Sync for SchemaGenerator
impl Unpin for SchemaGenerator
impl UnwindSafe for SchemaGenerator
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
    T: ?Sized, 
 
impl<T> BorrowMut<T> for T where
    T: ?Sized, 
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
 
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
    T: Clone, 
 
impl<T> ToOwned for T where
    T: Clone, 
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
 
fn clone_into(&self, target: &mut T)
toowned_clone_into)Uses borrowed data to replace owned data, usually by cloning. Read more