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
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 generator = SchemaGenerator::default();
let settings = generator.settings();
assert_eq!(settings.inline_subschemas, false);Sourcepub fn subschema_for<T>(&mut self) -> Schemawhere
T: JsonSchema + ?Sized,
pub fn subschema_for<T>(&mut self) -> Schemawhere
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 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, apply_transforms: bool) -> Map<String, Value>
pub fn take_definitions(&mut self, apply_transforms: bool) -> 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.
To apply this generator’s transforms to each of the returned
schemas, set apply_transforms to true.
Sourcepub fn transforms_mut(
&mut self,
) -> impl Iterator<Item = &mut (dyn GenTransform + 'static)>
pub fn transforms_mut( &mut self, ) -> impl Iterator<Item = &mut (dyn GenTransform + 'static)>
Returns an iterator over the transforms being used by this
SchemaGenerator.
Sourcepub fn root_schema_for<T>(&mut self) -> Schemawhere
T: JsonSchema + ?Sized,
pub fn root_schema_for<T>(&mut self) -> Schemawhere
T: JsonSchema + ?Sized,
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>(self) -> Schemawhere
T: JsonSchema + ?Sized,
pub fn into_root_schema_for<T>(self) -> Schemawhere
T: JsonSchema + ?Sized,
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>(&mut self, value: &T) -> Result<Schema, Error>
pub fn root_schema_for_value<T>(&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>(self, value: &T) -> Result<Schema, Error>
pub fn into_root_schema_for_value<T>(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.
Trait Implementations§
Source§impl Clone for SchemaGenerator
impl Clone for SchemaGenerator
Source§fn clone(&self) -> SchemaGenerator
fn clone(&self) -> SchemaGenerator
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SchemaGenerator
impl Debug for SchemaGenerator
Source§impl Default for SchemaGenerator
impl Default for SchemaGenerator
Source§fn default() -> SchemaGenerator
fn default() -> SchemaGenerator
Source§impl From<SchemaSettings> for SchemaGenerator
impl From<SchemaSettings> for SchemaGenerator
Source§fn from(settings: SchemaSettings) -> SchemaGenerator
fn from(settings: SchemaSettings) -> SchemaGenerator
Auto Trait Implementations§
impl Freeze for SchemaGenerator
impl !RefUnwindSafe for SchemaGenerator
impl Send for SchemaGenerator
impl !Sync for SchemaGenerator
impl Unpin for SchemaGenerator
impl UnsafeUnpin for SchemaGenerator
impl !UnwindSafe for SchemaGenerator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.