@@ -4,6 +4,7 @@ const { MonoSchemaParser } = require('../mono-schema-parser');
44
55class DiscriminatorSchemaParser extends MonoSchemaParser {
66 parse ( ) {
7+ const ts = this . config . Ts ;
78 const { discriminator, ...noDiscriminatorSchema } = this . schema ;
89
910 if ( ! discriminator . mapping ) {
@@ -27,7 +28,7 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
2728 abstractSchemaStruct,
2829 } ) ;
2930
30- const schemaContent = this . config . Ts . IntersectionType (
31+ const schemaContent = ts . IntersectionType (
3132 [
3233 abstractSchemaStruct ?. content ,
3334 discriminatorSchemaStruct ?. content ,
@@ -40,7 +41,7 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
4041 $parsedSchema : true ,
4142 schemaType : SCHEMA_TYPES . COMPLEX ,
4243 type : SCHEMA_TYPES . PRIMITIVE ,
43- typeIdentifier : this . config . Ts . Keyword . Type ,
44+ typeIdentifier : ts . Keyword . Type ,
4445 name : this . typeName ,
4546 description : this . schemaFormatters . formatDescription (
4647 this . schema . description ,
@@ -50,6 +51,8 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
5051 }
5152
5253 createDiscriminatorSchema = ( { skipMappingType, abstractSchemaStruct } ) => {
54+ const ts = this . config . Ts ;
55+
5356 const refPath = this . schemaComponentsMap . createRef ( [
5457 'components' ,
5558 'schemas' ,
@@ -71,26 +74,26 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
7174 } ) ;
7275
7376 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+ } ,
8987 ) ,
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 ) ,
9497 value : 'Key' ,
9598 } ) ,
9699 ) ,
@@ -99,6 +102,8 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
99102 genericArgs : [ { name : 'Key' } , { name : 'Type' } ] ,
100103 internal : true ,
101104 } ) ;
105+
106+ mappingTypeName = mappingTypeNameSchema . typeData . name ;
102107 }
103108
104109 /** returns (GenericType<"mapping_key", MappingType>) or ({ discriminatorProperty: "mapping_key" } & MappingType) */
@@ -112,18 +117,15 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
112117
113118 const mappingUsageKey =
114119 mappingPropertySchemaEnumKeysMap [ mappingKey ] ||
115- this . config . Ts . StringValue ( mappingKey ) ;
120+ ts . StringValue ( mappingKey ) ;
116121
117122 if ( ableToCreateMappingType ) {
118- return this . config . Ts . TypeWithGeneric ( mappingTypeName , [
119- mappingUsageKey ,
120- content ,
121- ] ) ;
123+ return ts . TypeWithGeneric ( mappingTypeName , [ mappingUsageKey , content ] ) ;
122124 } 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 ( {
127129 key : discriminator . propertyName ,
128130 value : mappingUsageKey ,
129131 } ) ,
@@ -151,9 +153,7 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
151153
152154 if ( skipMappingType ) return null ;
153155
154- const content = this . config . Ts . ExpressionGroup (
155- this . config . Ts . UnionType ( mappingContents ) ,
156- ) ;
156+ const content = ts . ExpressionGroup ( ts . UnionType ( mappingContents ) ) ;
157157
158158 return {
159159 content,
@@ -164,6 +164,8 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
164164 abstractSchemaStruct,
165165 discPropertyName,
166166 } ) => {
167+ const ts = this . config . Ts ;
168+
167169 let mappingPropertySchemaEnumKeysMap = { } ;
168170 let mappingPropertySchema = _ . get (
169171 abstractSchemaStruct ?. component ?. rawTypeData ,
@@ -183,7 +185,7 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
183185 ( acc , key , index ) => {
184186 const enumKey =
185187 mappingPropertySchema . rawTypeData . $parsed . content [ index ] . key ;
186- acc [ key ] = this . config . Ts . EnumUsageKey (
188+ acc [ key ] = ts . EnumUsageKey (
187189 mappingPropertySchema . rawTypeData . $parsed . typeName ,
188190 enumKey ,
189191 ) ;
@@ -284,12 +286,13 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
284286 } ;
285287
286288 createComplexSchemaStruct = ( ) => {
289+ const ts = this . config . Ts ;
287290 const complexType = this . schemaUtils . getComplexType ( this . schema ) ;
288291
289292 if ( complexType === SCHEMA_TYPES . COMPLEX_UNKNOWN ) return null ;
290293
291294 return {
292- content : this . config . Ts . ExpressionGroup (
295+ content : ts . ExpressionGroup (
293296 this . schemaParser . _complexSchemaParsers [ complexType ] ( this . schema ) ,
294297 ) ,
295298 } ;
0 commit comments