@@ -537,6 +537,51 @@ export class JsonSchemaGenerator {
537
537
return definition ;
538
538
}
539
539
540
+ private getIntersectionDefinition ( intersectionType : ts . IntersectionType , tc : ts . TypeChecker , definition : Definition ) {
541
+ const simpleTypes : string [ ] = [ ] ;
542
+ const schemas : Definition [ ] = [ ] ;
543
+
544
+ const addSimpleType = ( type : string ) => {
545
+ if ( simpleTypes . indexOf ( type ) === - 1 ) {
546
+ simpleTypes . push ( type ) ;
547
+ }
548
+ } ;
549
+
550
+ for ( let i = 0 ; i < intersectionType . types . length ; ++ i ) {
551
+ const def = this . getTypeDefinition ( intersectionType . types [ i ] , tc ) ;
552
+ if ( def . type === "undefined" ) {
553
+ console . error ( "Undefined in intersection makes no sense." ) ;
554
+ } else {
555
+ const keys = Object . keys ( def ) ;
556
+ if ( keys . length === 1 && keys [ 0 ] === "type" ) {
557
+ if ( typeof def . type !== "string" ) {
558
+ console . error ( "Expected only a simple type." ) ;
559
+ } else {
560
+ addSimpleType ( def . type ) ;
561
+ }
562
+ } else {
563
+ schemas . push ( def ) ;
564
+ }
565
+ }
566
+ }
567
+
568
+ if ( simpleTypes . length > 0 ) {
569
+ schemas . push ( { type : simpleTypes . length === 1 ? simpleTypes [ 0 ] : simpleTypes } ) ;
570
+ }
571
+
572
+ if ( schemas . length === 1 ) {
573
+ for ( let k in schemas [ 0 ] ) {
574
+ if ( schemas [ 0 ] . hasOwnProperty ( k ) ) {
575
+ definition [ k ] = schemas [ 0 ] [ k ] ;
576
+ }
577
+ }
578
+ } else {
579
+ definition . allOf = schemas ;
580
+ }
581
+ return definition ;
582
+ }
583
+
584
+
540
585
private getClassDefinition ( clazzType : ts . Type , tc : ts . TypeChecker , definition : Definition ) : Definition {
541
586
const node = clazzType . getSymbol ( ) ! . getDeclarations ( ) ! [ 0 ] ;
542
587
if ( this . args . typeOfKeyword && node . kind === ts . SyntaxKind . FunctionType ) {
@@ -797,22 +842,26 @@ export class JsonSchemaGenerator {
797
842
if ( typ . flags & ts . TypeFlags . Union ) {
798
843
this . getUnionDefinition ( typ as ts . UnionType , prop ! , tc , unionModifier , definition ) ;
799
844
} else if ( typ . flags & ts . TypeFlags . Intersection ) {
800
- // extend object instead of using allOf because allOf does not work well with additional properties. See #107
801
845
if ( this . args . noExtraProps ) {
802
- definition . additionalProperties = false ;
803
- }
804
-
805
- const types = ( < ts . IntersectionType > typ ) . types ;
806
- for ( let i = 0 ; i < types . length ; ++ i ) {
807
- const other = this . getTypeDefinition ( types [ i ] , tc , false ) ;
808
- definition . type = other . type ; // should always be object
809
- definition . properties = extend ( definition . properties || { } , other . properties ) ;
810
- if ( Object . keys ( other . default || { } ) . length > 0 ) {
811
- definition . default = extend ( definition . default || { } , other . default ) ;
846
+ // extend object instead of using allOf because allOf does not work well with additional properties. See #107
847
+ if ( this . args . noExtraProps ) {
848
+ definition . additionalProperties = false ;
812
849
}
813
- if ( other . required ) {
814
- definition . required = unique ( ( definition . required || [ ] ) . concat ( other . required ) ) . sort ( ) ;
850
+
851
+ const types = ( < ts . IntersectionType > typ ) . types ;
852
+ for ( let i = 0 ; i < types . length ; ++ i ) {
853
+ const other = this . getTypeDefinition ( types [ i ] , tc , false ) ;
854
+ definition . type = other . type ; // should always be object
855
+ definition . properties = extend ( definition . properties || { } , other . properties ) ;
856
+ if ( Object . keys ( other . default || { } ) . length > 0 ) {
857
+ definition . default = extend ( definition . default || { } , other . default ) ;
858
+ }
859
+ if ( other . required ) {
860
+ definition . required = unique ( ( definition . required || [ ] ) . concat ( other . required ) ) . sort ( ) ;
861
+ }
815
862
}
863
+ } else {
864
+ this . getIntersectionDefinition ( typ as ts . IntersectionType , tc , definition ) ;
816
865
}
817
866
} else if ( isRawType ) {
818
867
if ( pairedSymbol ) {
0 commit comments