Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.
/ trpc-openapi Public archive
  • Sponsor
  • Notifications You must be signed in to change notification settings
  • Fork 167

Commit 6af660b

Browse files
authoredJan 26, 2023
Fix: coerce all input values regardless of query/body/path (#247)
1 parent fab401a commit 6af660b

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed
 

‎src/adapters/node-http/core.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ export const createOpenApiNodeHttpHandler = <
113113
};
114114
}
115115

116-
// if query & supported, coerce all string values to correct types
117-
if (!useBody && zodSupportsCoerce) {
116+
// if supported, coerce all string values to correct types
117+
if (zodSupportsCoerce) {
118118
if (instanceofZodTypeObject(unwrappedSchema)) {
119119
Object.values(unwrappedSchema.shape).forEach((shapeSchema) => {
120120
const unwrappedShapeSchema = unwrapZodType(shapeSchema, false);

‎test/adapters/standalone.test.ts

+51-8
Original file line numberDiff line numberDiff line change
@@ -1232,25 +1232,68 @@ describe('standalone adapter', () => {
12321232

12331233
test('with coerce', async () => {
12341234
const appRouter = t.router({
1235-
plusOne: t.procedure
1235+
getPlusOne: t.procedure
12361236
.meta({ openapi: { method: 'GET', path: '/plus-one' } })
12371237
.input(z.object({ number: z.number() }))
12381238
.output(z.object({ result: z.number() }))
12391239
.query(({ input }) => ({ result: input.number + 1 })),
1240+
postPlusOne: t.procedure
1241+
.meta({ openapi: { method: 'POST', path: '/plus-one' } })
1242+
.input(z.object({ date: z.date() }))
1243+
.output(z.object({ result: z.number() }))
1244+
.mutation(({ input }) => ({ result: input.date.getTime() + 1 })),
1245+
pathPlusOne: t.procedure
1246+
.meta({ openapi: { method: 'GET', path: '/plus-one/{number}' } })
1247+
.input(z.object({ number: z.number() }))
1248+
.output(z.object({ result: z.number() }))
1249+
.query(({ input }) => ({ result: input.number + 1 })),
12401250
});
12411251

12421252
const { url, close } = createHttpServerWithRouter({
12431253
router: appRouter,
12441254
});
12451255

1246-
const res = await fetch(`${url}/plus-one?number=9`, { method: 'GET' });
1247-
const body = await res.json();
1256+
{
1257+
const res = await fetch(`${url}/plus-one?number=9`, { method: 'GET' });
1258+
const body = await res.json();
12481259

1249-
expect(res.status).toBe(200);
1250-
expect(body).toEqual({ result: 10 });
1251-
expect(createContextMock).toHaveBeenCalledTimes(1);
1252-
expect(responseMetaMock).toHaveBeenCalledTimes(1);
1253-
expect(onErrorMock).toHaveBeenCalledTimes(0);
1260+
expect(res.status).toBe(200);
1261+
expect(body).toEqual({ result: 10 });
1262+
expect(createContextMock).toHaveBeenCalledTimes(1);
1263+
expect(responseMetaMock).toHaveBeenCalledTimes(1);
1264+
expect(onErrorMock).toHaveBeenCalledTimes(0);
1265+
1266+
clearMocks();
1267+
}
1268+
{
1269+
const date = new Date();
1270+
1271+
const res = await fetch(`${url}/plus-one`, {
1272+
method: 'POST',
1273+
headers: { 'Content-Type': 'application/json' },
1274+
body: JSON.stringify({ date }),
1275+
});
1276+
const body = await res.json();
1277+
1278+
expect(res.status).toBe(200);
1279+
expect(body).toEqual({ result: date.getTime() + 1 });
1280+
expect(createContextMock).toHaveBeenCalledTimes(1);
1281+
expect(responseMetaMock).toHaveBeenCalledTimes(1);
1282+
expect(onErrorMock).toHaveBeenCalledTimes(0);
1283+
1284+
clearMocks();
1285+
}
1286+
1287+
{
1288+
const res = await fetch(`${url}/plus-one/9`, { method: 'GET' });
1289+
const body = await res.json();
1290+
1291+
expect(res.status).toBe(200);
1292+
expect(body).toEqual({ result: 10 });
1293+
expect(createContextMock).toHaveBeenCalledTimes(1);
1294+
expect(responseMetaMock).toHaveBeenCalledTimes(1);
1295+
expect(onErrorMock).toHaveBeenCalledTimes(0);
1296+
}
12541297

12551298
close();
12561299
});

0 commit comments

Comments
 (0)
This repository has been archived.