11import type { OpenAPIV3_1 } from 'openapi-types'
22import type { ParameterDefinition } from './generate-interface'
3+ import { wrapInterfaceKeyGuard } from './wrap-interface-key-guard'
34
45/**
56 * Check if a schema is nullable (OpenAPI 3.0 or 3.1)
@@ -298,14 +299,15 @@ function formatType(obj: Record<string, unknown>, indent: number = 0): string {
298299 . map ( ( [ key , value ] ) => {
299300 // Handle string values (e.g., component references)
300301 if ( typeof value === 'string' ) {
301- return `${ nextIndentStr } ${ key } : ${ value } `
302+ return `${ nextIndentStr } ${ wrapInterfaceKeyGuard ( key ) } : ${ value } `
302303 }
303304
304305 // Handle ParameterDefinition for params and query
305306 if ( isParameterDefinition ( value ) ) {
306307 const typeStr = formatTypeValue ( value . type , nextIndent )
307308 const isOptional = value . required === false
308- const keyWithOptional = isOptional ? `${ key } ?` : key
309+ const wrappedKey = wrapInterfaceKeyGuard ( key )
310+ const keyWithOptional = isOptional ? `${ wrappedKey } ?` : wrappedKey
309311 let description = ''
310312 if ( value . description ) {
311313 description += `${ nextIndentStr } /**\n${ nextIndentStr } * ${ value . description } `
@@ -325,7 +327,7 @@ function formatType(obj: Record<string, unknown>, indent: number = 0): string {
325327 if ( isTypeObject ( value ) ) {
326328 const formattedValue = formatTypeValue ( value . type , nextIndent )
327329 // Key already has '?' if it's optional (from getTypeFromSchema), keep it as is
328- return `${ nextIndentStr } ${ key } : ${ formattedValue } `
330+ return `${ nextIndentStr } ${ wrapInterfaceKeyGuard ( key ) } : ${ formattedValue } `
329331 }
330332
331333 // Check if value is an object (like params, query) with all optional properties
@@ -337,7 +339,7 @@ function formatType(obj: Record<string, unknown>, indent: number = 0): string {
337339 const optionalMarker = valueAllOptional ? '?' : ''
338340
339341 const formattedValue = formatTypeValue ( value , nextIndent )
340- return `${ nextIndentStr } ${ key } ${ optionalMarker } : ${ formattedValue } `
342+ return `${ nextIndentStr } ${ wrapInterfaceKeyGuard ( key ) } ${ optionalMarker } : ${ formattedValue } `
341343 } )
342344 . join ( ';\n' )
343345
0 commit comments