Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(remix-server-runtime): fix Jsonify for objects with toJSON function #7736

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
34 changes: 29 additions & 5 deletions packages/remix-server-runtime/jsonify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type Jsonify<T> =
IsAny<T> extends true ? any :

// toJSON
T extends { toJSON(): infer U } ? (U extends JsonValue ? U : unknown) :
T extends { toJSON(): infer U } ? (U extends JsonValue ? U : Jsonify<U>) :

// primitives
T extends JsonPrimitive ? T :
Expand Down Expand Up @@ -49,7 +49,7 @@ export type Jsonify<T> =

// value is always not JSON => true
// value is always JSON => false
// value is somtimes JSON, sometimes not JSON => boolean
// value is sometimes JSON, sometimes not JSON => boolean
// note: cannot be inlined as logic requires union distribution
type ValueIsNotJson<T> = T extends NotJson ? true : false;

Expand Down Expand Up @@ -150,8 +150,15 @@ type _tests = [
// toJson
Expect<Equal<Jsonify<{ toJSON(): "stuff" }>, "stuff">>,
Expect<Equal<Jsonify<Date>, string>>,
Expect<Equal<Jsonify<{ toJSON(): undefined }>, unknown>>,
Expect<Equal<Jsonify<{ toJSON(): Date }>, unknown>>,
Expect<Equal<Jsonify<{ toJSON(): any }>, any>>,
Expect<Equal<Jsonify<{ toJSON(): undefined }>, never>>,
Expect<Equal<Jsonify<{ toJSON(): Date }>, string>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't checked the other test cases, but this one is incorrect.

let result = JSON.parse(JSON.stringify({ toJSON: () => new Date() }))
// => {}

So the result is an empty object, not a string. toJSON doesn't seem to always recursively serialize the return value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was checking, which did respond with a string

let result = JSON.parse(JSON.stringify({ toJSON: () => Date() }))
// => 'Mon Oct 23 2023 03:45:02 GMT+0200 (Midden-Europese zomertijd)'

Expect<Equal<Jsonify<{ toJSON(): Map<unknown, unknown> }>, EmptyObject>>,
Expect<Equal<Jsonify<{ toJSON(): Int8Array}>, Record<string, number>>>,
Expect<Equal<Jsonify<{ toJSON(): [1, "two", Date, undefined, false]}>, [1, "two", string, null, false]>>,
Expect<Equal<Jsonify<{ toJSON(): [unknown, 1]}>, [unknown, 1]>>,
// @ts-expect-error
Expect<Equal<Jsonify<{ toJSON(): MyClass}>, {a: string}>>,
Expect<Equal<Jsonify<BooleanWithToJson>, string>>,


Expand All @@ -164,14 +171,31 @@ type _tests = [

// object
Expect<Equal<Pretty<Jsonify<{}>>, {}>>,
// @ts-expect-error
Expect<Equal<Pretty<Jsonify<{a: any}>>, {a: any}>>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't really get why this one is failing tbh 🤔
If I change this to be equal to {a?: any}, this passes though

Expect<Equal<Pretty<Jsonify<{a: string}>>, {a: string}>>,
Expect<Equal<Pretty<Jsonify<{a: Date}>>, {a: string}>>,
Expect<Equal<Pretty<Jsonify<{a: Map<unknown, unknown>}>>, {a: EmptyObject}>>,
Expect<Equal<Pretty<Jsonify<{a: Int8Array}>>, {a: Record<string, number>}>>,
Expect<Equal<Pretty<Jsonify<{a: [1, "two", Date, undefined, false]}>>, {a: [1, "two", string, null, false]}>>,
Expect<Equal<Pretty<Jsonify<{a: [unknown, 1]}>>, {a: [unknown, 1]}>>,
// @ts-expect-error
Expect<Equal<Pretty<Jsonify<{a: MyClass}>>, {a: {a: string}}>>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't seem to get these nested classes to work

@pcattori probably has a good idea where to look at

Expect<Equal<Pretty<Jsonify<{a: string | undefined}>>, {a?: string}>>,
Expect<Equal<Pretty<Jsonify<{a?: string | undefined}>>, {a?: string}>>,
Expect<Equal<Pretty<Jsonify<{a?: any}>>, {a?: any}>>,
Expect<Equal<Pretty<Jsonify<{a?: Date}>>, {a?: string}>>,
Expect<Equal<Pretty<Jsonify<{a?: Map<unknown, unknown>}>>, {a?: EmptyObject}>>,
Expect<Equal<Pretty<Jsonify<{a?: Int8Array}>>, {a?: Record<string, number>}>>,
Expect<Equal<Pretty<Jsonify<{a?: [1, "two", Date, undefined, false]}>>, {a?: [1, "two", string, null, false]}>>,
Expect<Equal<Pretty<Jsonify<{a?: [unknown, 1]}>>, {a?: [unknown, 1]}>>,
// @ts-expect-error
Expect<Equal<Pretty<Jsonify<{a?: MyClass}>>, {a?: {a: string}}>>,
Expect<Equal<Pretty<Jsonify<{a: string, b: string | undefined, c: undefined}>>, {a: string, b?: string}>>,
Expect<Equal<Pretty<Jsonify<{a: undefined}>>, {}>>,
Expect<Equal<Pretty<Jsonify<Record<string, any>>>, Record<string, any>>>,
Expect<Equal<Pretty<Jsonify<Record<string, number>>>, Record<string, number>>>,
Expect<MutualExtends<Jsonify<{payload: Record<string, any>}>, { payload: Record<string, any>}>>,
Expect<MutualExtends<Jsonify<{payload: Record<string, any>}>, {payload: Record<string, any>}>>,
Expect<Equal<Pretty<Jsonify<{
// Should be kept
requiredString: string;
Expand Down