@@ -4,6 +4,7 @@ const { MonoSchemaParser } = require('../mono-schema-parser');
4
4
5
5
class DiscriminatorSchemaParser extends MonoSchemaParser {
6
6
parse ( ) {
7
+ const ts = this . config . Ts ;
7
8
const { discriminator, ...noDiscriminatorSchema } = this . schema ;
8
9
9
10
if ( ! discriminator . mapping ) {
@@ -27,7 +28,7 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
27
28
abstractSchemaStruct,
28
29
} ) ;
29
30
30
- const schemaContent = this . config . Ts . IntersectionType (
31
+ const schemaContent = ts . IntersectionType (
31
32
[
32
33
abstractSchemaStruct ?. content ,
33
34
discriminatorSchemaStruct ?. content ,
@@ -40,7 +41,7 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
40
41
$parsedSchema : true ,
41
42
schemaType : SCHEMA_TYPES . COMPLEX ,
42
43
type : SCHEMA_TYPES . PRIMITIVE ,
43
- typeIdentifier : this . config . Ts . Keyword . Type ,
44
+ typeIdentifier : ts . Keyword . Type ,
44
45
name : this . typeName ,
45
46
description : this . schemaFormatters . formatDescription (
46
47
this . schema . description ,
@@ -50,6 +51,8 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
50
51
}
51
52
52
53
createDiscriminatorSchema = ( { skipMappingType, abstractSchemaStruct } ) => {
54
+ const ts = this . config . Ts ;
55
+
53
56
const refPath = this . schemaComponentsMap . createRef ( [
54
57
'components' ,
55
58
'schemas' ,
@@ -71,26 +74,26 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
71
74
} ) ;
72
75
73
76
if ( ableToCreateMappingType ) {
74
- mappingTypeName = this . schemaUtils . resolveTypeName (
75
- `${ abstractSchemaStruct . typeName } ${ discriminator . propertyName } ` ,
76
- {
77
- suffixes : this . config . extractingOptions . discriminatorMappingSuffix ,
78
- resolver :
79
- this . config . extractingOptions . discriminatorMappingNameResolver ,
80
- } ,
81
- ) ;
82
- this . schemaParserFabric . createSchema ( {
83
- linkedComponent : this . schemaComponentsMap . createComponent (
84
- this . schemaComponentsMap . createRef ( [
85
- 'components' ,
86
- 'schemas' ,
87
- mappingTypeName ,
88
- ] ) ,
77
+ const mappingTypeNameRef = this . schemaComponentsMap . createRef ( [
78
+ 'components' ,
79
+ 'schemas' ,
80
+ this . schemaUtils . resolveTypeName (
81
+ `${ abstractSchemaStruct . typeName } ${ discriminator . propertyName } ` ,
82
+ {
83
+ suffixes : this . config . extractingOptions . discriminatorMappingSuffix ,
84
+ resolver :
85
+ this . config . extractingOptions . discriminatorMappingNameResolver ,
86
+ } ,
89
87
) ,
90
- content : this . config . Ts . IntersectionType ( [
91
- this . config . Ts . ObjectWrapper (
92
- this . config . Ts . TypeField ( {
93
- key : discriminator . propertyName ,
88
+ ] ) ;
89
+ const mappingTypeNameComponent =
90
+ this . schemaComponentsMap . createComponent ( mappingTypeNameRef ) ;
91
+ const mappingTypeNameSchema = this . schemaParserFabric . createSchema ( {
92
+ linkedComponent : mappingTypeNameComponent ,
93
+ content : ts . IntersectionType ( [
94
+ ts . ObjectWrapper (
95
+ ts . TypeField ( {
96
+ key : ts . StringValue ( discriminator . propertyName ) ,
94
97
value : 'Key' ,
95
98
} ) ,
96
99
) ,
@@ -99,6 +102,8 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
99
102
genericArgs : [ { name : 'Key' } , { name : 'Type' } ] ,
100
103
internal : true ,
101
104
} ) ;
105
+
106
+ mappingTypeName = mappingTypeNameSchema . typeData . name ;
102
107
}
103
108
104
109
/** returns (GenericType<"mapping_key", MappingType>) or ({ discriminatorProperty: "mapping_key" } & MappingType) */
@@ -112,18 +117,15 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
112
117
113
118
const mappingUsageKey =
114
119
mappingPropertySchemaEnumKeysMap [ mappingKey ] ||
115
- this . config . Ts . StringValue ( mappingKey ) ;
120
+ ts . StringValue ( mappingKey ) ;
116
121
117
122
if ( ableToCreateMappingType ) {
118
- return this . config . Ts . TypeWithGeneric ( mappingTypeName , [
119
- mappingUsageKey ,
120
- content ,
121
- ] ) ;
123
+ return ts . TypeWithGeneric ( mappingTypeName , [ mappingUsageKey , content ] ) ;
122
124
} else {
123
- return this . config . Ts . ExpressionGroup (
124
- this . config . Ts . IntersectionType ( [
125
- this . config . Ts . ObjectWrapper (
126
- this . config . Ts . TypeField ( {
125
+ return ts . ExpressionGroup (
126
+ ts . IntersectionType ( [
127
+ ts . ObjectWrapper (
128
+ ts . TypeField ( {
127
129
key : discriminator . propertyName ,
128
130
value : mappingUsageKey ,
129
131
} ) ,
@@ -151,9 +153,7 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
151
153
152
154
if ( skipMappingType ) return null ;
153
155
154
- const content = this . config . Ts . ExpressionGroup (
155
- this . config . Ts . UnionType ( mappingContents ) ,
156
- ) ;
156
+ const content = ts . ExpressionGroup ( ts . UnionType ( mappingContents ) ) ;
157
157
158
158
return {
159
159
content,
@@ -164,6 +164,8 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
164
164
abstractSchemaStruct,
165
165
discPropertyName,
166
166
} ) => {
167
+ const ts = this . config . Ts ;
168
+
167
169
let mappingPropertySchemaEnumKeysMap = { } ;
168
170
let mappingPropertySchema = _ . get (
169
171
abstractSchemaStruct ?. component ?. rawTypeData ,
@@ -183,7 +185,7 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
183
185
( acc , key , index ) => {
184
186
const enumKey =
185
187
mappingPropertySchema . rawTypeData . $parsed . content [ index ] . key ;
186
- acc [ key ] = this . config . Ts . EnumUsageKey (
188
+ acc [ key ] = ts . EnumUsageKey (
187
189
mappingPropertySchema . rawTypeData . $parsed . typeName ,
188
190
enumKey ,
189
191
) ;
@@ -284,12 +286,13 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
284
286
} ;
285
287
286
288
createComplexSchemaStruct = ( ) => {
289
+ const ts = this . config . Ts ;
287
290
const complexType = this . schemaUtils . getComplexType ( this . schema ) ;
288
291
289
292
if ( complexType === SCHEMA_TYPES . COMPLEX_UNKNOWN ) return null ;
290
293
291
294
return {
292
- content : this . config . Ts . ExpressionGroup (
295
+ content : ts . ExpressionGroup (
293
296
this . schemaParser . _complexSchemaParsers [ complexType ] ( this . schema ) ,
294
297
) ,
295
298
} ;
0 commit comments