diff --git a/.changeset/rich-birds-raise.md b/.changeset/rich-birds-raise.md new file mode 100644 index 00000000..eb832c19 --- /dev/null +++ b/.changeset/rich-birds-raise.md @@ -0,0 +1,5 @@ +--- +'@hono/zod-openapi': patch +--- + +fix path param format `:id` to `{id}` diff --git a/packages/zod-openapi/src/index.ts b/packages/zod-openapi/src/index.ts index 8b2ea78f..3570772a 100644 --- a/packages/zod-openapi/src/index.ts +++ b/packages/zod-openapi/src/index.ts @@ -129,6 +129,10 @@ type Hook = ( c: Context ) => TypedResponse | Promise> | void +type ConvertPathType = T extends `${infer _}/{${infer Param}}${infer _}` + ? `/:${Param}` + : T + export class OpenAPIHono extends Hono< E, S, @@ -143,12 +147,13 @@ export class OpenAPIHono & InputTypeQuery & InputTypeForm & InputTypeJson + I extends Input = InputTypeParam & InputTypeQuery & InputTypeForm & InputTypeJson, + P extends string = ConvertPathType >( route: R, - handler: Handler>, - hook?: Hook> - ): Hono>, BasePath> => { + handler: Handler>, + hook?: Hook> + ): Hono>, BasePath> => { this.#registry.registerPath(route) const validators: MiddlewareHandler[] = [] @@ -187,7 +192,7 @@ export class OpenAPIHono { const UserSchema = z .object({ - id: z.number().openapi({ - example: 123, + id: z.string().openapi({ + example: '123', }), name: z.string().openapi({ example: 'John Doe', @@ -44,7 +44,7 @@ describe('Basic - params', () => { const route = createRoute({ method: 'get', - path: '/users/:id', + path: '/users/{id}', request: { params: ParamsSchema, }, @@ -75,7 +75,7 @@ describe('Basic - params', () => { (c) => { const { id } = c.req.valid('param') return c.jsonT({ - id: Number(id), + id, age: 20, name: 'Ultra-man', }) @@ -105,7 +105,7 @@ describe('Basic - params', () => { const res = await app.request('/users/123') expect(res.status).toBe(200) expect(await res.json()).toEqual({ - id: 123, + id: '123', age: 20, name: 'Ultra-man', }) @@ -128,7 +128,7 @@ describe('Basic - params', () => { User: { type: 'object', properties: { - id: { type: 'number', example: 123 }, + id: { type: 'string', example: '123' }, name: { type: 'string', example: 'John Doe' }, age: { type: 'number', example: 42 }, }, @@ -143,7 +143,7 @@ describe('Basic - params', () => { parameters: {}, }, paths: { - '/users/:id': { + '/users/{id}': { get: { parameters: [ {