Skip to content

Commit

Permalink
fix: change Jsonlike behavior for response body to allow both JsonVal…
Browse files Browse the repository at this point in the history
…ue and original type
  • Loading branch information
Coobaha committed Oct 30, 2023
1 parent 4f32cd4 commit b8716f6
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type NotJsonable = ((...arguments_: any[]) => any) | undefined | symbol | RegExp

type NeverToNull<T> = IsNever<T> extends true ? null : T;

type JsonCastBehavior = 'cast' | 'no-cast';
type JsonCastBehavior = 'cast' | 'combine';

// Handles tuples and arrays
type JsonlikeList<T extends unknown[], DoNotCastToPrimitive extends JsonCastBehavior> = T extends []
Expand All @@ -43,8 +43,8 @@ export type Jsonlike<T, CastBehavior extends JsonCastBehavior> = T extends Posit
toJSON(): infer J;
}
? (() => J) extends () => JsonValue // Is J assignable to JsonValue?
? CastBehavior extends 'no-cast'
? T
? CastBehavior extends 'combine'
? T | J
: J // Then T is Jsonable and its Jsonable value is J
: Jsonlike<J, CastBehavior> // Maybe if we look a level deeper we'll find a JsonValue
: // Instanced primitives are objects
Expand Down
2 changes: 1 addition & 1 deletion src/typed-fastify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ interface Reply<
]
: [Get2<Op['response'], Status, 'content'>] extends [never]
? []
: [Jsonlike<Get2<Op['response'], Status, 'content'>, 'no-cast'>]
: [Jsonlike<Get2<Op['response'], Status, 'content'>, 'combine'>]
): AsReply;

readonly request: Request<ServiceSchema, Op, Path, RawServer, RawRequest>;
Expand Down
32 changes: 29 additions & 3 deletions tap-snapshots/test/integration.test.ts.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,11 @@ Object {
"format": "date-time",
"type": "string",
},
"dateString": Object {
"format": "date-time",
"maxLength": 5,
"type": "string",
},
"regexpType": Object {
"type": "string",
},
Expand All @@ -1106,6 +1111,7 @@ Object {
},
"required": Array [
"date",
"dateString",
"regexpType",
"type",
],
Expand All @@ -1121,13 +1127,14 @@ Object {
"Headers": Array [
"HTTP/1.1 200 OK",
"content-type: application/json; charset=utf-8",
"content-length: 73",
"content-length: 104",
"Date: dateString",
"Connection: keep-alive",
],
"Payload": Array [
Object {
"date": "1970-01-01T00:00:00.000Z",
"dateString": "Thu Jan 01 1970",
"regexpType": "string",
"type": "string",
},
Expand Down Expand Up @@ -1173,6 +1180,11 @@ Object {
"format": "date-time",
"type": "string",
},
"dateString": Object {
"format": "date-time",
"maxLength": 5,
"type": "string",
},
"regexpType": Object {
"type": "string",
},
Expand All @@ -1182,6 +1194,7 @@ Object {
},
"required": Array [
"date",
"dateString",
"regexpType",
"type",
],
Expand All @@ -1197,13 +1210,14 @@ Object {
"Headers": Array [
"HTTP/1.1 200 OK",
"content-type: application/json; charset=utf-8",
"content-length: 73",
"content-length: 104",
"Date: dateString",
"Connection: keep-alive",
],
"Payload": Array [
Object {
"date": "1970-01-01T00:00:00.000Z",
"dateString": "Thu Jan 01 1970",
"regexpType": "string",
"type": "string",
},
Expand Down Expand Up @@ -1409,7 +1423,7 @@ Object {
"Headers": Array [
"HTTP/1.1 200 OK",
"content-type: application/json; charset=utf-8",
"content-length: 4445",
"content-length: 4524",
"Date: dateString",
"Connection: keep-alive",
],
Expand Down Expand Up @@ -1706,6 +1720,11 @@ Object {
"format": "date-time",
"type": "string",
},
"dateString": Object {
"format": "date-time",
"maxLength": 5,
"type": "string",
},
"regexpType": Object {
"type": "string",
},
Expand All @@ -1715,6 +1734,7 @@ Object {
},
"required": Array [
"date",
"dateString",
"regexpType",
"type",
],
Expand Down Expand Up @@ -2160,6 +2180,11 @@ Object {
"format": "date-time",
"type": "string",
},
"dateString": Object {
"format": "date-time",
"maxLength": 5,
"type": "string",
},
"regexpType": Object {
"type": "string",
},
Expand All @@ -2169,6 +2194,7 @@ Object {
},
"required": Array [
"date",
"dateString",
"regexpType",
"type",
],
Expand Down
1 change: 1 addition & 0 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const defaultService: Service<TestSchema> = {

return reply.status(200).send({
date: new Date(date),
dateString: new Date(date).toDateString(),
type: typeof date,
regexpType: typeof regexp,
});
Expand Down
9 changes: 7 additions & 2 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,12 @@ t.test('it works with /jsonify', async (t) => {
});

t.equal(res.statusCode, 200);
t.same(res.json(), { date: new Date(0).toJSON(), type: 'string', regexpType: 'string' });
t.same(res.json(), {
date: new Date(0).toJSON(),
dateString: 'Thu Jan 01 1970',
type: 'string',
regexpType: 'string',
});
});
t.test('it works with /jsonify 2', async (t) => {
const app = await buildApp({ t });
Expand All @@ -445,5 +450,5 @@ t.test('it works with /jsonify 2', async (t) => {
});
t.equal(res2.statusCode, 200);

t.same(res2.json(), { date, type: 'string', regexpType: 'string' });
t.same(res2.json(), { date, dateString: 'Thu Jan 01 1970', type: 'string', regexpType: 'string' });
});
7 changes: 6 additions & 1 deletion test/test_schema.gen.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@
"type": "object",
"required": [
"date",
"dateString",
"regexpType",
"type"
],
Expand All @@ -274,6 +275,10 @@
"type": "string",
"format": "date-time"
},
"dateString": {
"type": "string",
"format": "date-time"
},
"type": {
"type": "string"
},
Expand Down Expand Up @@ -462,5 +467,5 @@
}
}
},
"$hash": "38609094e259b0406fb6727e3c67af0ce5bad2bc2e945b8df8762d7f3bdf61c7__v1.2.0"
"$hash": "09d762cb9c53f4338c7374249078c983ab9ddf1a0814f98d7854a4e15f301e7b__v1.2.0"
}
7 changes: 6 additions & 1 deletion test/test_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ export interface TestSchema extends Schema {
};
response: {
200: {
content: { date: Date; type: string; regexpType: string };
content: {
date: Date;
dateString: Date;
type: string;
regexpType: string;
};
};
};
};
Expand Down

0 comments on commit b8716f6

Please sign in to comment.