Refactor out add_schema_as_property
This commit is contained in:
parent
1a2dafc1a5
commit
60a9869448
6 changed files with 89 additions and 101 deletions
|
@ -24,40 +24,12 @@ pub fn json_schema_for_flatten<T: ?Sized + JsonSchema>(
|
|||
schema
|
||||
}
|
||||
|
||||
// Helper for generating schemas for `Option` fields.
|
||||
pub fn add_schema_as_property<T: ?Sized + JsonSchema>(
|
||||
gen: &mut SchemaGenerator,
|
||||
parent: &mut SchemaObject,
|
||||
name: String,
|
||||
metadata: Option<Metadata>,
|
||||
required: Option<bool>,
|
||||
) {
|
||||
let is_type_option = T::_schemars_private_is_option();
|
||||
let required = required.unwrap_or(!is_type_option);
|
||||
|
||||
let mut schema = if required && is_type_option {
|
||||
T::_schemars_private_non_optional_json_schema(gen)
|
||||
pub fn apply_metadata(schema: Schema, metadata: Metadata) -> Schema {
|
||||
if metadata == Metadata::default() {
|
||||
schema
|
||||
} else {
|
||||
gen.subschema_for::<T>()
|
||||
};
|
||||
|
||||
schema = apply_metadata(schema, metadata);
|
||||
|
||||
let object = parent.object();
|
||||
if required {
|
||||
object.required.insert(name.clone());
|
||||
}
|
||||
object.properties.insert(name, schema);
|
||||
}
|
||||
|
||||
pub fn apply_metadata(schema: Schema, metadata: Option<Metadata>) -> Schema {
|
||||
match metadata {
|
||||
None => schema,
|
||||
Some(ref metadata) if *metadata == Metadata::default() => schema,
|
||||
Some(metadata) => {
|
||||
let mut schema_obj = schema.into_object();
|
||||
schema_obj.metadata = Some(Box::new(metadata)).merge(schema_obj.metadata);
|
||||
Schema::Object(schema_obj)
|
||||
}
|
||||
let mut schema_obj = schema.into_object();
|
||||
schema_obj.metadata = Some(Box::new(metadata)).merge(schema_obj.metadata);
|
||||
Schema::Object(schema_obj)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue