@@ -59,6 +59,7 @@ export function getDefaultArgs(): Args {
59
59
id : "" ,
60
60
defaultNumberType : "number" ,
61
61
tsNodeRegister : false ,
62
+ constAsEnum : false ,
62
63
} ;
63
64
}
64
65
@@ -89,6 +90,7 @@ export type Args = {
89
90
id : string ;
90
91
defaultNumberType : "number" | "integer" ;
91
92
tsNodeRegister : boolean ;
93
+ constAsEnum : boolean ;
92
94
} ;
93
95
94
96
export type PartialArgs = Partial < Args > ;
@@ -486,6 +488,12 @@ export class JsonSchemaGenerator {
486
488
*/
487
489
private userValidationKeywords : ValidationKeywords ;
488
490
491
+ /**
492
+ * If true, this makes constants be defined as enums with a single value. This is useful
493
+ * for cases where constant values are not supported, such as OpenAPI.
494
+ */
495
+ private constAsEnum : boolean ;
496
+
489
497
/**
490
498
* Types are assigned names which are looked up by their IDs. This is the
491
499
* map from type IDs to type names.
@@ -511,6 +519,7 @@ export class JsonSchemaGenerator {
511
519
this . inheritingTypes = inheritingTypes ;
512
520
this . tc = tc ;
513
521
this . userValidationKeywords = args . validationKeywords . reduce ( ( acc , word ) => ( { ...acc , [ word ] : true } ) , { } ) ;
522
+ this . constAsEnum = args . constAsEnum ;
514
523
}
515
524
516
525
public get ReffedDefinitions ( ) : { [ key : string ] : Definition } {
@@ -709,7 +718,11 @@ export class JsonSchemaGenerator {
709
718
default :
710
719
throw new Error ( `Not supported: ${ value } as a enum value` ) ;
711
720
}
712
- definition . const = value ;
721
+ if ( this . constAsEnum ) {
722
+ definition . enum = [ value ] ;
723
+ } else {
724
+ definition . const = value ;
725
+ }
713
726
} else if ( arrayType !== undefined ) {
714
727
if (
715
728
propertyType . flags & ts . TypeFlags . Object &&
0 commit comments