Skip to content

Commit 7c9e4fa

Browse files
committed
Fix xdr_to_json panic for ScType::Val with non-Void values
When a contract spec declares a return type as Val (the generic catch-all type), any concrete runtime value other than Void falls through to the todo!() panic in xdr_to_json. This causes the CLI to crash on output formatting even though the transaction succeeded. Delegate ScType::Val to to_json() which already handles all ScVal variants without needing type information. Close #2469
1 parent b36124d commit 7c9e4fa

File tree

1 file changed

+34
-0
lines changed
  • cmd/crates/soroban-spec-tools/src

1 file changed

+34
-0
lines changed

cmd/crates/soroban-spec-tools/src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,9 @@ impl Spec {
585585
(val, ScType::Result(inner)) => self.xdr_to_json(val, &inner.ok_type)?,
586586

587587
(val, ScType::Option(inner)) => self.xdr_to_json(val, &inner.value_type)?,
588+
589+
(val, ScType::Val) => to_json(val)?,
590+
588591
(ScVal::Map(Some(_)) | ScVal::Vec(Some(_)) | ScVal::U32(_), type_) => {
589592
self.sc_object_to_json(val, type_)?
590593
}
@@ -2450,4 +2453,35 @@ mod tests {
24502453
let json = spec.sc_map_to_json(&sc_map, &map_type).unwrap();
24512454
assert_eq!(json.to_string(), r#"{"foo":2}"#);
24522455
}
2456+
2457+
#[test]
2458+
fn test_xdr_to_json_sc_type_val_delegates_to_to_json() {
2459+
// Regression for #2469: when a contract spec declares the return type
2460+
// as Val (the generic catch-all), xdr_to_json used to panic via
2461+
// todo!() for any non-Void ScVal. It must now delegate to to_json()
2462+
// and produce the same output regardless of the underlying variant.
2463+
let spec = Spec(None);
2464+
2465+
let cases = vec![
2466+
ScVal::Void,
2467+
ScVal::Bool(true),
2468+
ScVal::U32(7),
2469+
ScVal::U64(42),
2470+
ScVal::I64(-42),
2471+
ScVal::Symbol(ScSymbol("hello".try_into().unwrap())),
2472+
ScVal::String(ScString("world".try_into().unwrap())),
2473+
ScVal::Bytes(ScBytes(vec![0xbe, 0xef].try_into().unwrap())),
2474+
];
2475+
2476+
for val in cases {
2477+
let direct = to_json(&val).unwrap();
2478+
let via_xdr = spec
2479+
.xdr_to_json(&val, &ScType::Val)
2480+
.unwrap_or_else(|e| panic!("xdr_to_json panicked for {val:?}: {e:?}"));
2481+
assert_eq!(
2482+
direct, via_xdr,
2483+
"ScType::Val should delegate to to_json for {val:?}"
2484+
);
2485+
}
2486+
}
24532487
}

0 commit comments

Comments
 (0)