Skip to content

Commit 208f806

Browse files
committed
Preserve raw JSON fallback for malformed help_text properties
Only extract properties.help_text when the schema is well-formed, matching the existing malformed-schema warning in assign_setting_display_orders.
1 parent e5190fd commit 208f806

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

src/api/edge_app/setting.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -222,33 +222,39 @@ fn is_structured_help_text(help_text: &str) -> bool {
222222
serde_json::from_str::<Value>(help_text).is_ok_and(|value| value.is_object())
223223
}
224224

225-
pub(crate) fn extract_display_help_text(help_text: &Value) -> String {
226-
let object = match help_text {
227-
Value::Object(_) => Some(help_text.clone()),
228-
Value::String(raw) if raw.trim_start().starts_with('{') => {
229-
serde_json::from_str::<Value>(raw)
230-
.ok()
231-
.filter(|value| value.is_object())
232-
}
233-
_ => None,
234-
};
225+
fn properties_are_malformed(object: &serde_json::Map<String, Value>) -> bool {
226+
matches!(object.get("properties"), Some(value) if !value.is_object())
227+
}
235228

236-
match object {
237-
Some(object) => object
229+
pub(crate) fn extract_display_help_text(help_text: &Value) -> String {
230+
match help_text {
231+
Value::Object(object) if !properties_are_malformed(object) => object
238232
.get("properties")
239233
.and_then(|properties| properties.get("help_text"))
240234
.and_then(|value| value.as_str())
241235
.unwrap_or_default()
242236
.to_string(),
243-
None => help_text.as_str().unwrap_or_default().to_string(),
237+
Value::String(raw) if raw.trim_start().starts_with('{') => {
238+
match serde_json::from_str::<Value>(raw) {
239+
Ok(Value::Object(object)) if !properties_are_malformed(&object) => object
240+
.get("properties")
241+
.and_then(|properties| properties.get("help_text"))
242+
.and_then(|value| value.as_str())
243+
.unwrap_or_default()
244+
.to_string(),
245+
_ => raw.clone(),
246+
}
247+
}
248+
Value::String(raw) => raw.clone(),
249+
_ => String::new(),
244250
}
245251
}
246252

247253
fn has_malformed_properties(help_text: &str) -> bool {
248254
let Ok(Value::Object(object)) = serde_json::from_str::<Value>(help_text) else {
249255
return false;
250256
};
251-
matches!(object.get("properties"), Some(value) if !value.is_object())
257+
properties_are_malformed(&object)
252258
}
253259

254260
pub fn help_text_with_display_order(name: &str, help_text: &str, display_order: usize) -> String {
@@ -638,6 +644,13 @@ mod display_order_tests {
638644
assert_eq!(extract_display_help_text(&json!(structured)), "");
639645
}
640646

647+
#[test]
648+
fn extract_display_help_text_returns_raw_json_for_malformed_properties() {
649+
let malformed = json!({ "schema_version": 1, "properties": "nope" }).to_string();
650+
651+
assert_eq!(extract_display_help_text(&json!(malformed)), malformed);
652+
}
653+
641654
#[test]
642655
fn extract_display_help_text_accepts_a_nested_object_value() {
643656
let structured = json!({

0 commit comments

Comments
 (0)