Skip to content

Commit 2b26629

Browse files
committed
Simplify wrap: remove root parameter, unify hash handling
1 parent a1e000f commit 2b26629

2 files changed

Lines changed: 15 additions & 23 deletions

File tree

lib/liquid/spec/json_rpc/drop_proxy.rb

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class << self
4444
# Drops become { "_rpc_drop": "id", "type": "ClassName" }
4545
# Primitives pass through unchanged
4646
# Hashes and arrays are recursively wrapped
47-
def wrap(obj, registry, seen = {}.compare_by_identity, root: true)
47+
def wrap(obj, registry, seen = {}.compare_by_identity)
4848
# Return placeholder for circular references to prevent JSON nesting errors
4949
return "[circular]" if seen.key?(obj)
5050

@@ -68,40 +68,29 @@ def wrap(obj, registry, seen = {}.compare_by_identity, root: true)
6868
# Ensure valid UTF-8 for JSON encoding
6969
sanitize_string(obj)
7070
when Hash
71-
if root
72-
# Root hash = environment. Keys are variable names — convert
73-
# symbols to strings (normal behavior for JSON transport).
74-
wrapped = {}
75-
seen[obj] = wrapped
76-
obj.each do |k, v|
77-
key = k.is_a?(String) ? sanitize_string(k) : k.to_s
78-
wrapped[key] = wrap(v, registry, seen, root: false)
79-
end
80-
wrapped
81-
elsif obj.keys.any? { |k| !k.is_a?(String) }
82-
# Inner hash with non-string keys (Symbol, Integer, etc.) —
71+
if obj.keys.any? { |k| !k.is_a?(String) }
72+
# Hash with non-string keys (Symbol, Integer, etc.) —
8373
# can't be faithfully represented in JSON. Send a _ruby_type
8474
# marker with the inspect string (for {{ v }} output) and a
8575
# JSON-safe version (for hash access like {{ v.foo }}).
8676
json_safe = {}
8777
obj.each do |k, v|
88-
json_safe[k.to_s] = wrap(v, registry, seen, root: false)
78+
json_safe[k.to_s] = wrap(v, registry, seen)
8979
end
9080
{ RUBY_TYPE_KEY => "Hash", "inspect" => obj.inspect, "data" => json_safe }
9181
else
92-
# Inner hash with string keys — normal JSON transport
82+
# String-keyed hash — normal JSON transport
9383
wrapped = {}
9484
seen[obj] = wrapped
9585
obj.each do |k, v|
96-
wrapped[k] = wrap(v, registry, seen, root: false)
86+
wrapped[k] = wrap(v, registry, seen)
9787
end
9888
wrapped
9989
end
10090
when Array
10191
wrapped = []
10292
seen[obj] = wrapped
103-
obj.each { |v| wrapped << wrap(v, registry, seen, root: false) }
104-
wrapped
93+
obj.each { |v| wrapped << wrap(v, registry, seen) }
10594
when Range
10695
# Ranges can't be faithfully represented in JSON.
10796
# Send a _ruby_type marker so the server can reconstruct

test/json_rpc_test.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,18 @@ def test_wrap_hash
149149
assert_equal({ "a" => 1, "b" => "hello" }, result)
150150
end
151151

152-
def test_wrap_hash_with_symbol_keys_root
153-
# Root hash = environment. Symbol keys are variable names → converted
154-
# to strings for JSON transport.
152+
def test_wrap_hash_with_symbol_keys_ruby_type
153+
# Hash with symbol keys — can't be faithfully represented in JSON.
154+
# Sent as _ruby_type marker with inspect and JSON-safe data.
155155
input = { a: 1, b: "hello" }
156156
result = DropProxy.wrap(input, @registry)
157157

158-
assert_equal({ "a" => 1, "b" => "hello" }, result)
158+
assert_equal "Hash", result["_ruby_type"]
159+
assert result["inspect"].include?("a")
160+
assert_equal({ "a" => 1, "b" => "hello" }, result["data"])
159161
end
160-
def test_wrap_inner_hash_with_symbol_keys_ruby_type
162+
163+
def test_wrap_inner_hash_with_integer_keys_ruby_type
161164
# Inner hash with symbol keys — sent as _ruby_type marker with inspect
162165
# string (for {{ v }} output) and JSON-safe data (for hash access).
163166
input = { "v" => { a: 1, b: "hello" } }

0 commit comments

Comments
 (0)