Skip to content

Commit a12f799

Browse files
fix: add type-gen support for versioning paths
1 parent 070919e commit a12f799

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/gen/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { TypeBox } from '@sinclair/typemap'
22
import type { AdditionalReference } from '../types'
33

44
const matchRoute = /: Elysia<(.*)>/gs
5-
const numberKey = /(\d+):/g
5+
const propertyKey = /([A-Za-z_]\w*|\d+):/g
66

77
export interface OpenAPIGeneratorOptions {
88
/**
@@ -120,7 +120,8 @@ export function declarationToJSONSchema(declaration: string) {
120120

121121
// Treaty is a collection of { ... } & { ... } & { ... }
122122
for (const route of extractRootObjects(
123-
declaration.replace(numberKey, '"$1":')
123+
// Ensure all property keys are perenthesised
124+
declaration.replace(propertyKey, '"$1":')
124125
)) {
125126
let schema = TypeBox(route.replaceAll(/readonly/g, ''))
126127
if (schema.type !== 'object') continue

test/gen/index.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,61 @@ describe('Gen > Type Gen', () => {
425425
})
426426
})
427427

428+
it('handle alphanumeric route keys like v1', () => {
429+
const reference = declarationToJSONSchema(`
430+
{
431+
v1: {
432+
foo: {
433+
get: {
434+
params: {}
435+
query: {}
436+
headers: {}
437+
body: {}
438+
response: {
439+
200: {
440+
value: number
441+
}
442+
}
443+
}
444+
}
445+
}
446+
}`)
447+
448+
expect(serializable(reference)!).toEqual({
449+
'/v1/foo': {
450+
get: {
451+
body: {
452+
properties: {},
453+
type: 'object'
454+
},
455+
headers: {
456+
properties: {},
457+
type: 'object'
458+
},
459+
params: {
460+
properties: {},
461+
type: 'object'
462+
},
463+
query: {
464+
properties: {},
465+
type: 'object'
466+
},
467+
response: {
468+
'200': {
469+
properties: {
470+
value: {
471+
type: 'number'
472+
}
473+
},
474+
required: ['value'],
475+
type: 'object'
476+
}
477+
}
478+
}
479+
}
480+
})
481+
})
482+
428483
it('integrate', async () => {
429484
const reference = fromTypes('test/gen/sample.ts')()
430485

0 commit comments

Comments
 (0)