Fix remote server (ssh) crash when editing json (#33818)

Closes #33807

Release Notes:

- (Preview Only) Fixes a remote server (ssh) crash when editing json
files

---------

Co-authored-by: Cole <cole@zed.dev>
This commit is contained in:
Michael Sloan 2025-07-02 18:21:39 -06:00 committed by GitHub
parent 77c4530e12
commit 32d058d95e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 27 additions and 38 deletions

View file

@ -321,7 +321,7 @@ inventory::submit! {
let language_settings_content_ref = generator let language_settings_content_ref = generator
.subschema_for::<LanguageSettingsContent>() .subschema_for::<LanguageSettingsContent>()
.to_value(); .to_value();
let schema = json_schema!({ replace_subschema::<LanguageToSettingsMap>(generator, || json_schema!({
"type": "object", "type": "object",
"properties": params "properties": params
.language_names .language_names
@ -333,8 +333,7 @@ inventory::submit! {
) )
}) })
.collect::<serde_json::Map<_, _>>() .collect::<serde_json::Map<_, _>>()
}); }))
replace_subschema::<LanguageToSettingsMap>(generator, schema)
} }
} }
} }

View file

@ -23,35 +23,26 @@ inventory::collect!(ParameterizedJsonSchema);
const DEFS_PATH: &str = "#/$defs/"; const DEFS_PATH: &str = "#/$defs/";
/// Replaces the JSON schema definition for some type, and returns a reference to it. /// Replaces the JSON schema definition for some type if it is in use (in the definitions list), and
/// returns a reference to it.
///
/// This asserts that JsonSchema::schema_name() + "2" does not exist because this indicates that
/// there are multiple types that use this name, and unfortunately schemars APIs do not support
/// resolving this ambiguity - see https://github.com/GREsau/schemars/issues/449
///
/// This takes a closure for `schema` because some settings types are not available on the remote
/// server, and so will crash when attempting to access e.g. GlobalThemeRegistry.
pub fn replace_subschema<T: JsonSchema>( pub fn replace_subschema<T: JsonSchema>(
generator: &mut schemars::SchemaGenerator, generator: &mut schemars::SchemaGenerator,
schema: schemars::Schema, schema: impl Fn() -> schemars::Schema,
) -> schemars::Schema { ) -> schemars::Schema {
// The key in definitions may not match T::schema_name() if multiple types have the same name.
// This is a workaround for there being no straightforward way to get the key used for a type -
// see https://github.com/GREsau/schemars/issues/449
let ref_schema = generator.subschema_for::<T>();
if let Some(serde_json::Value::String(definition_pointer)) = ref_schema.get("$ref") {
if let Some(definition_name) = definition_pointer.strip_prefix(DEFS_PATH) {
generator
.definitions_mut()
.insert(definition_name.to_string(), schema.to_value());
return ref_schema;
} else {
log::error!(
"bug: expected `$ref` field to start with {DEFS_PATH}, \
got {definition_pointer}"
);
}
} else {
log::error!("bug: expected `$ref` field in result of `subschema_for`");
}
// fallback on just using the schema name, which could collide. // fallback on just using the schema name, which could collide.
let schema_name = T::schema_name(); let schema_name = T::schema_name();
generator let definitions = generator.definitions_mut();
.definitions_mut() assert!(!definitions.contains_key(&format!("{schema_name}2")));
.insert(schema_name.to_string(), schema.to_value()); if definitions.contains_key(schema_name.as_ref()) {
definitions.insert(schema_name.to_string(), schema().to_value());
}
Schema::new_ref(format!("{DEFS_PATH}{schema_name}")) Schema::new_ref(format!("{DEFS_PATH}{schema_name}"))
} }

View file

@ -978,11 +978,10 @@ pub struct ThemeName(pub Arc<str>);
inventory::submit! { inventory::submit! {
ParameterizedJsonSchema { ParameterizedJsonSchema {
add_and_get_ref: |generator, _params, cx| { add_and_get_ref: |generator, _params, cx| {
let schema = json_schema!({ replace_subschema::<ThemeName>(generator, || json_schema!({
"type": "string", "type": "string",
"enum": ThemeRegistry::global(cx).list_names(), "enum": ThemeRegistry::global(cx).list_names(),
}); }))
replace_subschema::<ThemeName>(generator, schema)
} }
} }
} }
@ -996,15 +995,14 @@ pub struct IconThemeName(pub Arc<str>);
inventory::submit! { inventory::submit! {
ParameterizedJsonSchema { ParameterizedJsonSchema {
add_and_get_ref: |generator, _params, cx| { add_and_get_ref: |generator, _params, cx| {
let schema = json_schema!({ replace_subschema::<IconThemeName>(generator, || json_schema!({
"type": "string", "type": "string",
"enum": ThemeRegistry::global(cx) "enum": ThemeRegistry::global(cx)
.list_icon_themes() .list_icon_themes()
.into_iter() .into_iter()
.map(|icon_theme| icon_theme.name) .map(|icon_theme| icon_theme.name)
.collect::<Vec<SharedString>>(), .collect::<Vec<SharedString>>(),
}); }))
replace_subschema::<IconThemeName>(generator, schema)
} }
} }
} }
@ -1018,11 +1016,12 @@ pub struct FontFamilyName(pub Arc<str>);
inventory::submit! { inventory::submit! {
ParameterizedJsonSchema { ParameterizedJsonSchema {
add_and_get_ref: |generator, params, _cx| { add_and_get_ref: |generator, params, _cx| {
let schema = json_schema!({ replace_subschema::<FontFamilyName>(generator, || {
"type": "string", json_schema!({
"enum": params.font_names, "type": "string",
}); "enum": params.font_names,
replace_subschema::<FontFamilyName>(generator, schema) })
})
} }
} }
} }