Skip to content

Commit 3f042a4

Browse files
committed
fix(client, server): prevent toJSON problem
1 parent c0dd7cd commit 3f042a4

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

packages/client/src/adapters/standard/rpc-json-serializer.test.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ const customSupportedDataTypes: { name: string, value: unknown, expected: unknow
4040
value: new Person2('unnoq - 2', [{ nested: new Date('2023-01-02') }, /uic/gi]),
4141
expected: new Person2('unnoq - 2', [{ nested: new Date('2023-01-02') }, /uic/gi]),
4242
},
43+
{
44+
name: 'should not resolve toJSON',
45+
value: { value: { toJSON: () => 'hello' } },
46+
expected: { value: { } },
47+
},
48+
{
49+
name: 'should resolve invalid toJSON',
50+
value: { value: { toJSON: 'hello' } },
51+
expected: { value: { toJSON: 'hello' } },
52+
},
4353
]
4454

4555
describe.each([
@@ -66,7 +76,14 @@ describe.each([
6676
function assert(value: unknown, expected: unknown) {
6777
const [json, meta, maps, blobs] = serializer.serialize(value)
6878

69-
const deserialized = serializer.deserialize(json, meta, maps, (i: number) => blobs[i]!)
79+
const result = JSON.parse(JSON.stringify({ json, meta, maps }))
80+
81+
const deserialized = serializer.deserialize(
82+
result.json,
83+
result.meta,
84+
result.maps,
85+
(i: number) => blobs[i]!,
86+
)
7087
expect(deserialized).toEqual(expected)
7188
}
7289

packages/client/src/adapters/standard/rpc-json-serializer.ts

+9
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ export class StandardRPCJsonSerializer {
112112
const json: Record<string, unknown> = {}
113113

114114
for (const k in data) {
115+
/**
116+
* Skip custom toJSON methods to avoid JSON.stringify invoking them,
117+
* which could cause meta and serialized data mismatches during deserialization.
118+
* Instead, rely on custom serializers.
119+
*/
120+
if (k === 'toJSON' && typeof data[k] === 'function') {
121+
continue
122+
}
123+
115124
json[k] = this.serialize(data[k], [...segments, k], meta, maps, blobs)[0]
116125
}
117126

packages/openapi-client/src/adapters/standard/openapi-json-serializer.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ const customSupportedDataTypes: TestCase[] = [
145145
data: new Person2('unnoq - 2', [{ nested: new Date('2023-01-02') }, /uic/gi]),
146146
expected: { name: 'unnoq - 2', data: [{ nested: '2023-01-02T00:00:00.000Z' }, '/uic/gi'] },
147147
},
148+
{
149+
data: { value: { toJSON: () => 'hello' } },
150+
expected: { value: { } },
151+
},
152+
{
153+
data: { value: { toJSON: 'hello' } },
154+
expected: { value: { toJSON: 'hello' } },
155+
},
148156
]
149157

150158
describe.each<TestCase>([

packages/openapi-client/src/adapters/standard/openapi-json-serializer.ts

+9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ export class StandardOpenAPIJsonSerializer {
4949
const json: Record<string, unknown> = {}
5050

5151
for (const k in data) {
52+
/**
53+
* Skip custom toJSON methods to avoid JSON.stringify invoking them,
54+
* which could cause meta and serialized data mismatches during deserialization.
55+
* Instead, rely on custom serializers.
56+
*/
57+
if (k === 'toJSON' && typeof data[k] === 'function') {
58+
continue
59+
}
60+
5261
json[k] = this.serialize(data[k], hasBlobRef)[0]
5362
}
5463

0 commit comments

Comments
 (0)