Skip to content

Commit

Permalink
fix(zod-openapi): fix path param format :id to {id} (#132)
Browse files Browse the repository at this point in the history
* fix(zod-openapi): fix path param format `:id` to `{id}`

* changeset
  • Loading branch information
yusukebe authored Aug 23, 2023
1 parent c65b09e commit 2dbc823
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-birds-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---

fix path param format `:id` to `{id}`
15 changes: 10 additions & 5 deletions packages/zod-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ type Hook<T, E extends Env, P extends string, O> = (
c: Context<E, P>
) => TypedResponse<O> | Promise<TypedResponse<T>> | void

type ConvertPathType<T extends string> = T extends `${infer _}/{${infer Param}}${infer _}`
? `/:${Param}`
: T

export class OpenAPIHono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends Hono<
E,
S,
Expand All @@ -143,12 +147,13 @@ export class OpenAPIHono<E extends Env = Env, S = {}, BasePath extends string =

openapi = <
R extends RouteConfig,
I extends Input = InputTypeParam<R> & InputTypeQuery<R> & InputTypeForm<R> & InputTypeJson<R>
I extends Input = InputTypeParam<R> & InputTypeQuery<R> & InputTypeForm<R> & InputTypeJson<R>,
P extends string = ConvertPathType<R['path']>
>(
route: R,
handler: Handler<E, R['path'], I, OutputType<R>>,
hook?: Hook<I, E, R['path'], OutputType<R>>
): Hono<E, Schema<R['method'], R['path'], I['in'], OutputType<R>>, BasePath> => {
handler: Handler<E, P, I, OutputType<R>>,
hook?: Hook<I, E, P, OutputType<R>>
): Hono<E, Schema<R['method'], P, I['in'], OutputType<R>>, BasePath> => {
this.#registry.registerPath(route)

const validators: MiddlewareHandler[] = []
Expand Down Expand Up @@ -187,7 +192,7 @@ export class OpenAPIHono<E extends Env = Env, S = {}, BasePath extends string =
}
}

this.on([route.method], route.path, ...validators, handler)
this.on([route.method], route.path.replace(/\/{(.+)}/, '/:$1'), ...validators, handler)
return this
}

Expand Down
14 changes: 7 additions & 7 deletions packages/zod-openapi/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ describe('Basic - params', () => {

const UserSchema = z
.object({
id: z.number().openapi({
example: 123,
id: z.string().openapi({
example: '123',
}),
name: z.string().openapi({
example: 'John Doe',
Expand All @@ -44,7 +44,7 @@ describe('Basic - params', () => {

const route = createRoute({
method: 'get',
path: '/users/:id',
path: '/users/{id}',
request: {
params: ParamsSchema,
},
Expand Down Expand Up @@ -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',
})
Expand Down Expand Up @@ -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',
})
Expand All @@ -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 },
},
Expand All @@ -143,7 +143,7 @@ describe('Basic - params', () => {
parameters: {},
},
paths: {
'/users/:id': {
'/users/{id}': {
get: {
parameters: [
{
Expand Down

0 comments on commit 2dbc823

Please sign in to comment.