-
-
Notifications
You must be signed in to change notification settings - Fork 169
feat(openapi): accept optional path params in the input schema #1983
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
Merged
dinwwwh
merged 4 commits into
middleapi:main
from
dinwwwh:claude/dynamic-params-schema-required-ab1808
Sep 5, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b0908b2
feat(openapi): accept optional path params in the input schema
dinwwwh 80880d3
test(openapi): cover one procedure reused under multiple routes with …
dinwwwh f759084
docs(openapi): note that path params are always documented as required
dinwwwh 7a3f58b
docs(openapi): drop the required path param note
dinwwwh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import type { RouterClient } from '@orpc/server' | ||
| import { createORPCClient } from '@orpc/client' | ||
| import { openapi, OpenAPIGenerator } from '@orpc/openapi' | ||
| import { OpenAPIHandler, OpenAPILink } from '@orpc/openapi/fetch' | ||
| import { os } from '@orpc/server' | ||
| import { z } from 'zod' | ||
|
|
||
| /** | ||
| * One procedure exposed under several routes, where each route promotes a different optional | ||
| * field to a path param. Covers https://github.com/middleapi/orpc/issues/513. | ||
| */ | ||
| describe('one procedure reused under multiple routes with optional path params', () => { | ||
| const listComments = os | ||
| .meta(openapi({ method: 'GET', path: '/comments', tags: ['admin'] })) | ||
| .input(z.object({ | ||
| user: z.union([z.literal('me'), z.coerce.number()]).optional(), | ||
| tasting: z.coerce.number().optional(), | ||
| cursor: z.coerce.number().gte(1).default(1), | ||
| limit: z.coerce.number().gte(1).lte(100).default(100), | ||
| })) | ||
| .handler(async ({ input }) => input) | ||
|
|
||
| const router = { | ||
| admin: { listComments }, | ||
| tastings: { | ||
| listComments: listComments.meta(openapi({ path: '/tastings/{tasting}/comments', tags: ['tastings'] })), | ||
| }, | ||
| users: { | ||
| listComments: listComments.meta(openapi({ path: '/users/{user}/comments', tags: ['users'] })), | ||
| }, | ||
| } | ||
|
|
||
| describe('handler', () => { | ||
| const handler = new OpenAPIHandler(router) | ||
|
|
||
| it.each([ | ||
| ['/comments?limit=5', { cursor: 1, limit: 5 }], | ||
| ['/comments?tasting=42&user=me', { tasting: 42, user: 'me', cursor: 1, limit: 100 }], | ||
| ['/tastings/42/comments', { tasting: 42, cursor: 1, limit: 100 }], | ||
| ['/users/me/comments?cursor=2', { user: 'me', cursor: 2, limit: 100 }], | ||
| ['/users/7/comments', { user: 7, cursor: 1, limit: 100 }], | ||
| ])('routes GET %s to the shared handler with %o', async (path, expected) => { | ||
| const { matched, response } = await handler.handle(new Request(`https://example.com${path}`)) | ||
|
|
||
| expect(matched).toBe(true) | ||
| expect(response!.status).toBe(200) | ||
| await expect(response!.json()).resolves.toEqual(expected) | ||
| }) | ||
| }) | ||
|
|
||
| describe('link', () => { | ||
| const handler = new OpenAPIHandler(router) | ||
| const requests: string[] = [] | ||
|
|
||
| const link = new OpenAPILink(router, { | ||
| url: '/', | ||
| origin: 'https://example.com', | ||
| async fetch(url, init) { | ||
| const request = new Request(url, init) | ||
| const { pathname, search } = new URL(request.url) | ||
| requests.push(`${request.method} ${pathname}${search}`) | ||
| const { response } = await handler.handle(request) | ||
| return response ?? new Response('Not Found', { status: 404 }) | ||
| }, | ||
| }) | ||
|
|
||
| const client = createORPCClient<RouterClient<typeof router>>(link) | ||
|
|
||
| beforeEach(() => { | ||
| requests.length = 0 | ||
| }) | ||
|
|
||
| it('sends every field as a query param on the route without path params', async () => { | ||
| await expect(client.admin.listComments({ tasting: 42, limit: 5 })).resolves.toEqual({ tasting: 42, cursor: 1, limit: 5 }) | ||
| expect(requests).toEqual(['GET /comments?tasting=42&limit=5']) | ||
| }) | ||
|
|
||
| it('promotes the field to the path segment on the routes that declare it', async () => { | ||
| await expect(client.tastings.listComments({ tasting: 42 })).resolves.toEqual({ tasting: 42, cursor: 1, limit: 100 }) | ||
| await expect(client.users.listComments({ user: 'me', cursor: 2 })).resolves.toEqual({ user: 'me', cursor: 2, limit: 100 }) | ||
| expect(requests).toEqual(['GET /tastings/42/comments', 'GET /users/me/comments?cursor=2']) | ||
| }) | ||
|
|
||
| it('rejects at runtime when the optional field backing a path param is omitted', async () => { | ||
| await expect(client.tastings.listComments({})).rejects.toThrow('Path param "tasting" cannot be empty') | ||
| expect(requests).toEqual([]) | ||
| }) | ||
| }) | ||
|
|
||
| describe('generator', () => { | ||
| it('documents each route with its own path params and query params', async () => { | ||
| const doc = await new OpenAPIGenerator().generate(router) | ||
|
|
||
| expect(Object.keys(doc.paths ?? {})).toEqual(['/comments', '/tastings/{tasting}/comments', '/users/{user}/comments']) | ||
|
|
||
| const paramNames = (path: `/${string}`) => doc.paths![path]!.get!.parameters!.map((p: any) => `${p.in}:${p.name}${p.required ? '!' : ''}`) | ||
|
|
||
| expect(paramNames('/comments')).toEqual(['query:user', 'query:tasting', 'query:cursor', 'query:limit']) | ||
| expect(paramNames('/tastings/{tasting}/comments')).toEqual(['path:tasting!', 'query:user', 'query:cursor', 'query:limit']) | ||
| expect(paramNames('/users/{user}/comments')).toEqual(['path:user!', 'query:tasting', 'query:cursor', 'query:limit']) | ||
|
|
||
| expect(doc.paths!['/comments']!.get!.tags).toEqual(['admin']) | ||
| expect(doc.paths!['/tastings/{tasting}/comments']!.get!.tags).toEqual(['admin', 'tastings']) | ||
| expect(doc.paths!['/users/{user}/comments']!.get!.tags).toEqual(['admin', 'users']) | ||
| expect(doc.paths!['/tastings/{tasting}/comments']!.get!.operationId).toBe('tastings.listComments') | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.