schemars::gen

Struct SchemaGenerator

source
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§

source§

impl SchemaGenerator

source

pub fn new(settings: SchemaSettings) -> SchemaGenerator

Creates a new SchemaGenerator using the given settings.

source

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);
source

pub fn make_extensible(&self, _schema: &mut SchemaObject)

👎Deprecated: This method no longer has any effect.
source

pub fn schema_for_any(&self) -> Schema

👎Deprecated: Use Schema::Bool(true) instead
source

pub fn schema_for_none(&self) -> Schema

👎Deprecated: Use Schema::Bool(false) instead
source

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 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.

source

pub fn definitions(&self) -> &Map<String, Schema>

Borrows 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.

source

pub fn definitions_mut(&mut self) -> &mut Map<String, Schema>

Mutably borrows 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.

source

pub fn take_definitions(&mut self) -> Map<String, Schema>

Returns the collection of all referenceable 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.

source

pub fn visitors_mut(&mut self) -> impl Iterator<Item = &mut dyn GenVisitor>

Returns an iterator over the visitors being used by this SchemaGenerator.

source

pub fn root_schema_for<T: ?Sized + JsonSchema>(&mut self) -> RootSchema

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

source

pub fn into_root_schema_for<T: ?Sized + JsonSchema>(self) -> RootSchema

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

source

pub fn root_schema_for_value<T: ?Sized + Serialize>( &mut self, value: &T, ) -> Result<RootSchema, Error>

Generates a root 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.

source

pub fn into_root_schema_for_value<T: ?Sized + Serialize>( self, value: &T, ) -> Result<RootSchema, Error>

Consumes self and generates a root 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.

source

pub fn dereference<'a>(&'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§

source§

impl Clone for SchemaGenerator

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for SchemaGenerator

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for SchemaGenerator

source§

fn default() -> SchemaGenerator

Returns the “default value” for a type. Read more
source§

impl From<SchemaSettings> for SchemaGenerator

source§

fn from(settings: SchemaSettings) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> DynClone for T
where T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.