Refactory of private functions
This commit is contained in:
parent
5dc644000c
commit
55b860428e
8 changed files with 74 additions and 87 deletions
49
schemars/src/_private.rs
Normal file
49
schemars/src/_private.rs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
use crate::flatten::Merge;
|
||||||
|
use crate::gen::SchemaGenerator;
|
||||||
|
use crate::schema::{Metadata, Schema, SchemaObject};
|
||||||
|
use crate::JsonSchema;
|
||||||
|
|
||||||
|
// Helper for generating schemas for flattened `Option` fields.
|
||||||
|
pub fn json_schema_for_flatten<T: ?Sized + JsonSchema>(gen: &mut SchemaGenerator) -> Schema {
|
||||||
|
let mut schema = T::_schemars_private_non_optional_json_schema(gen);
|
||||||
|
if T::_schemars_private_is_option() {
|
||||||
|
if let Schema::Object(SchemaObject {
|
||||||
|
object: Some(ref mut object_validation),
|
||||||
|
..
|
||||||
|
}) = schema
|
||||||
|
{
|
||||||
|
object_validation.required.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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: bool,
|
||||||
|
) {
|
||||||
|
let mut schema = gen.subschema_for::<T>();
|
||||||
|
schema = apply_metadata(schema, metadata);
|
||||||
|
|
||||||
|
let object = parent.object();
|
||||||
|
if required && !T::_schemars_private_is_option() {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,7 +7,6 @@ There are two main types in this module:two main types in this module:
|
||||||
* [`SchemaGenerator`], which manages the generation of a schema document.
|
* [`SchemaGenerator`], which manages the generation of a schema document.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use crate::flatten::Merge;
|
|
||||||
use crate::schema::*;
|
use crate::schema::*;
|
||||||
use crate::{visit::*, JsonSchema, Map};
|
use crate::{visit::*, JsonSchema, Map};
|
||||||
use dyn_clone::DynClone;
|
use dyn_clone::DynClone;
|
||||||
|
@ -422,22 +421,6 @@ impl SchemaGenerator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function is only public for use by schemars_derive.
|
|
||||||
///
|
|
||||||
/// It should not be considered part of the public API.
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub fn apply_metadata(&self, 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn json_schema_internal<T: ?Sized + JsonSchema>(&mut self, name: &str) -> Schema {
|
fn json_schema_internal<T: ?Sized + JsonSchema>(&mut self, name: &str) -> Schema {
|
||||||
struct PendingSchemaState<'a> {
|
struct PendingSchemaState<'a> {
|
||||||
gen: &'a mut SchemaGenerator,
|
gen: &'a mut SchemaGenerator,
|
||||||
|
|
|
@ -45,30 +45,12 @@ impl<T: JsonSchema> JsonSchema for Option<T> {
|
||||||
schema
|
schema
|
||||||
}
|
}
|
||||||
|
|
||||||
fn json_schema_for_flatten(gen: &mut SchemaGenerator) -> Schema {
|
fn _schemars_private_non_optional_json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||||
let mut schema = T::json_schema_for_flatten(gen);
|
T::_schemars_private_non_optional_json_schema(gen)
|
||||||
if let Schema::Object(SchemaObject {
|
|
||||||
object: Some(ref mut object_validation),
|
|
||||||
..
|
|
||||||
}) = schema
|
|
||||||
{
|
|
||||||
object_validation.required.clear();
|
|
||||||
}
|
|
||||||
schema
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_schema_as_property(
|
fn _schemars_private_is_option() -> bool {
|
||||||
gen: &mut SchemaGenerator,
|
true
|
||||||
parent: &mut SchemaObject,
|
|
||||||
name: String,
|
|
||||||
metadata: Option<Metadata>,
|
|
||||||
_required: bool,
|
|
||||||
) {
|
|
||||||
let mut schema = gen.subschema_for::<Self>();
|
|
||||||
schema = gen.apply_metadata(schema, metadata);
|
|
||||||
|
|
||||||
let object = parent.object();
|
|
||||||
object.properties.insert(name, schema);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,18 +21,12 @@ macro_rules! forward_impl {
|
||||||
<$target>::json_schema(gen)
|
<$target>::json_schema(gen)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn json_schema_for_flatten(gen: &mut SchemaGenerator) -> Schema {
|
fn _schemars_private_non_optional_json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||||
<$target>::json_schema_for_flatten(gen)
|
<$target>::_schemars_private_non_optional_json_schema(gen)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_schema_as_property(
|
fn _schemars_private_is_option() -> bool {
|
||||||
gen: &mut SchemaGenerator,
|
<$target>::_schemars_private_is_option()
|
||||||
parent: &mut crate::schema::SchemaObject,
|
|
||||||
name: String,
|
|
||||||
metadata: Option<crate::schema::Metadata>,
|
|
||||||
required: bool,
|
|
||||||
) {
|
|
||||||
<$target>::add_schema_as_property(gen, parent, name, metadata, required)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -301,6 +301,10 @@ mod ser;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod macros;
|
mod macros;
|
||||||
|
|
||||||
|
/// This module is only public for use by `schemars_derive`. It should not need to be used by code
|
||||||
|
/// outside of `schemars`, and should not be considered part of the public API.
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub mod _private;
|
||||||
pub mod gen;
|
pub mod gen;
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
pub mod visit;
|
pub mod visit;
|
||||||
|
@ -314,7 +318,7 @@ pub use schemars_derive::*;
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub use serde_json as _serde_json;
|
pub use serde_json as _serde_json;
|
||||||
|
|
||||||
use schema::{Schema, SchemaObject};
|
use schema::Schema;
|
||||||
|
|
||||||
/// A type which can be described as a JSON Schema document.
|
/// A type which can be described as a JSON Schema document.
|
||||||
///
|
///
|
||||||
|
@ -357,35 +361,16 @@ pub trait JsonSchema {
|
||||||
/// This should not return a `$ref` schema.
|
/// This should not return a `$ref` schema.
|
||||||
fn json_schema(gen: &mut gen::SchemaGenerator) -> Schema;
|
fn json_schema(gen: &mut gen::SchemaGenerator) -> Schema;
|
||||||
|
|
||||||
/// Helper for generating schemas for flattened `Option` fields.
|
// TODO document and bring into public API?
|
||||||
///
|
|
||||||
/// This should not need to be called or implemented by code outside of `schemars`,
|
|
||||||
/// and should not be considered part of the public API.
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
fn json_schema_for_flatten(gen: &mut gen::SchemaGenerator) -> Schema {
|
fn _schemars_private_non_optional_json_schema(gen: &mut gen::SchemaGenerator) -> Schema {
|
||||||
Self::json_schema(gen)
|
Self::json_schema(gen)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper for generating schemas for `Option` fields.
|
// TODO document and bring into public API?
|
||||||
///
|
|
||||||
/// This should not need to be called or implemented by code outside of `schemars`,
|
|
||||||
/// and should not be considered part of the public API.
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
fn add_schema_as_property(
|
fn _schemars_private_is_option() -> bool {
|
||||||
gen: &mut gen::SchemaGenerator,
|
false
|
||||||
parent: &mut SchemaObject,
|
|
||||||
name: String,
|
|
||||||
metadata: Option<schema::Metadata>,
|
|
||||||
required: bool,
|
|
||||||
) {
|
|
||||||
let mut schema = gen.subschema_for::<Self>();
|
|
||||||
schema = gen.apply_metadata(schema, metadata);
|
|
||||||
|
|
||||||
let object = parent.object();
|
|
||||||
if required {
|
|
||||||
object.required.insert(name.clone());
|
|
||||||
}
|
|
||||||
object.properties.insert(name, schema);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -70,18 +70,12 @@ fn derive_json_schema(
|
||||||
<#ty as schemars::JsonSchema>::json_schema(gen)
|
<#ty as schemars::JsonSchema>::json_schema(gen)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn json_schema_for_flatten(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
|
fn _schemars_private_non_optional_json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
|
||||||
<#ty as schemars::JsonSchema>::json_schema_for_flatten(gen)
|
<#ty as schemars::JsonSchema>::_schemars_private_non_optional_json_schema(gen)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_schema_as_property(
|
fn _schemars_private_is_option() -> bool {
|
||||||
gen: &mut schemars::gen::SchemaGenerator,
|
<#ty as schemars::JsonSchema>::_schemars_private_is_option()
|
||||||
parent: &mut schemars::schema::SchemaObject,
|
|
||||||
name: String,
|
|
||||||
metadata: Option<schemars::schema::Metadata>,
|
|
||||||
required: bool,
|
|
||||||
) {
|
|
||||||
<#ty as schemars::JsonSchema>::add_schema_as_property(gen, parent, name, metadata, required)
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -49,7 +49,7 @@ impl<'a> SchemaMetadata<'a> {
|
||||||
quote! {
|
quote! {
|
||||||
{
|
{
|
||||||
let schema = #schema_expr;
|
let schema = #schema_expr;
|
||||||
gen.apply_metadata(schema, #self)
|
schemars::_private::apply_metadata(schema, #self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -433,7 +433,7 @@ fn expr_for_struct(
|
||||||
let args = quote!(gen, &mut schema_object, #name.to_owned(), #metadata, #required);
|
let args = quote!(gen, &mut schema_object, #name.to_owned(), #metadata, #required);
|
||||||
|
|
||||||
quote_spanned! {ty.span()=>
|
quote_spanned! {ty.span()=>
|
||||||
<#ty as schemars::JsonSchema>::add_schema_as_property(#args);
|
schemars::_private::add_schema_as_property::<#ty>(#args);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
@ -448,7 +448,7 @@ fn expr_for_struct(
|
||||||
|
|
||||||
let gen = quote!(gen);
|
let gen = quote!(gen);
|
||||||
quote_spanned! {ty.span()=>
|
quote_spanned! {ty.span()=>
|
||||||
.flatten(<#ty as schemars::JsonSchema>::json_schema_for_flatten(#gen))
|
.flatten(schemars::_private::json_schema_for_flatten::<#ty>(#gen))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue