@@ -2,6 +2,7 @@ import { ReferenceObject, SchemaObject } from 'openapi3-ts/oas30';
2
2
import { resolveExampleRefs , resolveObject , resolveValue } from '../resolvers' ;
3
3
import {
4
4
ContextSpecs ,
5
+ PropertySortOrder ,
5
6
ScalarValue ,
6
7
SchemaType ,
7
8
SchemaWithConst ,
@@ -70,98 +71,100 @@ export const getObject = ({
70
71
}
71
72
72
73
if ( item . properties && Object . entries ( item . properties ) . length > 0 ) {
73
- return Object . entries ( item . properties )
74
- . sort ( ( a , b ) => {
74
+ const entries = Object . entries ( item . properties ) ;
75
+ if ( context . output . propertySortOrder === PropertySortOrder . ALPHABETICAL ) {
76
+ entries . sort ( ( a , b ) => {
75
77
return a [ 0 ] . localeCompare ( b [ 0 ] ) ;
76
- } )
77
- . reduce (
78
- (
79
- acc ,
80
- [ key , schema ] : [ string , ReferenceObject | SchemaObject ] ,
81
- index ,
82
- arr ,
83
- ) => {
84
- const isRequired = (
85
- Array . isArray ( item . required ) ? item . required : [ ]
86
- ) . includes ( key ) ;
87
-
88
- let propName = '' ;
89
-
90
- if ( name ) {
91
- const isKeyStartWithUnderscore = key . startsWith ( '_' ) ;
92
-
93
- propName += pascal (
94
- `${ isKeyStartWithUnderscore ? '_' : '' } ${ name } _${ key } ` ,
95
- ) ;
96
- }
97
-
98
- const allSpecSchemas =
99
- context . specs [ context . target ] ?. components ?. schemas ?? { } ;
100
-
101
- const isNameAlreadyTaken = Object . keys ( allSpecSchemas ) . some (
102
- ( schemaName ) => pascal ( schemaName ) === propName ,
78
+ } ) ;
79
+ }
80
+ return entries . reduce (
81
+ (
82
+ acc ,
83
+ [ key , schema ] : [ string , ReferenceObject | SchemaObject ] ,
84
+ index ,
85
+ arr ,
86
+ ) => {
87
+ const isRequired = (
88
+ Array . isArray ( item . required ) ? item . required : [ ]
89
+ ) . includes ( key ) ;
90
+
91
+ let propName = '' ;
92
+
93
+ if ( name ) {
94
+ const isKeyStartWithUnderscore = key . startsWith ( '_' ) ;
95
+
96
+ propName += pascal (
97
+ `${ isKeyStartWithUnderscore ? '_' : '' } ${ name } _${ key } ` ,
103
98
) ;
104
-
105
- if ( isNameAlreadyTaken ) {
106
- propName = propName + 'Property' ;
107
- }
108
-
109
- const resolvedValue = resolveObject ( {
110
- schema ,
111
- propName ,
112
- context ,
113
- } ) ;
114
-
115
- const isReadOnly = item . readOnly || ( schema as SchemaObject ) . readOnly ;
116
- if ( ! index ) {
117
- acc . value += '{' ;
118
- }
119
-
120
- const doc = jsDoc ( schema as SchemaObject , true ) ;
121
-
122
- acc . hasReadonlyProps ||= isReadOnly || false ;
123
- acc . imports . push ( ... resolvedValue . imports ) ;
124
- acc . value += `\n ${ doc ? ` ${ doc } ` : '' } $ {
125
- isReadOnly && ! context . output . override . suppressReadonlyModifier
126
- ? 'readonly '
127
- : ''
128
- } ${ getKey ( key ) } ${ isRequired ? '' : '?' } : ${ resolvedValue . value } ;` ;
129
- acc . schemas . push ( ... resolvedValue . schemas ) ;
130
-
131
- if ( arr . length - 1 === index ) {
132
- if ( item . additionalProperties ) {
133
- if ( isBoolean ( item . additionalProperties ) ) {
134
- acc . value += `\n [key: string]: unknown;\n }` ;
135
- } else {
136
- const resolvedValue = resolveValue ( {
137
- schema : item . additionalProperties ,
138
- name ,
139
- context ,
140
- } ) ;
141
- acc . value += `\n [key: string]: ${ resolvedValue . value } ;\n}` ;
142
- }
99
+ }
100
+
101
+ const allSpecSchemas =
102
+ context . specs [ context . target ] ?. components ?. schemas ?? { } ;
103
+
104
+ const isNameAlreadyTaken = Object . keys ( allSpecSchemas ) . some (
105
+ ( schemaName ) => pascal ( schemaName ) === propName ,
106
+ ) ;
107
+
108
+ if ( isNameAlreadyTaken ) {
109
+ propName = propName + 'Property' ;
110
+ }
111
+
112
+ const resolvedValue = resolveObject ( {
113
+ schema ,
114
+ propName ,
115
+ context ,
116
+ } ) ;
117
+
118
+ const isReadOnly = item . readOnly || ( schema as SchemaObject ) . readOnly ;
119
+ if ( ! index ) {
120
+ acc . value += '{' ;
121
+ }
122
+
123
+ const doc = jsDoc ( schema as SchemaObject , true ) ;
124
+
125
+ acc . hasReadonlyProps ||= isReadOnly || false ;
126
+ acc . imports . push ( ... resolvedValue . imports ) ;
127
+ acc . value += `\n ${ doc ? ` ${ doc } ` : '' } $ {
128
+ isReadOnly && ! context . output . override . suppressReadonlyModifier
129
+ ? 'readonly '
130
+ : ''
131
+ } ${ getKey ( key ) } ${ isRequired ? '' : '?' } : ${ resolvedValue . value } ;` ;
132
+ acc . schemas . push ( ... resolvedValue . schemas ) ;
133
+
134
+ if ( arr . length - 1 === index ) {
135
+ if ( item . additionalProperties ) {
136
+ if ( isBoolean ( item . additionalProperties ) ) {
137
+ acc . value += `\n [key: string]: unknown;\n }` ;
143
138
} else {
144
- acc . value += '\n}' ;
139
+ const resolvedValue = resolveValue ( {
140
+ schema : item . additionalProperties ,
141
+ name,
142
+ context,
143
+ } ) ;
144
+ acc . value += `\n [key: string]: ${ resolvedValue . value } ;\n}` ;
145
145
}
146
-
147
- acc . value += nullable ;
146
+ } else {
147
+ acc . value += '\n}' ;
148
148
}
149
149
150
- return acc ;
151
- } ,
152
- {
153
- imports : [ ] ,
154
- schemas : [ ] ,
155
- value : '' ,
156
- isEnum : false ,
157
- type : 'object' as SchemaType ,
158
- isRef : false ,
159
- schema : { } ,
160
- hasReadonlyProps : false ,
161
- example : item . example ,
162
- examples : resolveExampleRefs ( item . examples , context ) ,
163
- } as ScalarValue ,
164
- ) ;
150
+ acc . value += nullable ;
151
+ }
152
+
153
+ return acc ;
154
+ } ,
155
+ {
156
+ imports : [ ] ,
157
+ schemas : [ ] ,
158
+ value : '' ,
159
+ isEnum : false ,
160
+ type : 'object' as SchemaType ,
161
+ isRef : false ,
162
+ schema : { } ,
163
+ hasReadonlyProps : false ,
164
+ example : item . example ,
165
+ examples : resolveExampleRefs ( item . examples , context ) ,
166
+ } as ScalarValue ,
167
+ ) ;
165
168
}
166
169
167
170
if ( item . additionalProperties ) {
0 commit comments