Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/gen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions test/gen/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')()

Expand Down