Allow opting out of ref schema generation
This commit is contained in:
parent
f4ca23ddeb
commit
42013cff01
2 changed files with 21 additions and 9 deletions
|
@ -18,6 +18,10 @@ impl SchemaGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn subschema_for<T: MakeSchema + 'static>(&mut self) -> Schema {
|
pub fn subschema_for<T: MakeSchema + 'static>(&mut self) -> Schema {
|
||||||
|
if !T::generates_ref_schema() {
|
||||||
|
return T::make_schema(self);
|
||||||
|
}
|
||||||
|
|
||||||
let type_id = TypeId::of::<T>();
|
let type_id = TypeId::of::<T>();
|
||||||
// TODO is there a nicer way to do this?
|
// TODO is there a nicer way to do this?
|
||||||
if !self.definitions.contains_key(&type_id) {
|
if !self.definitions.contains_key(&type_id) {
|
||||||
|
@ -52,13 +56,21 @@ impl SchemaGenerator {
|
||||||
schema
|
schema
|
||||||
}
|
}
|
||||||
|
|
||||||
fn schema_name<T: MakeSchema>() -> String {
|
pub fn into_root_schema_for<T: MakeSchema>(mut self) -> Schema {
|
||||||
let override_name = T::override_schema_name();
|
let schema = T::make_schema(&mut self);
|
||||||
if override_name.is_empty() {
|
if let Schema::Object(mut o) = schema {
|
||||||
type_name::<T>().to_owned()
|
o.schema = Some("http://json-schema.org/draft-07/schema#".to_owned());
|
||||||
} else {
|
o.title = Some(Self::schema_name::<T>());
|
||||||
override_name
|
for (_, (name, schema)) in self.definitions {
|
||||||
|
o.definitions.insert(name, schema);
|
||||||
}
|
}
|
||||||
|
return Schema::Object(o);
|
||||||
|
}
|
||||||
|
schema
|
||||||
|
}
|
||||||
|
|
||||||
|
fn schema_name<T: MakeSchema>() -> String {
|
||||||
|
T::override_schema_name().unwrap_or_else(|| type_name::<T>().to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_unique_name<T: MakeSchema>(&mut self) -> String {
|
fn make_unique_name<T: MakeSchema>(&mut self) -> String {
|
||||||
|
|
|
@ -4,11 +4,11 @@ use serde_json::json;
|
||||||
use std::collections::BTreeMap as Map;
|
use std::collections::BTreeMap as Map;
|
||||||
|
|
||||||
pub trait MakeSchema {
|
pub trait MakeSchema {
|
||||||
fn override_schema_name() -> String {
|
fn override_schema_name() -> Option<String> {
|
||||||
String::new()
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn makes_ref_schema() -> bool {
|
fn generates_ref_schema() -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue