From 110e27246015cac60c8a07cd078c3245b7ddbeeb Mon Sep 17 00:00:00 2001 From: Fumiaki Kinoshita Date: Thu, 11 Apr 2024 05:28:53 +0000 Subject: [PATCH] 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 --- .changeset/long-crabs-drum.md | 5 +++++ packages/zod-openapi/src/index.ts | 3 ++- packages/zod-openapi/test/createRoute.test.ts | 6 +----- 3 files changed, 8 insertions(+), 6 deletions(-) create mode 100644 .changeset/long-crabs-drum.md diff --git a/.changeset/long-crabs-drum.md b/.changeset/long-crabs-drum.md new file mode 100644 index 000000000..95ba86227 --- /dev/null +++ b/.changeset/long-crabs-drum.md @@ -0,0 +1,5 @@ +--- +'@hono/zod-openapi': patch +--- + +Make getRoutingPath property of a RouteConfig non-enumerable diff --git a/packages/zod-openapi/src/index.ts b/packages/zod-openapi/src/index.ts index 887fafcf7..bb62a6f54 100644 --- a/packages/zod-openapi/src/index.ts +++ b/packages/zod-openapi/src/index.ts @@ -431,12 +431,13 @@ type RoutingPath

= P extends `${infer Head}/{${infer Param}}${ export const createRoute =

& { path: P }>( routeConfig: R ) => { - return { + const route = { ...routeConfig, getRoutingPath(): RoutingPath { return routeConfig.path.replaceAll(/\/{(.+?)}/g, '/:$1') as RoutingPath

}, } + return Object.defineProperty(route, 'getRoutingPath', { enumerable: false }) } extendZodWithOpenApi(z) diff --git a/packages/zod-openapi/test/createRoute.test.ts b/packages/zod-openapi/test/createRoute.test.ts index 52e596358..a0445232f 100644 --- a/packages/zod-openapi/test/createRoute.test.ts +++ b/packages/zod-openapi/test/createRoute.test.ts @@ -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() })