11import type { Model } from '../../../client/interfaces/Model' ;
2+ import { findOneOfParentDiscriminator , mapPropertyValue } from '../../../utils/discriminator' ;
23import { getPattern } from '../../../utils/getPattern' ;
34import type { OpenApi } from '../interfaces/OpenApi' ;
45import type { OpenApiSchema } from '../interfaces/OpenApiSchema' ;
@@ -10,77 +11,82 @@ import { getType } from './getType';
1011// Fix for circular dependency
1112export type GetModelFn = typeof getModel ;
1213
13- export function getModelProperties ( openApi : OpenApi , definition : OpenApiSchema , getModel : GetModelFn ) : Model [ ] {
14+ export function getModelProperties (
15+ openApi : OpenApi ,
16+ definition : OpenApiSchema ,
17+ getModel : GetModelFn ,
18+ parent ?: Model
19+ ) : Model [ ] {
1420 const models : Model [ ] = [ ] ;
21+ const discriminator = findOneOfParentDiscriminator ( openApi , parent ) ;
1522 for ( const propertyName in definition . properties ) {
1623 if ( definition . properties . hasOwnProperty ( propertyName ) ) {
1724 const property = definition . properties [ propertyName ] ;
1825 const propertyRequired = ! ! definition . required ?. includes ( propertyName ) ;
19- if ( property . $ref ) {
26+ const propertyValues = {
27+ name : escapeName ( propertyName ) ,
28+ description : getComment ( property . description ) ,
29+ isDefinition : false ,
30+ isReadOnly : property . readOnly === true ,
31+ isRequired : propertyRequired ,
32+ format : property . format ,
33+ maximum : property . maximum ,
34+ exclusiveMaximum : property . exclusiveMaximum ,
35+ minimum : property . minimum ,
36+ exclusiveMinimum : property . exclusiveMinimum ,
37+ multipleOf : property . multipleOf ,
38+ maxLength : property . maxLength ,
39+ minLength : property . minLength ,
40+ maxItems : property . maxItems ,
41+ minItems : property . minItems ,
42+ uniqueItems : property . uniqueItems ,
43+ maxProperties : property . maxProperties ,
44+ minProperties : property . minProperties ,
45+ pattern : getPattern ( property . pattern ) ,
46+ } ;
47+ if ( parent && discriminator ?. propertyName == propertyName ) {
48+ models . push ( {
49+ export : 'reference' ,
50+ type : 'string' ,
51+ base : `'${ mapPropertyValue ( discriminator , parent ) } '` ,
52+ template : null ,
53+ isNullable : property . nullable === true ,
54+ link : null ,
55+ imports : [ ] ,
56+ enum : [ ] ,
57+ enums : [ ] ,
58+ properties : [ ] ,
59+ ...propertyValues ,
60+ } ) ;
61+ } else if ( property . $ref ) {
2062 const model = getType ( property . $ref ) ;
2163 models . push ( {
22- name : escapeName ( propertyName ) ,
2364 export : 'reference' ,
2465 type : model . type ,
2566 base : model . base ,
2667 template : model . template ,
2768 link : null ,
28- description : getComment ( property . description ) ,
29- isDefinition : false ,
30- isReadOnly : property . readOnly === true ,
31- isRequired : propertyRequired ,
3269 isNullable : model . isNullable || property . nullable === true ,
33- format : property . format ,
34- maximum : property . maximum ,
35- exclusiveMaximum : property . exclusiveMaximum ,
36- minimum : property . minimum ,
37- exclusiveMinimum : property . exclusiveMinimum ,
38- multipleOf : property . multipleOf ,
39- maxLength : property . maxLength ,
40- minLength : property . minLength ,
41- maxItems : property . maxItems ,
42- minItems : property . minItems ,
43- uniqueItems : property . uniqueItems ,
44- maxProperties : property . maxProperties ,
45- minProperties : property . minProperties ,
46- pattern : getPattern ( property . pattern ) ,
4770 imports : model . imports ,
4871 enum : [ ] ,
4972 enums : [ ] ,
5073 properties : [ ] ,
74+ ...propertyValues ,
5175 } ) ;
5276 } else {
5377 const model = getModel ( openApi , property ) ;
5478 models . push ( {
55- name : escapeName ( propertyName ) ,
5679 export : model . export ,
5780 type : model . type ,
5881 base : model . base ,
5982 template : model . template ,
6083 link : model . link ,
61- description : getComment ( property . description ) ,
62- isDefinition : false ,
63- isReadOnly : property . readOnly === true ,
64- isRequired : propertyRequired ,
6584 isNullable : model . isNullable || property . nullable === true ,
66- format : property . format ,
67- maximum : property . maximum ,
68- exclusiveMaximum : property . exclusiveMaximum ,
69- minimum : property . minimum ,
70- exclusiveMinimum : property . exclusiveMinimum ,
71- multipleOf : property . multipleOf ,
72- maxLength : property . maxLength ,
73- minLength : property . minLength ,
74- maxItems : property . maxItems ,
75- minItems : property . minItems ,
76- uniqueItems : property . uniqueItems ,
77- maxProperties : property . maxProperties ,
78- minProperties : property . minProperties ,
79- pattern : getPattern ( property . pattern ) ,
8085 imports : model . imports ,
8186 enum : model . enum ,
8287 enums : model . enums ,
8388 properties : model . properties ,
89+ ...propertyValues ,
8490 } ) ;
8591 }
8692 }
0 commit comments