Add private field to SchemaSettings to prevent struct initialization.
This means future additions to SchemaSettings will not be breaking changes.
This commit is contained in:
parent
1963b5c715
commit
233b1a4165
4 changed files with 41 additions and 27 deletions
|
@ -8,19 +8,16 @@ pub struct MyStruct {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(JsonSchema)]
|
#[derive(JsonSchema)]
|
||||||
pub enum MyEnum {
|
pub enum MyEnum {
|
||||||
StringNewType(String),
|
StringNewType(String),
|
||||||
StructVariant {
|
StructVariant { floats: Vec<f32> },
|
||||||
floats: Vec<f32>,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let settings = SchemaSettings {
|
let settings = SchemaSettings::draft07().with(|s| {
|
||||||
option_nullable: true,
|
s.option_nullable = true;
|
||||||
option_add_null_type: false,
|
s.option_add_null_type = false;
|
||||||
..SchemaSettings::draft07()
|
});
|
||||||
};
|
|
||||||
let gen = settings.into_generator();
|
let gen = settings.into_generator();
|
||||||
let schema = gen.into_root_schema_for::<MyStruct>();
|
let schema = gen.into_root_schema_for::<MyStruct>();
|
||||||
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
||||||
|
|
|
@ -2,7 +2,7 @@ use crate::schema::*;
|
||||||
use crate::{JsonSchema, Map};
|
use crate::{JsonSchema, Map};
|
||||||
|
|
||||||
/// Settings to customize how Schemas are generated.
|
/// Settings to customize how Schemas are generated.
|
||||||
///
|
///
|
||||||
/// The default settings currently conform to [JSON Schema Draft 7](https://json-schema.org/specification-links.html#draft-7), but this is liable to change in a future version of Schemars if support for other JSON Schema versions is added.
|
/// The default settings currently conform to [JSON Schema Draft 7](https://json-schema.org/specification-links.html#draft-7), but this is liable to change in a future version of Schemars if support for other JSON Schema versions is added.
|
||||||
/// If you require your generated schemas to conform to draft 7, consider using the [`draft07`](#method.draft07) method.
|
/// If you require your generated schemas to conform to draft 7, consider using the [`draft07`](#method.draft07) method.
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
@ -29,6 +29,7 @@ pub struct SchemaSettings {
|
||||||
///
|
///
|
||||||
/// Defaults to `"http://json-schema.org/draft-07/schema#"`.
|
/// Defaults to `"http://json-schema.org/draft-07/schema#"`.
|
||||||
pub meta_schema: Option<String>,
|
pub meta_schema: Option<String>,
|
||||||
|
_hidden: (),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Controls whether trivial [`Bool`](../schema/enum.Schema.html#variant.Bool) schemas may be generated.
|
/// Controls whether trivial [`Bool`](../schema/enum.Schema.html#variant.Bool) schemas may be generated.
|
||||||
|
@ -57,6 +58,7 @@ impl SchemaSettings {
|
||||||
bool_schemas: BoolSchemas::Enabled,
|
bool_schemas: BoolSchemas::Enabled,
|
||||||
definitions_path: "#/definitions/".to_owned(),
|
definitions_path: "#/definitions/".to_owned(),
|
||||||
meta_schema: Some("http://json-schema.org/draft-07/schema#".to_owned()),
|
meta_schema: Some("http://json-schema.org/draft-07/schema#".to_owned()),
|
||||||
|
_hidden: (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,9 +73,27 @@ impl SchemaSettings {
|
||||||
"https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema"
|
"https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema"
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
),
|
),
|
||||||
|
_hidden: (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Modifies the `SchemaSettings` by calling the given function.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```
|
||||||
|
/// use schemars::gen::{SchemaGenerator, SchemaSettings};
|
||||||
|
///
|
||||||
|
/// let settings = SchemaSettings::default().with(|s| {
|
||||||
|
/// s.option_nullable = true;
|
||||||
|
/// s.option_add_null_type = false;
|
||||||
|
/// });
|
||||||
|
/// let gen = settings.into_generator();
|
||||||
|
/// ```
|
||||||
|
pub fn with(mut self, configure_fn: impl FnOnce(&mut Self)) -> Self {
|
||||||
|
configure_fn(&mut self);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a new [`SchemaGenerator`] using these settings.
|
/// Creates a new [`SchemaGenerator`] using these settings.
|
||||||
pub fn into_generator(self) -> SchemaGenerator {
|
pub fn into_generator(self) -> SchemaGenerator {
|
||||||
SchemaGenerator::new(self)
|
SchemaGenerator::new(self)
|
||||||
|
@ -100,6 +120,12 @@ pub struct SchemaGenerator {
|
||||||
definitions: Map<String, Schema>,
|
definitions: Map<String, Schema>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<SchemaSettings> for SchemaGenerator {
|
||||||
|
fn from(settings: SchemaSettings) -> Self {
|
||||||
|
settings.into_generator()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl SchemaGenerator {
|
impl SchemaGenerator {
|
||||||
/// Creates a new `SchemaGenerator` using the given settings.
|
/// Creates a new `SchemaGenerator` using the given settings.
|
||||||
pub fn new(settings: SchemaSettings) -> SchemaGenerator {
|
pub fn new(settings: SchemaSettings) -> SchemaGenerator {
|
||||||
|
|
|
@ -199,11 +199,10 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn schema_for_option_with_nullable() {
|
fn schema_for_option_with_nullable() {
|
||||||
let settings = SchemaSettings {
|
let settings = SchemaSettings::default().with(|s| {
|
||||||
option_nullable: true,
|
s.option_nullable = true;
|
||||||
option_add_null_type: false,
|
s.option_add_null_type = false;
|
||||||
..Default::default()
|
});
|
||||||
};
|
|
||||||
let schema = custom_schema_object_for::<Option<i32>>(settings);
|
let schema = custom_schema_object_for::<Option<i32>>(settings);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
schema.instance_type,
|
schema.instance_type,
|
||||||
|
|
|
@ -52,10 +52,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn schema_for_map_any_value() {
|
fn schema_for_map_any_value() {
|
||||||
for bool_schemas in &[BoolSchemas::Enabled, BoolSchemas::AdditionalPropertiesOnly] {
|
for bool_schemas in &[BoolSchemas::Enabled, BoolSchemas::AdditionalPropertiesOnly] {
|
||||||
let settings = SchemaSettings {
|
let settings = SchemaSettings::default().with(|s| s.bool_schemas = *bool_schemas);
|
||||||
bool_schemas: *bool_schemas,
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let schema = custom_schema_object_for::<BTreeMap<String, serde_json::Value>>(settings);
|
let schema = custom_schema_object_for::<BTreeMap<String, serde_json::Value>>(settings);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
schema.instance_type,
|
schema.instance_type,
|
||||||
|
@ -72,10 +69,8 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn schema_for_map_any_value_no_bool_schema() {
|
fn schema_for_map_any_value_no_bool_schema() {
|
||||||
let settings = SchemaSettings {
|
let settings =
|
||||||
bool_schemas: BoolSchemas::Disabled,
|
SchemaSettings::default().with(|s| s.bool_schemas = BoolSchemas::Disabled);
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let schema = custom_schema_object_for::<BTreeMap<String, serde_json::Value>>(settings);
|
let schema = custom_schema_object_for::<BTreeMap<String, serde_json::Value>>(settings);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
schema.instance_type,
|
schema.instance_type,
|
||||||
|
@ -96,10 +91,7 @@ mod tests {
|
||||||
BoolSchemas::Disabled,
|
BoolSchemas::Disabled,
|
||||||
BoolSchemas::AdditionalPropertiesOnly,
|
BoolSchemas::AdditionalPropertiesOnly,
|
||||||
] {
|
] {
|
||||||
let settings = SchemaSettings {
|
let settings = SchemaSettings::default().with(|s| s.bool_schemas = *bool_schemas);
|
||||||
bool_schemas: *bool_schemas,
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let schema = custom_schema_object_for::<BTreeMap<String, i32>>(settings);
|
let schema = custom_schema_object_for::<BTreeMap<String, i32>>(settings);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
schema.instance_type,
|
schema.instance_type,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue