Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ const customSupportedDataTypes: { name: string, value: unknown, expected: unknow
value: new Person2('unnoq - 2', [{ nested: new Date('2023-01-02') }, /uic/gi]),
expected: new Person2('unnoq - 2', [{ nested: new Date('2023-01-02') }, /uic/gi]),
},
{
name: 'should not resolve toJSON',
value: { value: { toJSON: () => 'hello' } },
expected: { value: { } },
},
{
name: 'should resolve invalid toJSON',
value: { value: { toJSON: 'hello' } },
expected: { value: { toJSON: 'hello' } },
},
]

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

const deserialized = serializer.deserialize(json, meta, maps, (i: number) => blobs[i]!)
const result = JSON.parse(JSON.stringify({ json, meta, maps }))

const deserialized = serializer.deserialize(
result.json,
result.meta,
result.maps,
(i: number) => blobs[i]!,
)
expect(deserialized).toEqual(expected)
}

Expand Down
9 changes: 9 additions & 0 deletions packages/client/src/adapters/standard/rpc-json-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ export class StandardRPCJsonSerializer {
const json: Record<string, unknown> = {}

for (const k in data) {
/**
* Skip custom toJSON methods to avoid JSON.stringify invoking them,
* which could cause meta and serialized data mismatches during deserialization.
* Instead, rely on custom serializers.
*/
if (k === 'toJSON' && typeof data[k] === 'function') {
continue
}

json[k] = this.serialize(data[k], [...segments, k], meta, maps, blobs)[0]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ const customSupportedDataTypes: TestCase[] = [
data: new Person2('unnoq - 2', [{ nested: new Date('2023-01-02') }, /uic/gi]),
expected: { name: 'unnoq - 2', data: [{ nested: '2023-01-02T00:00:00.000Z' }, '/uic/gi'] },
},
{
data: { value: { toJSON: () => 'hello' } },
expected: { value: { } },
},
{
data: { value: { toJSON: 'hello' } },
expected: { value: { toJSON: 'hello' } },
},
]

describe.each<TestCase>([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ export class StandardOpenAPIJsonSerializer {
const json: Record<string, unknown> = {}

for (const k in data) {
/**
* Skip custom toJSON methods to avoid JSON.stringify invoking them,
* which could cause meta and serialized data mismatches during deserialization.
* Instead, rely on custom serializers.
*/
if (k === 'toJSON' && typeof data[k] === 'function') {
continue
}

json[k] = this.serialize(data[k], hasBlobRef)[0]
}

Expand Down