Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Fix more instances of JSON schema getting clobbered when attaching references (#15339)
Marshall Bowers1ffb34c5fca4
diff --git a/Cargo.lock b/Cargo.lockindex 54405379abc..a71790233ee 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -9645,6 +9645,7 @@ dependencies = ["gpui","indoc","lazy_static",+ "log","paths","pretty_assertions","release_channel",diff --git a/crates/language/src/language_settings.rs b/crates/language/src/language_settings.rsindex cbd4ed68c3d..4a21ba8ee0a 100644--- a/crates/language/src/language_settings.rs+++ b/crates/language/src/language_settings.rs@@ -16,7 +16,7 @@ use serde::{Deserialize, Deserializer, Serialize,};use serde_json::Value;-use settings::{Settings, SettingsLocation, SettingsSources};+use settings::{add_references_to_properties, Settings, SettingsLocation, SettingsSources};use std::{num::NonZeroU32, path::Path, sync::Arc};use util::serde::default_true;@@ -1009,16 +1009,10 @@ impl settings::Settings for AllLanguageSettings {.definitions.extend([("Languages".into(), languages_object_schema.into())]);- root_schema- .schema- .object- .as_mut()- .unwrap()- .properties- .extend([(- "languages".to_owned(),- Schema::new_ref("#/definitions/Languages".into()),- )]);+ add_references_to_properties(+ &mut root_schema,+ &[("languages", "#/definitions/Languages")],+ );root_schema}diff --git a/crates/settings/Cargo.toml b/crates/settings/Cargo.tomlindex a60a910ed4e..e764cc79948 100644--- a/crates/settings/Cargo.toml+++ b/crates/settings/Cargo.toml@@ -22,6 +22,7 @@ fs.workspace = truefutures.workspace = truegpui.workspace = truelazy_static.workspace = true+log.workspace = truepaths.workspace = truerelease_channel.workspace = truerust-embed.workspace = truediff --git a/crates/settings/src/json_schema.rs b/crates/settings/src/json_schema.rsnew file mode 100644index 00000000000..f6e0aee1792--- /dev/null+++ b/crates/settings/src/json_schema.rs@@ -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.+ }+ }+ }+}diff --git a/crates/settings/src/settings.rs b/crates/settings/src/settings.rsindex c47301c7ca9..70e7085d454 100644--- a/crates/settings/src/settings.rs+++ b/crates/settings/src/settings.rs@@ -1,4 +1,5 @@mod editable_setting_control;+mod json_schema;mod keymap_file;mod settings_file;mod settings_store;@@ -9,6 +10,7 @@ use std::{borrow::Cow, str};use util::asset_str;pub use editable_setting_control::*;+pub use json_schema::*;pub use keymap_file::KeymapFile;pub use settings_file::*;pub use settings_store::{diff --git a/crates/terminal/src/terminal_settings.rs b/crates/terminal/src/terminal_settings.rsindex 4fbe2cdc867..1e1580cba63 100644--- a/crates/terminal/src/terminal_settings.rs+++ b/crates/terminal/src/terminal_settings.rs@@ -4,12 +4,12 @@ use gpui::{};use schemars::{gen::SchemaGenerator,- schema::{ArrayValidation, InstanceType, RootSchema, Schema, SchemaObject},+ schema::{ArrayValidation, InstanceType, RootSchema, SchemaObject},JsonSchema,};use serde_derive::{Deserialize, Serialize};use serde_json::Value;-use settings::{SettingsJsonSchemaParams, SettingsSources};+use settings::{add_references_to_properties, SettingsJsonSchemaParams, SettingsSources};use std::path::PathBuf;use task::Shell;@@ -231,22 +231,14 @@ impl settings::Settings for TerminalSettings {("FontFamilies".into(), font_family_schema.into()),("FontFallbacks".into(), font_fallback_schema.into()),]);- root_schema- .schema- .object- .as_mut()- .unwrap()- .properties- .extend([- (- "font_family".to_owned(),- Schema::new_ref("#/definitions/FontFamilies".into()),- ),- (- "font_fallbacks".to_owned(),- Schema::new_ref("#/definitions/FontFallbacks".into()),- ),- ]);++ add_references_to_properties(+ &mut root_schema,+ &[+ ("font_family", "#/definitions/FontFamilies"),+ ("font_fallbacks", "#/definitions/FontFallbacks"),+ ],+ );root_schema}diff --git a/crates/theme/src/settings.rs b/crates/theme/src/settings.rsindex 10647f0e77a..4ec1fa4d231 100644--- a/crates/theme/src/settings.rs+++ b/crates/theme/src/settings.rs@@ -15,7 +15,7 @@ use schemars::{};use serde::{Deserialize, Serialize};use serde_json::Value;-use settings::{Settings, SettingsJsonSchemaParams, SettingsSources};+use settings::{add_references_to_properties, Settings, SettingsJsonSchemaParams, SettingsSources};use std::sync::Arc;use util::ResultExt as _;@@ -649,30 +649,15 @@ impl settings::Settings for ThemeSettings {("FontFallbacks".into(), font_fallback_schema.into()),]);- // The list of properties that should reference another definition in- // the schema.- let properties_with_references = vec![- ("buffer_font_family", "#/definitions/FontFamilies"),- ("buffer_font_fallbacks", "#/definitions/FontFallbacks"),- ("ui_font_family", "#/definitions/FontFamilies"),- ("ui_font_fallbacks", "#/definitions/FontFallbacks"),- ];-- 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.into());- }- Schema::Bool(_) => {- // Boolean schemas can't have references.- }- }- }+ add_references_to_properties(+ &mut root_schema,+ &[+ ("buffer_font_family", "#/definitions/FontFamilies"),+ ("buffer_font_fallbacks", "#/definitions/FontFallbacks"),+ ("ui_font_family", "#/definitions/FontFamilies"),+ ("ui_font_fallbacks", "#/definitions/FontFallbacks"),+ ],+ );root_schema}