From 2f05199f02e67cb7b8e820058ca522cb3128f6e8 Mon Sep 17 00:00:00 2001 From: weifanglab Date: Tue, 7 Jul 2026 15:55:42 +0800 Subject: [PATCH] Fix HexString JSON schema for empty byte strings Signed-off-by: weifanglab --- .../rollup-interface/src/common/hex_string.rs | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/rollup-interface/src/common/hex_string.rs b/crates/rollup-interface/src/common/hex_string.rs index 1f07b7834e..885e397213 100644 --- a/crates/rollup-interface/src/common/hex_string.rs +++ b/crates/rollup-interface/src/common/hex_string.rs @@ -33,8 +33,8 @@ impl schemars::JsonSchema for HexString { fn json_schema(_gen: &mut schemars::SchemaGenerator) -> schemars::Schema { schemars::json_schema!({ "type": "string", - "pattern": "^0x(?:[a-fA-F0-9]{2})+$", - "description": "A `0x`-prefixed hexadecimal string (uppercase or lowercase) of variable length, with an even number of hex digits.", + "pattern": "^0x(?:[a-fA-F0-9]{2})*$", + "description": "A `0x`-prefixed hexadecimal string (uppercase or lowercase) of variable length, including zero bytes, with an even number of hex digits.", }) } } @@ -273,4 +273,21 @@ mod tests { fn hex_string_str_roundtrip(item: HexString) { test_str_roundtrip(item); } + + #[test] + fn variable_length_hex_string_json_schema_allows_empty_string() { + let empty_hex = HexString(Vec::::new()); + + assert_eq!(serde_json::to_string(&empty_hex).unwrap(), "\"0x\""); + assert_eq!( + HexString::>::from_str("0x").unwrap(), + HexString(Vec::new()) + ); + + let mut schema_generator = schemars::SchemaGenerator::default(); + let schema = ::json_schema(&mut schema_generator); + let schema = serde_json::to_value(schema).unwrap(); + + assert_eq!(schema["pattern"], "^0x(?:[a-fA-F0-9]{2})*$"); + } }