Registry Mirror
Skip to content

File tree

‎packages/nest/src/implement.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,47 @@ describe('compatibility', () => {
14521452
expect(req!.url).toBe('/injection')
14531453
})
14541454

1455+
it('router-based implementation skips null and undefined contract entries', async () => {
1456+
const ping = oc.meta(openapi({ path: '/ping' }))
1457+
const nestedPing = oc.meta(openapi({ path: '/nested/ping' }))
1458+
const contract: any = {
1459+
ping,
1460+
missing: undefined,
1461+
empty: null,
1462+
nested: { ping: nestedPing, missing: undefined, empty: null },
1463+
}
1464+
1465+
@Controller()
1466+
class ImplController {
1467+
@Implement(contract)
1468+
sparse() {
1469+
return {
1470+
ping: implement(ping).handler(() => 'pong'),
1471+
nested: { ping: implement(nestedPing).handler(() => 'nested pong') },
1472+
}
1473+
}
1474+
}
1475+
1476+
expect(Object.getOwnPropertyNames(ImplController.prototype)).not.toEqual(
1477+
expect.arrayContaining([expect.stringMatching(/_(missing|empty)/)]),
1478+
)
1479+
1480+
const moduleRef = await Test.createTestingModule({
1481+
controllers: [ImplController],
1482+
}).compile()
1483+
1484+
const app = moduleRef.createNestApplication()
1485+
await app.init()
1486+
1487+
const res = await supertest(app.getHttpServer()).post('/ping')
1488+
expect(res.status).toBe(200)
1489+
expect(res.text).toContain('pong')
1490+
1491+
const nestedRes = await supertest(app.getHttpServer()).post('/nested/ping')
1492+
expect(nestedRes.status).toBe(200)
1493+
expect(nestedRes.text).toContain('nested pong')
1494+
})
1495+
14551496
it('router-based implementation controller can handle conflict method names and reflect all metadata on new methods regardless of decorator order', async () => {
14561497
const contract = {
14571498
ping: oc.meta(openapi({ path: '/ping' })),

‎packages/nest/src/implement.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { StandardHandler } from '@orpc/server/standard'
3636
import type { AnyFunction, Promisable } from '@orpc/shared'
3737
import {
3838
isAsyncIteratorObject,
39+
isNonPrimitive,
3940
listenAbort,
4041
mergeHttpPath,
4142
NullProtoObj,
@@ -162,6 +163,10 @@ function implementRouterContract(
162163
const originalMethod = descriptor.value!
163164

164165
for (const [key, childContract] of Object.entries(contract)) {
166+
if (!isNonPrimitive(childContract)) {
167+
continue
168+
}
169+
165170
let methodName = `${propertyKey}_${key}`
166171

167172
let i = 0
@@ -206,7 +211,7 @@ function implementRouterContract(
206211
routeDecorator(target, methodName, childDescriptor)
207212
})
208213
} else {
209-
implementRouterContract(childContract, target, methodName, childDescriptor)
214+
implementRouterContract(childContract as RouterContract, target, methodName, childDescriptor)
210215
}
211216
}
212217
}

‎packages/server/src/implementer-router.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,29 @@ describe('createRouterImplementer', () => {
319319
})
320320
})
321321
})
322+
323+
it.each([undefined, null])('skips %s contract entries', (entry) => {
324+
const sparse = {
325+
ping: oc.output(z.string()),
326+
missing: entry,
327+
use: entry,
328+
nested: { ping: oc.output(z.string()), missing: entry },
329+
} as any
330+
331+
const implementer = createRouterImplementer(sparse, config) as any
332+
333+
expect(implementer.missing).toBeUndefined()
334+
expect(implementer.nested.missing).toBeUndefined()
335+
expect(implementer.use).toBeTypeOf('function')
336+
337+
const router = implementer.router({
338+
ping: implementer.ping.handler(() => 'pong'),
339+
nested: { ping: implementer.nested.ping.handler(() => 'pong') },
340+
})
341+
const entries = vi.fn()
342+
343+
RouterUtilsModule.walkProcedureContractsSync(router, entries)
344+
345+
expect(entries.mock.calls.map(([, path]) => path)).toEqual([['ping'], ['nested', 'ping']])
346+
})
322347
})

‎packages/server/src/implementer-router.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ErrorMap, InferRouterContractErrorMap, RouterContract } from '@orpc/contract'
22
import { ProcedureContract } from '@orpc/contract'
33
import type { IntersectPick, Public } from '@orpc/shared'
4-
import { bindMethods, omit, setOwn } from '@orpc/shared'
4+
import { bindMethods, isNonPrimitive, omit, setOwn } from '@orpc/shared'
55

66
import type { Context, MergedContext, MergedInitialContext } from './context'
77
import { ProcedureImplementer } from './implementer-procedure'
@@ -246,6 +246,10 @@ function toPlainRouterContract<T extends RouterContract>(contract: T): T {
246246
) as T
247247
}
248248

249+
if (!isNonPrimitive(contract)) {
250+
return contract
251+
}
252+
249253
let copied: Record<string, RouterContract> | undefined
250254

251255
for (const [key, child] of Object.entries(contract as Record<string, RouterContract>)) {
@@ -282,6 +286,10 @@ function createRouterImplementerInternal<
282286
const implementer: Record<string, any> = {}
283287

284288
for (const [key, child] of Object.entries(contract)) {
289+
if (!isNonPrimitive(child)) {
290+
continue
291+
}
292+
285293
setOwn(
286294
implementer,
287295
key,

‎packages/server/src/router-hidden.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ it('withHiddenRouterContract & getHiddenRouterContract', () => {
2121
contract.ping,
2222
)
2323
})
24+
25+
it.each([undefined, null])('getHiddenRouterContract returns undefined for %s', (router) => {
26+
expect(getHiddenRouterContract(router)).toBeUndefined()
27+
})

‎packages/server/src/router-hidden.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { RouterContract } from '@orpc/contract'
2+
import { isNonPrimitive } from '@orpc/shared'
23

34
import type { Lazyable } from './lazy'
45
import type { AnyRouter } from './router'
@@ -20,8 +21,10 @@ export function withHiddenRouterContract<T extends Lazyable<AnyRouter>>(
2021
})
2122
}
2223

23-
export function getHiddenRouterContract(
24-
router: Lazyable<AnyRouter | RouterContract>,
25-
): RouterContract | undefined {
26-
return (router as any)[HIDDEN_ROUTER_CONTRACT_SYMBOL]
24+
export function getHiddenRouterContract(router: unknown): RouterContract | undefined {
25+
if (!isNonPrimitive(router)) {
26+
return undefined
27+
}
28+
29+
return router[HIDDEN_ROUTER_CONTRACT_SYMBOL] as RouterContract | undefined
2730
}

‎packages/server/src/router-utils.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,22 @@ const router = {
7474
p1: procedure1,
7575
p2: procedure2,
7676
invalid: 'invalid' as any,
77+
undefined: undefined as any,
78+
null: null as any,
7779
nested: callable({
7880
p1: callable(procedure1),
7981
invalid: 'invalid' as any,
82+
undefined: undefined as any,
83+
null: null as any,
8084
}),
8185
lazy: new Lazy({
8286
loader: async () => ({
8387
default: {
8488
p1: procedure1,
8589
nested: callable({ p1: procedure1 }),
8690
invalid: 'invalid' as any,
91+
undefined: undefined as any,
92+
null: null as any,
8793
},
8894
}),
8995
meta: { lazy: true },

0 commit comments

Comments
 (0)