Struct schemars::gen::SchemaGenerator
source · pub struct SchemaGenerator { /* private fields */ }
Expand description
The main type used to generate JSON Schemas.
§Example
use schemars::{JsonSchema, SchemaGenerator};
#[derive(JsonSchema)]
struct MyStruct {
foo: i32,
}
let gen = SchemaGenerator::default();
let schema = gen.into_root_schema_for::<MyStruct>();
Implementations§
source§impl 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::SchemaGenerator;
let gen = SchemaGenerator::default();
let settings = gen.settings();
assert_eq!(settings.option_add_null_type, true);
sourcepub fn subschema_for<T: ?Sized + JsonSchema>(&mut self) -> Schema
pub fn subschema_for<T: ?Sized + JsonSchema>(&mut self) -> Schema
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 not inlined, 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 non-inlined schemas, then this method will
add them to the SchemaGenerator
’s schema definitions.
sourcepub fn definitions(&self) -> &Map<String, Value>
pub fn definitions(&self) -> &Map<String, Value>
Borrows the collection of all non-inlined schemas that have been generated.
The keys of the returned Map
are the schema names, and the values are the schemas
themselves.
sourcepub fn definitions_mut(&mut self) -> &mut Map<String, Value>
pub fn definitions_mut(&mut self) -> &mut Map<String, Value>
Mutably borrows the collection of all non-inlined schemas that have been generated.
The keys of the returned Map
are the schema names, and the values are the schemas
themselves.
sourcepub fn take_definitions(&mut self) -> Map<String, Value>
pub fn take_definitions(&mut self) -> Map<String, Value>
Returns the collection of all non-inlined schemas that have been generated,
leaving an empty Map
in its place.
The keys of the returned Map
are the schema names, and the values are the schemas
themselves.
sourcepub fn visitors_mut(&mut self) -> impl Iterator<Item = &mut dyn GenVisitor>
pub fn visitors_mut(&mut self) -> impl Iterator<Item = &mut dyn GenVisitor>
Returns an iterator over the visitors being used by this SchemaGenerator
.
sourcepub fn root_schema_for<T: ?Sized + JsonSchema>(&mut self) -> Schema
pub fn root_schema_for<T: ?Sized + JsonSchema>(&mut self) -> Schema
Generates a JSON Schema for the type T
.
If T
’s schema depends on any non-inlined schemas, then this method will
include them in the returned Schema
at the definitions path (by default "$defs"
).
sourcepub fn into_root_schema_for<T: ?Sized + JsonSchema>(self) -> Schema
pub fn into_root_schema_for<T: ?Sized + JsonSchema>(self) -> Schema
Consumes self
and generates a JSON Schema for the type T
.
If T
’s schema depends on any non-inlined schemas, then this method will
include them in the returned Schema
at the definitions path (by default "$defs"
).
sourcepub fn root_schema_for_value<T: ?Sized + Serialize>(
&mut self,
value: &T,
) -> Result<Schema, Error>
pub fn root_schema_for_value<T: ?Sized + Serialize>( &mut self, value: &T, ) -> Result<Schema, Error>
Generates a JSON Schema for the given example value.
If the value implements JsonSchema
, then prefer using the root_schema_for()
function which will generally produce a more precise schema, particularly when the value contains any enums.
If the Serialize
implementation of the value decides to fail, this will return an Err
.
sourcepub fn into_root_schema_for_value<T: ?Sized + Serialize>(
self,
value: &T,
) -> Result<Schema, Error>
pub fn into_root_schema_for_value<T: ?Sized + Serialize>( self, value: &T, ) -> Result<Schema, Error>
Consumes self
and generates a JSON Schema for the given example value.
If the value implements JsonSchema
, then prefer using the into_root_schema_for()!
function which will generally produce a more precise schema, particularly when the value contains any enums.
If the Serialize
implementation of the value decides to fail, this will return an Err
.