Refactor out some unneeded clones
This commit is contained in:
parent
49b2d1f89d
commit
b9e9360b5c
2 changed files with 30 additions and 29 deletions
|
@ -90,10 +90,10 @@ impl SchemaGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
let name = T::schema_name();
|
let name = T::schema_name();
|
||||||
if !self.definitions.contains_key(&name) {
|
|
||||||
self.insert_new_subschema_for::<T>(name.clone())?;
|
|
||||||
}
|
|
||||||
let reference = format!("{}{}", self.settings().definitions_path, name);
|
let reference = format!("{}{}", self.settings().definitions_path, name);
|
||||||
|
if !self.definitions.contains_key(&name) {
|
||||||
|
self.insert_new_subschema_for::<T>(name)?;
|
||||||
|
}
|
||||||
Ok(Ref { reference }.into())
|
Ok(Ref { reference }.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,7 +104,7 @@ impl SchemaGenerator {
|
||||||
|
|
||||||
match T::json_schema(self) {
|
match T::json_schema(self) {
|
||||||
Ok(schema) => {
|
Ok(schema) => {
|
||||||
self.definitions.insert(name.clone(), schema);
|
self.definitions.insert(name, schema);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
@ -148,11 +148,10 @@ impl SchemaGenerator {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_schema_object(&self, schema: &Schema) -> Result<SchemaObject> {
|
pub fn get_schema_object(&self, mut schema: Schema) -> Result<SchemaObject> {
|
||||||
let mut schema = schema;
|
|
||||||
loop {
|
loop {
|
||||||
match schema {
|
match schema {
|
||||||
Schema::Object(o) => return Ok(o.clone()),
|
Schema::Object(obj) => return Ok(obj),
|
||||||
Schema::Bool(true) => return Ok(Default::default()),
|
Schema::Bool(true) => return Ok(Default::default()),
|
||||||
Schema::Bool(false) => {
|
Schema::Bool(false) => {
|
||||||
return Ok(SchemaObject {
|
return Ok(SchemaObject {
|
||||||
|
@ -160,30 +159,32 @@ impl SchemaGenerator {
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Schema::Ref(r) => {
|
Schema::Ref(schema_ref) => {
|
||||||
let definitions_path_len = self.settings().definitions_path.len();
|
let definitions_path = &self.settings().definitions_path;
|
||||||
let name = r.reference.get(definitions_path_len..).ok_or_else(|| {
|
if !schema_ref.reference.starts_with(definitions_path) {
|
||||||
JsonSchemaError::new(
|
return Err(JsonSchemaError::new(
|
||||||
"Could not extract referenced schema name.",
|
"Could not extract referenced schema name.",
|
||||||
Schema::Ref(r.clone()),
|
Schema::Ref(schema_ref),
|
||||||
)
|
));
|
||||||
})?;
|
}
|
||||||
|
|
||||||
schema = self.definitions.get(name).ok_or_else(|| {
|
let name = &schema_ref.reference[definitions_path.len()..];
|
||||||
JsonSchemaError::new(
|
schema = self
|
||||||
"Could not find referenced schema.",
|
.definitions
|
||||||
Schema::Ref(r.clone()),
|
.get(name)
|
||||||
)
|
.ok_or_else(|| {
|
||||||
})?;
|
JsonSchemaError::new(
|
||||||
|
"Could not find referenced schema.",
|
||||||
|
Schema::Ref(schema_ref.clone()),
|
||||||
|
)
|
||||||
|
})?
|
||||||
|
.clone();
|
||||||
|
|
||||||
match schema {
|
if schema == Schema::Ref(schema_ref) {
|
||||||
Schema::Ref(r2) if r2 == r => {
|
return Err(JsonSchemaError::new(
|
||||||
return Err(JsonSchemaError::new(
|
"Schema is referencing itself.",
|
||||||
"Schema is referencing itself.",
|
schema,
|
||||||
schema.clone(),
|
));
|
||||||
));
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ impl<T: JsonSchema> JsonSchema for Option<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if gen.settings().option_nullable {
|
if gen.settings().option_nullable {
|
||||||
let mut deref = gen.get_schema_object(&schema)?;
|
let mut deref = gen.get_schema_object(schema)?;
|
||||||
deref.extensions.insert("nullable".to_owned(), json!(true));
|
deref.extensions.insert("nullable".to_owned(), json!(true));
|
||||||
schema = Schema::Object(deref);
|
schema = Schema::Object(deref);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue