Skip to content

fix(kv): serialize maps as objects in KvStore::put - #1094

Open
PedroRosalba wants to merge 1 commit into
cloudflare:mainfrom
PedroRosalba:fix/kv-put-json-maps
Open

PedroRosalba wants to merge 1 commit into
cloudflare:mainfrom
PedroRosalba:fix/kv-put-json-maps

Conversation

@PedroRosalba

Copy link
Copy Markdown

Problem

KvStore::put serializes any non-string value with serde_wasm_bindgen::to_value and then JSON.stringify. The default serializer writes Rust maps (HashMap, BTreeMap, serde_json::Value::Object) as a JS Map and byte buffers as a Uint8Array; JSON.stringify turns a Map into {} and a Uint8Array into an object keyed by index. Every map-typed field is silently stored as {} and cannot be read back.

Same root cause as the queue producer fix in the sibling PR for Queue::send.

Fix

Serialize with serialize_maps_as_objects(true) and serialize_bytes_as_arrays(true), which is what the builder options in the same module already do. Strings, numbers and booleans keep their existing short-circuits.

Tests

Puts a struct with a HashMap and a serde_json::Value field, then checks both the Rust round-trip and the raw stored JSON text. Fails on main, passes with this change; full suite green.

Tested on macOS (Apple Silicon) only.

KvStore::put serializes any non-string value with
serde_wasm_bindgen::to_value and then JSON.stringify, but the default
serializer writes Rust maps (HashMap, BTreeMap, serde_json::Value::Object)
as JS Map and byte buffers as Uint8Array. JSON.stringify encodes a Map as
{} and a Uint8Array as an object keyed by index, so every map-typed field
is silently stored as {} and cannot be read back.

Serialize the value with serialize_maps_as_objects and
serialize_bytes_as_arrays, as the builder options already do. Strings,
numbers and booleans take the same short-circuits as before. Add a test
that puts a struct with a HashMap and a serde_json::Value field and checks
both the Rust round-trip and the raw stored JSON.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant