Skip to content

Commit

Permalink
fix(zod-openapi): make getRoutingPath non-enumerable (#445)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
fumieval authored Apr 11, 2024
1 parent b865bb7 commit 110e272
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-crabs-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---

Make getRoutingPath property of a RouteConfig non-enumerable
3 changes: 2 additions & 1 deletion packages/zod-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions packages/zod-openapi/test/createRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>()
})
Expand Down

0 comments on commit 110e272

Please sign in to comment.