- the
enumset1
/enumset
optional dependency has been removed, as itsJsonSchema
impl did not actually match the default serialization format ofEnumSet
(#339)
⚠️ MSRV is now 1.70⚠️
- The "deserialize" schema for
bytes::Bytes
/BytesMut
now allows strings, matching the actual deserialize behaviour of the types. - The schema for
either::Either
now matches the actual serialize/deserialize behaviour of that type.
SchemaSettings
now has acontract
field which determines whether the generated schemas describe how types are serialized or deserialized. By default, this is set toDeserialize
, as this more closely matches the behaviour of previous versions - you can change this toSerialize
to instead generate schemas describing the type's serialization behaviour (#48 / #335)
- Schemas generated for enums with no variants will now generate
false
(or equivalently{"not":{}}
), instead of{"enum":[]}
. This is so generated schemas no longer violate the JSON Schema spec's recommendation that a schema'senum
array "SHOULD have at least one element".
- Read
#[garde(...)]
attributes as an alternative to#[validate(...)]
(#233 / #331). See the documentation for a full list of supported attributes.
- Fix compile errors when using
#[validate(regex(path = *expr))]
attribute
- Allow
regex(path = ...)
value to be a non-string expression (#302 / #328) - Respect
#[serde(rename_all_fields = ...)]
attribute (#273 / #304)
- Invalid attributes that were previously silently ignored (e.g. setting
schema_with
on structs) will now cause compile errors - Validation attribute parsing has been altered to match the latest version of the validator crate:
- Remove the
phone
attribute - Remove the
required_nested
attribute regex
andcontains
attributes must now be specified in list form#[validate(regex(path = ...))]
rather than name/value form#[validate(regex = ...)]
- Remove the
- Values in
#[doc = ...]
and#[schemars(description = ..., title = ...)]
attributes may now be any arbitrary expression rather than just string literals. (#204 / #327) ⚠️ MSRV is now 1.65⚠️
- Replace
schemars::gen
module withschemars::generate
. This is becausegen
is a reserved keyword in rust 2024, so can only be used asr#gen
. Theschemars::gen
module is still available for ease of upgrading, but is marked as deprecated and may be removed in the future 1.0.0 release. (#306 / #323)
- Fix behaviour of
flatten
for schemas withadditionalProperties
- Fix behaviour of
flatten
of multiple enums (#165 / #320)
- Fixed a configuration error that caused rustdoc generation to fail on docs.rs
- Schemars can now be used in
no_std
environments by disabling the newstd
feature flag (which is enabled by default). Schemars still requires an allocator to be available.
- Reduce size of MIR output (and improve release-mode compile time) when deriving
JsonSchema
involving applying schema metadata - Fix
flatten
ing ofserde_json::Value
- Use absolute import for
Result
in derive output, ignoring any locally imported types calledResult
(#307)
#[schemars(transform = some::transform)]
for applying arbitrary modifications to generated schemas.some::transform
must be an expression of typeschemars::transform::Transform
- note that this can be a function with the signaturefn(&mut Schema) -> ()
.SchemaSettings
andSchemaGenerator
are both nowSend
visit
module andVisitor
trait have been replace withtransform
andTransform
respectively. Accordingly, these items have been renamed:SchemaSettings::visitors
->SchemaSettings::transforms
SchemaSettings::with_visitor
->SchemaSettings::with_transform
SchemaGenerator::visitors_mut
->SchemaGenerator::transforms_mut
GenVisitor
->GenTransform
Visitor::visit_schema
->Transform::transform
visit::visit_schema
->transform::transform_subschemas
GenTransform
must also implSend
, but no longer needs to implDebug
- Doc comments no longer have newlines collapsed when generating the
description
property (#310)
#[schemars(extend("key" = value))]
attribute which can be used to add properties (or replace existing properties) in a generated schema (#50 / #297)- Can be set on a struct, enum, or enum variant
- Value can be any expression that results in a value implementing
Serialize
- Value can also be a JSON literal following the rules of
serde_json::json!(value)
macro, i.e. it can interpolate other values that implementSerialize
json_schema!
macro for creating a customSchema
- Implement
JsonSchema
for uuid 1.x types, under the optionaluuid1
feature flag SchemaSettings::draft2020_12()
to construct settings conforming to JSON Schema draft 2020-12
- The
Schema
type is now defined as a thin wrapper around aserde_json::Value
- The default
SchemaSettings
(used by theschema_for!()
/schema_for_value!()
macros andSchemaGenerator::default()
) now conform to JSON Schema draft 2020-12 instead of draft 7. - Schemas generated using
SchemaSettings::draft2019_09()
(anddraft2020_12()
anddefault()
) now use$defs
instead ofdefinitions
. While usingdefinitions
is allowed by the spec,$defs
is the preferred property for storing reusable schemas. JsonSchema::schema_name()
now returnsCow<'static, str>
instead ofString
JsonSchema::is_referenceable()
has been removed, and replaced with the more clearly-namedJsonSchema::always_inline()
(which should returns the opposite value to whatis_referenceable
returned!)- The
SchemaGenerator.definitions
field is now aserde_json::Map<String, Value>
- Macros/functions that previously returned a
RootSchema
now return aSchema
instead - All optional dependencies are now suffixed by their version:
chrono
is nowchrono04
either
is noweither1
smallvec
is nowsmallvec1
url
is nowurl2
bytes
is nowbytes1
rust_decimal
is nowrust_decimal1
enumset
is nowenumset1
smol_str
is nowsmol_str02
semver
is nowsemver1
indexmap2
,arrayvec07
andbigdecimal04
are unchanged
- Removed deprecated
SchemaGenerator
methodsmake_extensible
,schema_for_any
andschema_for_none
- Removed the
schema
module- The
Schema
type is now accessible from the crate root (i.e.schemars::Schema
instead ofschemars::schema::Schema
) - All other types that were in the module have been removed:
RootSchema
SchemaObject
Metadata
SubschemaValidation
NumberValidation
StringValidation
ArrayValidation
ObjectValidation
InstanceType
SingleOrVec
- The
- Removed
schemars::Set
andschemars::Map
type aliases - Removed the
impl_json_schema
feature flag -JsonSchema
is now always implemented onSchema
- Remove methods
visit_schema_object
andvisit_root_schema
from theVisitor
trait (visit_schema
is unchanged)- Visitors that previously defined
visit_schema_object
should instead definevisit_schema
and use anif let Some(obj) = schema.as_object_mut()
or similar construct
- Visitors that previously defined
- Old versions of optional dependencies have been removed - all of these have newer versions (shown in brackets) which are supported by schemars
indexmap
(consider usingindexmap2
)uuid08
(consider usinguuid1
)arrayvec05
(consider usingarrayvec07
)bigdecimal03
(consider usingbigdecimal04
)
- Remove the retain_examples field from SetSingleExample, which is now a unit struct
- Revert unintentional change in behaviour when combining
default
andrequired
attributes (#292)
- Regression that caused a compile error when deriving
JsonSchema
on an enum with no variants (#287)
- Reduce size of MIR output (and improve release-mode compile time) when deriving
JsonSchema
on enums (#266 / #286)
- Update to syn 2.0, which should improve compile times in many cases (#281)
- Reduce size of MIR output (and improve release-mode compile time) when deriving
JsonSchema
- Implement
JsonSchema
forBigDecimal
frombigdecimal
0.4 (#237)
- Add
#[schemars(inner(...)]
attribute to specify schema for array items (#234)
- New optional associated function on
JsonSchema
trait:schema_id()
, which is similar toschema_name()
, but does not have to be human-readable, and defaults to the type name including module path. This allows schemars to differentiate between types with the same name in different modules/crates (#62 / #247)
- Schemas for
rust_decimal::Decimal
andbigdecimal::BigDecimal
now match how those types are serialized by default, i.e. as numeric strings (#248)
- Implement
JsonSchema
forsemver::Version
(#195 / #238) - Include const generics in generated schema names (#179 / #239)
- Implement
JsonSchema
for types from indexmap v2 (#226 / #240) - Implement
JsonSchema
forserde_json::value::RawValue
(#183)
- Minimum supported rust version is now 1.60.0
- Implement
JsonSchema
forsmol_str::SmolStr
(#72)
- Change
serde_json
dependency min version to 1.0.25 (was 1.0.0) (#192)
- Replace auto-inferred trait bounds with bounds specified in
#[schemars(bound = "...")]
attribute
- Derived
JsonSchema
now respects attributes on unit enum variants (#152) - Minimum supported rust version is now 1.45.0
- Undo "Support generic default values in default attributes (#83)" as it inadvertently introduced a breaking change (#144)
Support generic default values indefault
attributes (#83)- This inadvertently introduced a breaking change and was removed in 0.8.10
- Add missing MIT licence text for usage of code from regex_syntax crate (#132)
- Support uuid v1 and arrayvec 0.7 via feature flags
uuid1
andarrayvec07
(#142)- This also adds
uuid08
andarrayvec05
feature flags for the previously supported versions of these crates. The existinguuid
andarrayvec
flags are still supported for backward-compatibility, but they are deprecated. - Similarly,
indexmap1
feature flag is added, andindexmap
flag is deprecated.
- This also adds
- Implement
JsonSchema
for types fromrust_decimal
andbigdecimal
crates (#101)
- Fixes for internally tagged enums and flattening additional_properties (#113)
- Implement
JsonSchema
forEnumSet
(#92)
- Do not cause compile error when using a default value that doesn't implement
Serialize
(#115)
- Use
oneOf
instead ofanyOf
for enums when possible (#108)
- Allow fields with plain
#[validate]
attributes (#109)
#[schemars(schema_with = "...")]
attribute can now be set on enum variants.- Deriving JsonSchema will now take into account
#[validate(...)]
attributes, compatible with the validator crate (#78)
- Support for
#[schemars(crate = "...")]
attribute to allow deriving JsonSchema when the schemars crate is aliased to a different name (#55 / #80) - Implement
JsonSchema
forbytes::Bytes
andbytes::BytesMut
(#68)
- Enable generating a schema from any serializable value using
schema_for_value!(...)
macro orSchemaGenerator::root_schema_for_value()
/SchemaGenerator::into_root_schema_for_value()
methods (#75) #[derive(JsonSchema_repr)]
can be used on C-like enums for generating a serde_repr-compatible schema (#76)- Implement
JsonSchema
forurl::Url
(#63)
SchemaGenerator::definitions_mut()
which returns a mutable reference to the generator's schema definitions- Implement
JsonSchema
for slices
- Minimum supported rust version is now 1.37.0
- Deriving JsonSchema on enums now sets
additionalProperties
to false on generated schemas wherever serde doesn't accept unknown properties. This includes non-unit variants of externally tagged enums, and struct-style variants of all enums that have thedeny_unknown_fields
attribute. - Schemas for HashSet and BTreeSet now have
uniqueItems
set to true (#64)
- Fix use of
#[serde(transparent)]
in combination with#[schemars(with = ...)]
(#67) - Fix clippy
field_reassign_with_default
warning in schemars_derive generated code in rust <1.51 (#65) - Prevent stack overflow when using
inline_subschemas
with recursive types
visit::Visitor
, a trait for updating a schema and all schemas it contains recursively. ASchemaSettings
can now contain a list of visitors.into_object()
method added toSchema
as a shortcut forinto::<SchemaObject>()
- Preserve order of schema properties under
preserve_order
feature flag (#32) SchemaGenerator::take_definitions()
which behaves similarly to the now-removedinto_definitions()
method but without consuming the generatorSchemaGenerator::visitors_mut()
which returns an iterator over a generator's settings's visitorsSchemaSettings::inline_subschemas
- enforces inlining of all subschemas instead of using references (#44)
SchemaSettings::bool_schemas
- this has been superseded by theReplaceBoolSchemas
visitorSchemaSettings::allow_ref_siblings
- this has been superseded by theRemoveRefSiblings
visitorSchemaSettings
no longer implementsPartialEq
SchemaGenerator::into_definitions()
- this has been superseded bySchemaGenerator::take_definitions()
- BREAKING CHANGE Minimum supported rust version is now 1.36.0
- BREAKING CHANGE unknown items in
#[schemars(...)]
attributes now cause a compilation error (#18)
make_extensible
,schema_for_any
, andschema_for_none
methods onSchemaGenerator
#[schemars(example = "...")]
attribute for setting examples on generated schemas (#23)
- Setting
#[deprecated]
attribute will now cause generated schemas to have thedeprecated
property set totrue
- Respect
#[serde(transparent)]
attribute (#17) #[schemars(title = "...", description = "...")]
can now be used to set schema title/description. If present, these values will be used instead of doc comments (#13)
- schemars_derive is now an optional dependency, but included by default
- If a struct is annotated with
#[serde(deny_unknown_fields)]
, generated schema will haveadditionalProperties
set tofalse
(#30) - Set
type
property tostring
on simple enums (#28)
#[schemars(schema_with = "...")]
attribute can be set on variants and fields. This allows you to specify another function which returns the schema you want, which is particularly useful on fields of types that don't implement the JsonSchema trait (#15)
#[serde(with = "...")]
/#[schemars(with = "...")]
attributes on enum variants are now respected- Some compiler errors generated by schemars_derive should now have more accurate spans
- Enable deriving JsonSchema on adjacent tagged enums (#4)
- Added
examples
(https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-9.5) toMetadata
- Fixed a bug in schemars_derive causing a compile error when the
default
,skip_serializing_if
, andserialize_with
/with
attributes are used together (#26)
- BREAKING CHANGE -
SchemaSettings
can no longer be created using struct initialization syntax. Instead, if you need to use custom schema settings, you can use a constructor function and either:- assign it to a
mut
variable and modify its public fields - call the
with(|s| ...)
method on the settings and modify the settings inside the closure/function (as in the custom_settings.rs example)
- assign it to a
- When deriving
JsonSchema
on structs,Option<T>
struct fields are no longer included in the list of required properties in the schema (#11) - Fix deriving
JsonSchema
when a non-stdString
type is in scope (#19) - This will now compile:
#[schemars(with="()")]
- Added
allow_ref_siblings
setting toSchemaSettings
. When enabled, schemas with a$ref
property may have other properties set. - Can create JSON Schema 2019-09 schemas using
SchemaSettings::draft2019_09()
(which enablesallow_ref_siblings
)
- Implemented
JsonSchema
on types fromsmallvec
andarrayvec
(as optional dependencies)
- Implemented
JsonSchema
on types fromindexmap
,either
anduuid
(as optional dependencies)
- Remove trait bounds from Map/Set JsonSchema impls. They are unnecessary as we never create/use any instances of these types.
- No actual code changes - this version was just published to fix broken README on crates.io
- Documentation website available at https://graham.cool/schemars/!
- Rename
derive_json_schema
toimpl_json_schema
.derive_json_schema
is still available for backward-compatibility, but will be removed in a future version. - Improve schema naming for deriving on remote types. A
#[serde(remote = "Duration")]
attribute is now treated similarly to#[serde(rename = "Duration")]
. - Ensure root schemas do not have a
$ref
property. If necessary, wrap the$ref
in anallOf
.
- Fix a compile error that can occur when deriving
JsonSchema
from a project that doesn't reference serde_json
- When deriving
JsonSchema
, the schema'stitle
anddescription
are now set from#[doc]
comments (#7) - When deriving
JsonSchema
on structs using a#[serde(default)]
attribute, the schema's properties will now includedefault
, unless the default value is skipped by the field'sskip_serializing_if
function (#6)
- When the
option_nullable
setting is enabled (e.g. for openapi 3), schemas forOption<T>
will no longer inlineT
's schema when it should be referenceable.
- Added missing doc comment for
title
schema property
- Implemented
JsonSchema
for more standard library types (#3)
- Unsigned integer types (usize, u8 etc.) now have their
minimum
explicitly set to zero - Made prepositions/conjunctions in generated schema names lowercase
- e.g. schema name for
Result<MyStruct, Vec<String>>
has changed from "Result_Of_MyStruct_Or_Array_Of_String" to "Result_of_MyStruct_or_Array_of_String"
- e.g. schema name for
- Some provided
JsonSchema
implementations with the sametype
but differentformat
s (e.g.i8
andusize
) used thetype
as their name. They have now been updated to useformat
as their name.- Previously, schema generation would incorrectly assume types such as
MyStruct<i8>
andMyStruct<usize>
were identical, and give them a single schema definition calledMyStruct_for_Integer
despite the fact they should have different schemas. Now they will each have their own schema (MyStruct_for_i8
andMyStruct_for_usize
respectively).
- Previously, schema generation would incorrectly assume types such as