Struct 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 generator = SchemaGenerator::default();
let schema = generator.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::SchemaGenerator;

let generator = SchemaGenerator::default();
let settings = generator.settings();

assert_eq!(settings.inline_subschemas, false);
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 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.

Source

pub fn definitions(&self) -> &JsonMap<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.

Source

pub fn definitions_mut(&mut self) -> &mut JsonMap<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.

Source

pub fn take_definitions( &mut self, apply_transforms: bool, ) -> JsonMap<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.

To apply this generator’s transforms to each of the returned schemas, set apply_transforms to true.

Source

pub fn transforms_mut(&mut self) -> impl Iterator<Item = &mut dyn GenTransform>

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

Source

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

Source

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

Source

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.

Source

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.

Source

pub fn contract(&self) -> &Contract

Returns a reference to the contract for the settings on this SchemaGenerator.

This specifies whether generated schemas describe serialize or deserialize behaviour.

Trait Implementations§

Source§

impl Clone for SchemaGenerator

Source§

fn clone(&self) -> Self

Returns a duplicate 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() -> Self

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.