@@ -284,12 +284,7 @@ export class TsSchemaGenerator {
284284 }
285285
286286 private createDataModelFieldObject ( field : DataModelField ) {
287- const objectFields = [
288- ts . factory . createPropertyAssignment (
289- 'type' ,
290- ts . factory . createStringLiteral ( field . type . type ?? field . type . reference ! . $refText ) ,
291- ) ,
292- ] ;
287+ const objectFields = [ ts . factory . createPropertyAssignment ( 'type' , this . generateFieldTypeLiteral ( field ) ) ] ;
293288
294289 if ( isIdField ( field ) ) {
295290 objectFields . push ( ts . factory . createPropertyAssignment ( 'id' , ts . factory . createTrue ( ) ) ) ;
@@ -325,7 +320,7 @@ export class TsSchemaGenerator {
325320
326321 const defaultValue = this . getMappedDefault ( field ) ;
327322 if ( defaultValue !== undefined ) {
328- if ( typeof defaultValue === 'object' ) {
323+ if ( typeof defaultValue === 'object' && ! Array . isArray ( defaultValue ) ) {
329324 if ( 'call' in defaultValue ) {
330325 objectFields . push (
331326 ts . factory . createPropertyAssignment (
@@ -371,18 +366,20 @@ export class TsSchemaGenerator {
371366 throw new Error ( `Unsupported default value type for field ${ field . name } ` ) ;
372367 }
373368 } else {
374- objectFields . push (
375- ts . factory . createPropertyAssignment (
376- 'default' ,
377- typeof defaultValue === 'string'
378- ? ts . factory . createStringLiteral ( defaultValue )
379- : typeof defaultValue === 'number'
380- ? ts . factory . createNumericLiteral ( defaultValue )
381- : defaultValue === true
382- ? ts . factory . createTrue ( )
383- : ts . factory . createFalse ( ) ,
384- ) ,
385- ) ;
369+ if ( Array . isArray ( defaultValue ) ) {
370+ objectFields . push (
371+ ts . factory . createPropertyAssignment (
372+ 'default' ,
373+ ts . factory . createArrayLiteralExpression (
374+ defaultValue . map ( ( item ) => this . createLiteralNode ( item as any ) ) ,
375+ ) ,
376+ ) ,
377+ ) ;
378+ } else {
379+ objectFields . push (
380+ ts . factory . createPropertyAssignment ( 'default' , this . createLiteralNode ( defaultValue ) ) ,
381+ ) ;
382+ }
386383 }
387384 }
388385
@@ -440,7 +437,7 @@ export class TsSchemaGenerator {
440437
441438 private getMappedDefault (
442439 field : DataModelField ,
443- ) : string | number | boolean | { call : string ; args : any [ ] } | { authMember : string [ ] } | undefined {
440+ ) : string | number | boolean | unknown [ ] | { call : string ; args : any [ ] } | { authMember : string [ ] } | undefined {
444441 const defaultAttr = getAttribute ( field , '@default' ) ;
445442 if ( ! defaultAttr ) {
446443 return undefined ;
@@ -456,6 +453,8 @@ export class TsSchemaGenerator {
456453 : [ 'Int' , 'Float' , 'Decimal' , 'BigInt' ] . includes ( field . type . type ! )
457454 ? Number ( lit )
458455 : lit ;
456+ } else if ( isArrayExpr ( defaultValue ) ) {
457+ return defaultValue . items . map ( ( item ) => this . getLiteral ( item ) ) ;
459458 } else if ( isReferenceExpr ( defaultValue ) && isEnumField ( defaultValue . target . ref ) ) {
460459 return defaultValue . target . ref . name ;
461460 } else if ( isInvocationExpr ( defaultValue ) ) {
@@ -681,9 +680,17 @@ export class TsSchemaGenerator {
681680 return ts . factory . createObjectLiteralExpression ( properties , true ) ;
682681 }
683682
684- private generateFieldTypeLiteral ( field : DataModelField ) : ts . Expression {
685- invariant ( field . type . type || field . type . reference , 'Field type must be a primitive or reference' ) ;
686- return ts . factory . createStringLiteral ( field . type . type ?? field . type . reference ! . $refText ) ;
683+ private generateFieldTypeLiteral ( field : DataModelField | ProcedureParam ) : ts . Expression {
684+ invariant (
685+ field . type . type || field . type . reference || field . type . unsupported ,
686+ 'Field type must be a primitive, reference, or Unsupported' ,
687+ ) ;
688+
689+ return field . type . type
690+ ? ts . factory . createStringLiteral ( field . type . type )
691+ : field . type . reference
692+ ? ts . factory . createStringLiteral ( field . type . reference . $refText )
693+ : ts . factory . createStringLiteral ( 'unknown' ) ;
687694 }
688695
689696 private createEnumObject ( e : Enum ) {
0 commit comments