Skip to content

Commit 1af3ef3

Browse files
authored
EFF-697 Fix JSON RpcSerialization decode array double-wrap (#1722)
1 parent 6cc67c8 commit 1af3ef3

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Fix `RpcSerialization.json` decode so JSON array payloads are not wrapped in an extra outer array.

packages/effect/src/unstable/rpc/RpcSerialization.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ export const json: RpcSerialization["Service"] = RpcSerialization.of({
3737
makeUnsafe: () => {
3838
const decoder = new TextDecoder()
3939
return {
40-
decode: (bytes) => [JSON.parse(typeof bytes === "string" ? bytes : decoder.decode(bytes))],
40+
decode: (bytes) => {
41+
const decoded = JSON.parse(typeof bytes === "string" ? bytes : decoder.decode(bytes))
42+
return Array.isArray(decoded) ? decoded : [decoded]
43+
},
4144
encode: (response) => JSON.stringify(response)
4245
}
4346
}

packages/effect/test/rpc/RpcSerialization.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ const responseExitSuccess = (requestId: string, value: unknown) => ({
1111
})
1212

1313
describe("RpcSerialization", () => {
14+
it("json decode keeps array payloads flat", () => {
15+
const parser = RpcSerialization.json.makeUnsafe()
16+
const decoded = parser.decode("[1,2,3]")
17+
assert.strictEqual(decoded.length, 3)
18+
assert.deepStrictEqual(decoded, [1, 2, 3])
19+
})
20+
21+
it("json decode wraps non-array payloads", () => {
22+
const parser = RpcSerialization.json.makeUnsafe()
23+
const decoded = parser.decode("{\"a\":1}")
24+
assert.strictEqual(decoded.length, 1)
25+
assert.deepStrictEqual(decoded, [{ a: 1 }])
26+
})
27+
1428
it("jsonRpc encodes a non-batched single response array as an object", () => {
1529
const parser = RpcSerialization.jsonRpc().makeUnsafe()
1630
const decoded = parser.decode("{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"users.get\"}")

0 commit comments

Comments
 (0)