File tree Expand file tree Collapse file tree 2 files changed +58
-2
lines changed Expand file tree Collapse file tree 2 files changed +58
-2
lines changed Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import { TypeBox } from '@sinclair/typemap'
22import type { AdditionalReference } from '../types'
33
44const matchRoute = / : E l y s i a < ( .* ) > / gs
5- const numberKey = / ( \d + ) : / g
5+ const propertyKey = / ( [ A - Z a - z _ ] \w * | \d + ) : / g
66
77export 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 ( / r e a d o n l y / g, '' ) )
126127 if ( schema . type !== 'object' ) continue
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments