Fix more instances of JSON schema getting clobbered when attaching references (#15339)
This PR extends the fix from #15336 to more places that had the same issue. An `add_references_to_properties` helper function has been added to handle these cases uniformly. Release Notes: - N/A
This commit is contained in:
parent
8b22f09b6f
commit
1ffb34c5fc
7 changed files with 65 additions and 54 deletions
36
crates/settings/src/json_schema.rs
Normal file
36
crates/settings/src/json_schema.rs
Normal file
|
@ -0,0 +1,36 @@
|
|||
use schemars::schema::{RootSchema, Schema};
|
||||
|
||||
type PropertyName<'a> = &'a str;
|
||||
type ReferencePath<'a> = &'a str;
|
||||
|
||||
/// Modifies the provided [`RootSchema`] by adding references to all of the specified properties.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # let root_schema = RootSchema::default();
|
||||
/// add_references_to_properties(&mut root_schema, &[
|
||||
/// ("property_a", "#/definitions/DefinitionA"),
|
||||
/// ("property_b", "#/definitions/DefinitionB"),
|
||||
/// ])
|
||||
/// ```
|
||||
pub fn add_references_to_properties(
|
||||
root_schema: &mut RootSchema,
|
||||
properties_with_references: &[(PropertyName, ReferencePath)],
|
||||
) {
|
||||
for (property, definition) in properties_with_references {
|
||||
let Some(schema) = root_schema.schema.object().properties.get_mut(*property) else {
|
||||
log::warn!("property '{property}' not found in JSON schema");
|
||||
continue;
|
||||
};
|
||||
|
||||
match schema {
|
||||
Schema::Object(schema) => {
|
||||
schema.reference = Some(definition.to_string());
|
||||
}
|
||||
Schema::Bool(_) => {
|
||||
// Boolean schemas can't have references.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue