@@ -77,6 +77,28 @@ export type Definition = {
77
77
typeof ?: "function"
78
78
} ;
79
79
80
+ function extend ( target : any , ..._ : any [ ] ) {
81
+ if ( target == null ) { // TypeError if undefined or null
82
+ throw new TypeError ( "Cannot convert undefined or null to object" ) ;
83
+ }
84
+
85
+ const to = Object ( target ) ;
86
+
87
+ for ( var index = 1 ; index < arguments . length ; index ++ ) {
88
+ const nextSource = arguments [ index ] ;
89
+
90
+ if ( nextSource != null ) { // Skip over if undefined or null
91
+ for ( const nextKey in nextSource ) {
92
+ // Avoid bugs when hasOwnProperty is shadowed
93
+ if ( Object . prototype . hasOwnProperty . call ( nextSource , nextKey ) ) {
94
+ to [ nextKey ] = nextSource [ nextKey ] ;
95
+ }
96
+ }
97
+ }
98
+ }
99
+ return to ;
100
+ }
101
+
80
102
export class JsonSchemaGenerator {
81
103
/**
82
104
* JSDoc keywords that should be used to annotate the JSON schema.
@@ -667,10 +689,22 @@ export class JsonSchemaGenerator {
667
689
if ( typ . flags & ts . TypeFlags . Union ) {
668
690
this . getUnionDefinition ( typ as ts . UnionType , prop ! , tc , unionModifier , definition ) ;
669
691
} else if ( typ . flags & ts . TypeFlags . Intersection ) {
670
- definition . allOf = [ ] ;
692
+ // extend object instead of using allOf because allOf does not work well with additional properties. See #107
693
+ if ( this . args . disableExtraProperties ) {
694
+ definition . additionalProperties = false ;
695
+ }
696
+
671
697
const types = ( < ts . IntersectionType > typ ) . types ;
672
698
for ( let i = 0 ; i < types . length ; ++ i ) {
673
- definition . allOf . push ( this . getTypeDefinition ( types [ i ] , tc ) ) ;
699
+ const other = this . getTypeDefinition ( types [ i ] , tc , false ) ;
700
+ definition . type = other . type ; // should always be object
701
+ definition . properties = extend ( definition . properties || { } , other . properties ) ;
702
+ if ( Object . keys ( other . default || { } ) . length > 0 ) {
703
+ definition . default = extend ( definition . default || { } , other . default ) ;
704
+ }
705
+ if ( other . required ) {
706
+ definition . required = ( definition . required || [ ] ) . concat ( other . required ) ;
707
+ }
674
708
}
675
709
} else if ( isRawType ) {
676
710
this . getDefinitionForRootType ( typ , tc , reffedType ! , definition ) ;
0 commit comments