11import { printIdentifier } from "@typespec/compiler" ;
22import {
33 OpenAPI3Encoding ,
4- OpenAPI3Schema ,
5- OpenAPI3SchemaProperty ,
4+ OpenAPISchema3_2 ,
65 Refable ,
6+ SupportedOpenAPISchema ,
77} from "../../../../types.js" ;
88import { Context } from "../utils/context.js" ;
99import {
@@ -17,7 +17,7 @@ export class SchemaToExpressionGenerator {
1717 constructor ( public rootNamespace : string ) { }
1818
1919 public generateTypeFromRefableSchema (
20- schema : Refable < OpenAPI3Schema > ,
20+ schema : Refable < SupportedOpenAPISchema > ,
2121 callingScope : string [ ] ,
2222 isHttpPart = false ,
2323 encoding ?: Record < string , OpenAPI3Encoding > ,
@@ -29,7 +29,7 @@ export class SchemaToExpressionGenerator {
2929 : this . getTypeFromSchema ( schema , callingScope , isHttpPart , encoding , context ) ;
3030 }
3131
32- public generateArrayType ( schema : OpenAPI3Schema , callingScope : string [ ] ) : string {
32+ public generateArrayType ( schema : SupportedOpenAPISchema , callingScope : string [ ] ) : string {
3333 const items = schema . items ;
3434 if ( ! items ) {
3535 return "unknown[]" ;
@@ -85,7 +85,7 @@ export class SchemaToExpressionGenerator {
8585 }
8686
8787 private getTypeFromSchema (
88- schema : OpenAPI3Schema ,
88+ schema : SupportedOpenAPISchema ,
8989 callingScope : string [ ] ,
9090 isHttpPart = false ,
9191 encoding ?: Record < string , OpenAPI3Encoding > ,
@@ -103,7 +103,7 @@ export class SchemaToExpressionGenerator {
103103 } else {
104104 // Create a schema with a single type to reuse existing type extraction logic
105105 // Remove type array, nullable, and default to avoid double-processing
106- const singleTypeSchema : OpenAPI3Schema = {
106+ const singleTypeSchema : SupportedOpenAPISchema = {
107107 ...schema ,
108108 type : t as any ,
109109 nullable : undefined ,
@@ -154,7 +154,7 @@ export class SchemaToExpressionGenerator {
154154 type = this . generateArrayType ( schema , callingScope ) ;
155155 }
156156
157- if ( schema . nullable ) {
157+ if ( "nullable" in schema && schema . nullable ) {
158158 type += ` | null` ;
159159 }
160160
@@ -170,7 +170,7 @@ export class SchemaToExpressionGenerator {
170170 }
171171
172172 private generateDefaultValue (
173- schema : OpenAPI3Schema ,
173+ schema : SupportedOpenAPISchema ,
174174 callingScope : string [ ] ,
175175 context ?: Context ,
176176 ) : string | undefined {
@@ -224,9 +224,9 @@ export class SchemaToExpressionGenerator {
224224 return undefined ; // Return undefined to indicate no default found
225225 }
226226
227- private getAllOfType ( schema : OpenAPI3Schema , callingScope : string [ ] ) : string {
227+ private getAllOfType ( schema : SupportedOpenAPISchema , callingScope : string [ ] ) : string {
228228 const requiredProps : string [ ] = schema . required || [ ] ;
229- let properties : Record < string , Refable < OpenAPI3Schema > > = { } ;
229+ let properties : Record < string , Refable < SupportedOpenAPISchema > > = { } ;
230230 const baseTypes : string [ ] = [ ] ;
231231
232232 for ( const member of schema . allOf || [ ] ) {
@@ -261,7 +261,9 @@ export class SchemaToExpressionGenerator {
261261 }
262262 }
263263
264- private stripDefaultsFromSchema ( schema : Refable < OpenAPI3Schema > ) : Refable < OpenAPI3Schema > {
264+ private stripDefaultsFromSchema (
265+ schema : Refable < SupportedOpenAPISchema > ,
266+ ) : Refable < SupportedOpenAPISchema > {
265267 if ( "$ref" in schema ) {
266268 return schema ;
267269 }
@@ -274,24 +276,31 @@ export class SchemaToExpressionGenerator {
274276 strippedSchema . items = this . stripDefaultsFromSchema ( strippedSchema . items ) ;
275277 }
276278
279+ // in the next blocks the casts are ugly but safe because the stripped schema is going to be the same as the original one
277280 if ( strippedSchema . anyOf ) {
278- strippedSchema . anyOf = strippedSchema . anyOf . map ( ( item ) => this . stripDefaultsFromSchema ( item ) ) ;
281+ strippedSchema . anyOf = strippedSchema . anyOf . map ( ( item ) =>
282+ this . stripDefaultsFromSchema ( item ) ,
283+ ) as unknown as OpenAPISchema3_2 [ ] ;
279284 }
280285
281286 if ( strippedSchema . oneOf ) {
282- strippedSchema . oneOf = strippedSchema . oneOf . map ( ( item ) => this . stripDefaultsFromSchema ( item ) ) ;
287+ strippedSchema . oneOf = strippedSchema . oneOf . map ( ( item ) =>
288+ this . stripDefaultsFromSchema ( item ) ,
289+ ) as unknown as OpenAPISchema3_2 [ ] ;
283290 }
284291
285292 if ( strippedSchema . allOf ) {
286- strippedSchema . allOf = strippedSchema . allOf . map ( ( item ) => this . stripDefaultsFromSchema ( item ) ) ;
293+ strippedSchema . allOf = strippedSchema . allOf . map ( ( item ) =>
294+ this . stripDefaultsFromSchema ( item ) ,
295+ ) as unknown as OpenAPISchema3_2 [ ] ;
287296 }
288297
289298 if ( strippedSchema . properties ) {
290- const strippedProperties : Record < string , Refable < OpenAPI3Schema > > = { } ;
299+ const strippedProperties : Record < string , Refable < SupportedOpenAPISchema > > = { } ;
291300 for ( const [ key , prop ] of Object . entries ( strippedSchema . properties ) ) {
292301 strippedProperties [ key ] = this . stripDefaultsFromSchema ( prop ) ;
293302 }
294- strippedSchema . properties = strippedProperties ;
303+ strippedSchema . properties = strippedProperties as unknown as Record < string , OpenAPISchema3_2 > ;
295304 }
296305
297306 if (
@@ -306,7 +315,7 @@ export class SchemaToExpressionGenerator {
306315 return strippedSchema ;
307316 }
308317
309- private getAnyOfType ( schema : OpenAPI3Schema , callingScope : string [ ] ) : string {
318+ private getAnyOfType ( schema : SupportedOpenAPISchema , callingScope : string [ ] ) : string {
310319 const definitions : string [ ] = [ ] ;
311320
312321 for ( const item of schema . anyOf ?? [ ] ) {
@@ -318,7 +327,7 @@ export class SchemaToExpressionGenerator {
318327 return definitions . join ( " | " ) ;
319328 }
320329
321- private getOneOfType ( schema : OpenAPI3Schema , callingScope : string [ ] ) : string {
330+ private getOneOfType ( schema : SupportedOpenAPISchema , callingScope : string [ ] ) : string {
322331 const definitions : string [ ] = [ ] ;
323332
324333 for ( const item of schema . oneOf ?? [ ] ) {
@@ -408,7 +417,7 @@ export class SchemaToExpressionGenerator {
408417 public static readonly decoratorNamesToExcludeForParts : string [ ] = [ "minValue" , "maxValue" ] ;
409418
410419 private getObjectType (
411- schema : OpenAPI3Schema ,
420+ schema : SupportedOpenAPISchema ,
412421 callingScope : string [ ] ,
413422 isHttpPart = false ,
414423 encoding ?: Record < string , OpenAPI3Encoding > ,
@@ -466,7 +475,7 @@ export class SchemaToExpressionGenerator {
466475}
467476
468477export function isReferencedEnumType (
469- propSchema : OpenAPI3SchemaProperty ,
478+ propSchema : Refable < SupportedOpenAPISchema > ,
470479 context : Context ,
471480) : boolean {
472481 let isEnumType = false ;
@@ -486,7 +495,7 @@ export function isReferencedEnumType(
486495}
487496
488497export function isReferencedUnionType (
489- propSchema : OpenAPI3SchemaProperty ,
498+ propSchema : Refable < SupportedOpenAPISchema > ,
490499 context : Context ,
491500) : boolean {
492501 let isUnionType = false ;
@@ -509,7 +518,7 @@ export function isReferencedUnionType(
509518 return isUnionType ;
510519}
511520
512- export function getTypeSpecPrimitiveFromSchema ( schema : OpenAPI3Schema ) : string | undefined {
521+ export function getTypeSpecPrimitiveFromSchema ( schema : SupportedOpenAPISchema ) : string | undefined {
513522 if ( schema . type === "boolean" ) {
514523 return "boolean" ;
515524 } else if ( ( schema as any ) . type === "null" ) {
@@ -524,7 +533,7 @@ export function getTypeSpecPrimitiveFromSchema(schema: OpenAPI3Schema): string |
524533 return ;
525534}
526535
527- function getIntegerType ( schema : OpenAPI3Schema ) : string {
536+ function getIntegerType ( schema : SupportedOpenAPISchema ) : string {
528537 const format = schema . format ?? "" ;
529538 switch ( format ) {
530539 case "int8" :
@@ -543,7 +552,7 @@ function getIntegerType(schema: OpenAPI3Schema): string {
543552 }
544553}
545554
546- function getNumberType ( schema : OpenAPI3Schema ) : string {
555+ function getNumberType ( schema : SupportedOpenAPISchema ) : string {
547556 const format = schema . format ?? "" ;
548557 switch ( format ) {
549558 case "decimal" :
@@ -559,7 +568,7 @@ function getNumberType(schema: OpenAPI3Schema): string {
559568 }
560569}
561570
562- function getStringType ( schema : OpenAPI3Schema ) : string {
571+ function getStringType ( schema : SupportedOpenAPISchema ) : string {
563572 const format = schema . format ?? "" ;
564573 let type = "string" ;
565574 switch ( format ) {
@@ -588,6 +597,6 @@ function getStringType(schema: OpenAPI3Schema): string {
588597 return type ;
589598}
590599
591- function getEnum ( schemaEnum : ( string | number | boolean ) [ ] ) : string {
600+ function getEnum ( schemaEnum : ( string | number | boolean | null ) [ ] ) : string {
592601 return schemaEnum . map ( ( e ) => JSON . stringify ( e ) ) . join ( " | " ) ;
593602}
0 commit comments