From b229f831db046b979279c1e549b5d8bc23b33329 Mon Sep 17 00:00:00 2001 From: Prithpal Sooriya Date: Sat, 18 Oct 2025 23:30:56 +0530 Subject: [PATCH] fix: add type-gen support for versioning paths --- src/gen/index.ts | 5 ++-- test/gen/index.test.ts | 55 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/gen/index.ts b/src/gen/index.ts index 6847f63..d26f323 100644 --- a/src/gen/index.ts +++ b/src/gen/index.ts @@ -2,7 +2,7 @@ import { TypeBox } from '@sinclair/typemap' import type { AdditionalReference } from '../types' const matchRoute = /: Elysia<(.*)>/gs -const numberKey = /(\d+):/g +const propertyKey = /([A-Za-z_]\w*|\d+):/g export interface OpenAPIGeneratorOptions { /** @@ -120,7 +120,8 @@ export function declarationToJSONSchema(declaration: string) { // Treaty is a collection of { ... } & { ... } & { ... } for (const route of extractRootObjects( - declaration.replace(numberKey, '"$1":') + // Ensure all property keys are wrapped in quotations + declaration.replace(propertyKey, '"$1":') )) { let schema = TypeBox(route.replaceAll(/readonly/g, '')) if (schema.type !== 'object') continue diff --git a/test/gen/index.test.ts b/test/gen/index.test.ts index 850b654..b7da41d 100644 --- a/test/gen/index.test.ts +++ b/test/gen/index.test.ts @@ -425,6 +425,61 @@ describe('Gen > Type Gen', () => { }) }) + it('handle alphanumeric route keys like v1', () => { + const reference = declarationToJSONSchema(` + { + v1: { + foo: { + get: { + params: {} + query: {} + headers: {} + body: {} + response: { + 200: { + value: number + } + } + } + } + } + }`) + + expect(serializable(reference)!).toEqual({ + '/v1/foo': { + get: { + body: { + properties: {}, + type: 'object' + }, + headers: { + properties: {}, + type: 'object' + }, + params: { + properties: {}, + type: 'object' + }, + query: { + properties: {}, + type: 'object' + }, + response: { + '200': { + properties: { + value: { + type: 'number' + } + }, + required: ['value'], + type: 'object' + } + } + } + } + }) + }) + it('integrate', async () => { const reference = fromTypes('test/gen/sample.ts')()