fix(zod-openapi): make getRoutingPath non-enumerable (#445)

* zod-openapi: make getRoutingPath non-enumerable

This prevents potential serialisation errors (e.g. yaml's `stringify`) of the return value of getOpenAPIDocument

* .changeset: add an entry for #445
pull/446/head
Fumiaki Kinoshita 2024-04-11 05:28:53 +00:00 committed by GitHub
parent b865bb7ca4
commit 110e272460
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 6 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---
Make getRoutingPath property of a RouteConfig non-enumerable

View File

@ -431,12 +431,13 @@ type RoutingPath<P extends string> = P extends `${infer Head}/{${infer Param}}${
export const createRoute = <P extends string, R extends Omit<RouteConfig, 'path'> & { path: P }>(
routeConfig: R
) => {
return {
const route = {
...routeConfig,
getRoutingPath(): RoutingPath<R['path']> {
return routeConfig.path.replaceAll(/\/{(.+?)}/g, '/:$1') as RoutingPath<P>
},
}
return Object.defineProperty(route, 'getRoutingPath', { enumerable: false })
}
extendZodWithOpenApi(z)

View File

@ -50,11 +50,7 @@ describe('createRoute', () => {
},
} as const
const route = createRoute(config)
expect(route).toEqual({
...config,
getRoutingPath: expect.any(Function),
})
expect(route).toEqual(config)
expect(route.getRoutingPath()).toBe(expected)
expectTypeOf(route.getRoutingPath()).toEqualTypeOf<typeof expected>()
})