@devlearning/swagger-generator 0.0.15 → 0.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/generator.ts CHANGED
@@ -10,39 +10,43 @@ import { MODEL_POST, MODEL_PRE } from './model.constants.js';
10
10
  const contentType = 'application/json';
11
11
 
12
12
  export class Generator {
13
- private _swagger: Swagger;
14
- private _outputDirectory: string;
15
-
16
- constructor(swagger: Swagger, outputDirectory: string) {
17
- this._swagger = swagger;
18
- this._outputDirectory = outputDirectory;
19
- }
20
-
21
- generateApi() {
22
- console.debug(`Start autogeneration Apis`);
23
-
24
- let apiMethods = ``;
25
- for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
26
- const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
27
- const swaggerMethod = this._swagger.paths[apiName];
28
- const method = Object.getOwnPropertyNames(swaggerMethod)[0];
29
- const swaggerMethodInfo = swaggerMethod[method];
30
- console.debug(`\tAPI - ${apiName} - ${method}`);
31
-
32
- let parametersString = this.retrieveParameters(swaggerMethodInfo);
33
- let queryParameters = this.retrieveQueryParameters(swaggerMethodInfo);
34
- let returnTypeString = this.retrieveReturnType(swaggerMethodInfo);
35
- if (returnTypeString == null) returnTypeString = 'void';
36
- let prepareRequestString = ``; //request = this._handleRequest(request);
37
- let haveRequest = swaggerMethodInfo.requestBody != null;
38
-
39
- if (haveRequest) {
40
- prepareRequestString = `request = this._handleRequest(request);
13
+ private _swagger: Swagger;
14
+ private _outputDirectory: string;
15
+
16
+ constructor(swagger: Swagger, outputDirectory: string) {
17
+ this._swagger = swagger;
18
+ this._outputDirectory = outputDirectory;
19
+ }
20
+
21
+ generateApi() {
22
+ console.debug(`Start autogeneration Apis`);
23
+
24
+ let apiMethods = ``;
25
+ for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
26
+ const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
27
+ const swaggerMethod = this._swagger.paths[apiName];
28
+ const method = Object.getOwnPropertyNames(swaggerMethod)[0];
29
+ const swaggerMethodInfo = swaggerMethod[method];
30
+ console.debug(`\tAPI - ${apiName} - ${method}`);
31
+
32
+ if (apiName == '/api/v{version}/TransportOrder/ListByServiceRequest') {
33
+ console.debug('');
34
+ }
35
+
36
+ let parametersString = this.retrieveParameters(swaggerMethodInfo);
37
+ let queryParameters = this.retrieveQueryParameters(swaggerMethodInfo);
38
+ let returnTypeString = this.retrieveReturnType(swaggerMethodInfo);
39
+ if (returnTypeString == null) returnTypeString = 'void';
40
+ let prepareRequestString = ``; //request = this._handleRequest(request);
41
+ let haveRequest = swaggerMethodInfo.requestBody != null;
42
+
43
+ if (haveRequest) {
44
+ prepareRequestString = `request = this._handleRequest(request);
41
45
  `;
42
- }
46
+ }
43
47
 
44
- apiMethods +=
45
- `
48
+ apiMethods +=
49
+ `
46
50
  public ${apiName.replace('/api/v{version}/', '').replaceAll('/', '_')}(${parametersString}): Observable<${returnTypeString}> {
47
51
  ${prepareRequestString}return this._http.${method}<${returnTypeString}>(\`\${this._baseUrl}${apiName.replace('{version}', '1')}${queryParameters}\`${haveRequest ? ', request' : ''}, httpOptions)
48
52
  .pipe(
@@ -51,252 +55,267 @@ export class Generator {
51
55
  );
52
56
  }
53
57
  `;
54
- }
58
+ }
55
59
 
56
- fs.writeFileSync(this._outputDirectory + "/api.autogenerated.ts",
57
- `${API_PRE}
60
+ fs.writeFileSync(this._outputDirectory + "/api.autogenerated.ts",
61
+ `${API_PRE}
58
62
  ${apiMethods}
59
63
  ${API_POST}`,
60
- { flag: 'w' });
61
- }
64
+ { flag: 'w' });
65
+ }
62
66
 
63
- generateModel() {
64
- let usedTypes: string[] = [];
67
+ generateModel() {
68
+ let usedTypes: string[] = [];
65
69
 
66
- for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
67
- const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
68
- const swaggerMethod = this._swagger.paths[apiName];
69
- const method = Object.getOwnPropertyNames(swaggerMethod)[0];
70
- const swaggerMethodInfo = swaggerMethod[method];
70
+ for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
71
+ const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
72
+ const swaggerMethod = this._swagger.paths[apiName];
73
+ const method = Object.getOwnPropertyNames(swaggerMethod)[0];
74
+ const swaggerMethodInfo = swaggerMethod[method];
71
75
 
72
- // if(apiName == "/api/v{version}/Ticket/Create"){
73
- // debugger
74
- // }
75
- let parametersRefType = swaggerMethodInfo.parameters.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''))
76
- usedTypes = usedTypes.concat(parametersRefType);
76
+ // if(apiName == "/api/v{version}/Ticket/Create"){
77
+ // debugger
78
+ // }
79
+ let parametersRefType = swaggerMethodInfo.parameters.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''))
80
+ usedTypes = usedTypes.concat(parametersRefType);
77
81
 
78
- if (swaggerMethodInfo.responses[200].content[contentType].schema.$ref != null) {
79
- usedTypes.push(swaggerMethodInfo.responses[200].content[contentType].schema.$ref.replace('#/components/schemas/', ''));
80
- }
82
+ if (swaggerMethodInfo.responses[200].content[contentType].schema.$ref != null) {
83
+ usedTypes.push(swaggerMethodInfo.responses[200].content[contentType].schema.$ref.replace('#/components/schemas/', ''));
84
+ }
81
85
 
82
- if (swaggerMethodInfo.requestBody?.content[contentType]?.schema?.$ref) {
83
- usedTypes.push(swaggerMethodInfo.requestBody?.content[contentType]?.schema?.$ref.replace('#/components/schemas/', ''));
84
- }
85
- }
86
+ if (swaggerMethodInfo.requestBody?.content[contentType]?.schema?.$ref) {
87
+ usedTypes.push(swaggerMethodInfo.requestBody?.content[contentType]?.schema?.$ref.replace('#/components/schemas/', ''));
88
+ }
89
+ }
86
90
 
87
- this.retrieveNestedObjects(usedTypes);
91
+ this.retrieveNestedObjects(usedTypes);
88
92
 
89
- usedTypes = [...new Set(usedTypes.map(item => item))]; // [ 'A', 'B']
93
+ usedTypes = [...new Set(usedTypes.map(item => item))]; // [ 'A', 'B']
90
94
 
91
- // usedTypes = usedTypes.filter((value, index, array) => {
92
- // array.indexOf(value) === index;
93
- // });
94
- // usedTypes.forEach(element => {
95
- // console.debug(element);
96
- // });
95
+ // usedTypes = usedTypes.filter((value, index, array) => {
96
+ // array.indexOf(value) === index;
97
+ // });
98
+ // usedTypes.forEach(element => {
99
+ // console.debug(element);
100
+ // });
97
101
 
98
- console.debug(`Start autogeneration Models`);
99
- let models = ``;
100
- for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.components.schemas).length; index++) {
101
- const modelName = Object.getOwnPropertyNames(this._swagger.components.schemas)[index];
102
+ console.debug(`Start autogeneration Models`);
103
+ let models = ``;
104
+ for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.components.schemas).length; index++) {
105
+ const modelName = Object.getOwnPropertyNames(this._swagger.components.schemas)[index];
102
106
 
103
- if (modelName == 'ActivatedVoucherSnackListResponse') {
104
- console.debug("ActivatedVoucherSnackListResponse");
105
- }
107
+ if (modelName == 'ActivatedVoucherSnackListResponse') {
108
+ console.debug("ActivatedVoucherSnackListResponse");
109
+ }
106
110
 
107
- if (usedTypes.indexOf(modelName) < 0) {
108
- console.debug(`\tModel SKIP - ${modelName}`);
109
- continue
110
- };
111
+ if (usedTypes.indexOf(modelName) < 0) {
112
+ console.debug(`\tModel SKIP - ${modelName}`);
113
+ continue
114
+ };
111
115
 
112
- const swaggerCopmponent = this._swagger.components.schemas[modelName];
116
+ const swaggerCopmponent = this._swagger.components.schemas[modelName];
113
117
 
114
- console.debug(`\tModel - ${modelName}`);
118
+ console.debug(`\tModel - ${modelName}`);
115
119
 
116
- let type = swaggerCopmponent.type == 'integer' ? 'enum' : 'class';
117
- let content = this.retrieveObjectContent(modelName, swaggerCopmponent);
120
+ let type = swaggerCopmponent.type == 'integer' ? 'enum' : 'class';
121
+ let content = this.retrieveObjectContent(modelName, swaggerCopmponent);
118
122
 
119
- models +=
120
- `
123
+ models +=
124
+ `
121
125
  export ${type} ${modelName} {${content}
122
126
  }
123
127
  `;
124
- }
128
+ }
125
129
 
126
- fs.writeFileSync(this._outputDirectory + "/model.autogenerated.ts",
127
- `${MODEL_PRE}
130
+ fs.writeFileSync(this._outputDirectory + "/model.autogenerated.ts",
131
+ `${MODEL_PRE}
128
132
  ${models}
129
133
  ${MODEL_POST}`,
130
- { flag: 'w' });
131
- }
134
+ { flag: 'w' });
135
+ }
136
+
137
+ retrieveParameters(swaggerMethodInfo: SwaggerMethod) {
138
+ if (swaggerMethodInfo.requestBody != null) {
139
+ return `request: Models.${swaggerMethodInfo.requestBody.content[contentType].schema.$ref.replace('#/components/schemas/', '')}`
140
+ }
141
+
142
+ let parameters = ``;
143
+ swaggerMethodInfo.parameters.filter(x => x.in == 'query').forEach(parameter => {
144
+ if (parameter.schema.$ref != null) {
145
+ parameters += `${this.toFirstLetterLowercase(parameter.name)}: Models.${parameter.schema.$ref.replace('#/components/schemas/', '')}, `;
146
+ } else {
147
+ const nativeType = this.getNativeType(parameter.schema);
148
+ const nullable = !parameter.required ? '?' : '';
149
+ parameters += `${this.toFirstLetterLowercase(parameter.name)}${nullable}: ${nativeType}, `;
150
+ }
151
+ });
152
+
153
+ if (parameters.length > 2)
154
+ parameters = parameters.substring(0, parameters.length - 2);
155
+
156
+ return parameters;
157
+ }
158
+
159
+ retrieveQueryParameters(swaggerMethodInfo: SwaggerMethod) {
160
+ if (swaggerMethodInfo.requestBody != null) return ``;
161
+
162
+ let filteredParameters = swaggerMethodInfo.parameters.filter(x => x.in == 'query');
163
+ if (filteredParameters.length == 0) return ``;
164
+
165
+ let parameters = `?`;
166
+ filteredParameters.forEach(parameter => {
167
+ if (parameter.schema.$ref != null) {
168
+ if (this.isEnum(parameter.schema.$ref) != null) {
169
+ parameters += `${this.toFirstLetterLowercase(parameter.name)}=\${` + this.toFirstLetterLowercase(parameter.name) + `}&`;;
170
+ } else {
171
+ throw new Error("retrieveQueryParameters unmanaged parameter.schema.$ref");
172
+ }
173
+ } else {
174
+ parameters += `${this.toFirstLetterLowercase(parameter.name)}=\${` + this.toFirstLetterLowercase(parameter.name) + `}&`;
175
+ }
176
+ });
177
+
178
+ if (parameters.length > 1)
179
+ parameters = parameters.substring(0, parameters.length - 1);
180
+
181
+ return parameters;
182
+ }
183
+
184
+ retrieveReturnType(swaggerMethodInfo: SwaggerMethod) {
185
+ if (swaggerMethodInfo.responses[200] == null)
186
+ return 'void';
187
+
188
+ if (swaggerMethodInfo.responses[200].content[contentType].schema.$ref != null)
189
+ return `Models.${this.getObjectName(swaggerMethodInfo.responses[200]?.content[contentType].schema.$ref)}`;
190
+
191
+ if (swaggerMethodInfo.responses[200]?.content[contentType].schema.type != null)
192
+ return this.getNativeType(swaggerMethodInfo.responses[200]?.content[contentType].schema);
193
+
194
+ console.error("unmanaged swaggerMethodInfo", swaggerMethodInfo);
195
+ throw new Error("unmanaged swaggerMethodInfo");
196
+ }
197
+
198
+ retrieveType(swaggerComponentProperty: SwaggerComponentProperty) {
199
+ if (swaggerComponentProperty.$ref != null)
200
+ return swaggerComponentProperty.$ref.replace('#/components/schemas/', '');
201
+
202
+ if (swaggerComponentProperty.type != null && swaggerComponentProperty.type == 'array')
203
+ if (swaggerComponentProperty.items.$ref != null)
204
+ return `${this.getObjectName(swaggerComponentProperty.items.$ref)}[]`;
205
+ else
206
+ return this.getNativeType(swaggerComponentProperty);
207
+
208
+ if (swaggerComponentProperty.type != null)
209
+ return this.getNativeType(swaggerComponentProperty);
210
+
211
+ if (swaggerComponentProperty.type == null)
212
+ return '';
213
+
214
+ console.error("unmanaged swaggerMethodInfo", swaggerComponentProperty);
215
+ throw new Error("unmanaged swaggerMethodInfo");
216
+ }
217
+
218
+ parametrizeObject(objectName: string) {
219
+ let component = this._swagger.components.schemas[this.getObjectName(objectName)];
220
+
221
+ if (component == null || component.properties == null) return ``;
132
222
 
133
- retrieveParameters(swaggerMethodInfo: SwaggerMethod) {
134
- if (swaggerMethodInfo.requestBody != null) {
135
- return `request: Models.${swaggerMethodInfo.requestBody.content[contentType].schema.$ref.replace('#/components/schemas/', '')}`
136
- }
137
-
138
- let parameters = ``;
139
- swaggerMethodInfo.parameters.filter(x => x.in == 'query').forEach(parameter => {
140
- if (parameter.schema.$ref != null) {
141
- parameters += `${this.toFirstLetterLowercase(parameter.name)}: Models.${parameter.schema.$ref.replace('#/components/schemas/', '')}, `;
142
- } else {
143
- parameters += `${this.toFirstLetterLowercase(parameter.name)}: ${this.getNativeType(parameter.schema)}, `;
144
- }
145
- });
146
-
147
- if (parameters.length > 2)
148
- parameters = parameters.substring(0, parameters.length - 2);
149
-
150
- return parameters;
151
- }
152
-
153
- retrieveQueryParameters(swaggerMethodInfo: SwaggerMethod) {
154
- if (swaggerMethodInfo.requestBody != null) return ``;
155
-
156
- let filteredParameters = swaggerMethodInfo.parameters.filter(x => x.in == 'query');
157
- if (filteredParameters.length == 0) return ``;
158
-
159
- let parameters = `?`;
160
- filteredParameters.forEach(parameter => {
161
- if (parameter.schema.$ref != null) {
162
- this.parametrizeObject(parameter.schema.$ref)
163
- //parameters += `${parameter.name}: ${parameter.schema.$ref.replace('#/components/schemas/', '')}&`;
164
- } else {
165
- parameters += `${this.toFirstLetterLowercase(parameter.name)}=\${` + this.toFirstLetterLowercase(parameter.name) + `}&`;
166
- }
167
- });
168
-
169
- if (parameters.length > 1)
170
- parameters = parameters.substring(0, parameters.length - 1);
171
-
172
- return parameters;
173
- }
174
-
175
- retrieveReturnType(swaggerMethodInfo: SwaggerMethod) {
176
- if (swaggerMethodInfo.responses[200] == null)
177
- return 'void';
178
-
179
- if (swaggerMethodInfo.responses[200].content[contentType].schema.$ref != null)
180
- return `Models.${swaggerMethodInfo.responses[200]?.content[contentType].schema.$ref.replace('#/components/schemas/', '')}`;
181
-
182
- if (swaggerMethodInfo.responses[200]?.content[contentType].schema.type != null)
183
- return this.getNativeType(swaggerMethodInfo.responses[200]?.content[contentType].schema);
184
-
185
- console.error("unmanaged swaggerMethodInfo", swaggerMethodInfo);
186
- throw new Error("unmanaged swaggerMethodInfo");
187
- }
188
-
189
- retrieveType(swaggerComponentProperty: SwaggerComponentProperty) {
190
- if (swaggerComponentProperty.$ref != null)
191
- return swaggerComponentProperty.$ref.replace('#/components/schemas/', '');
192
-
193
- if (swaggerComponentProperty.type != null && swaggerComponentProperty.type == 'array')
194
- if (swaggerComponentProperty.items.$ref != null)
195
- return `${swaggerComponentProperty.items.$ref.replace('#/components/schemas/', '')}[]`;
196
- else
197
- return this.getNativeType(swaggerComponentProperty);
198
-
199
- if (swaggerComponentProperty.type != null)
200
- return this.getNativeType(swaggerComponentProperty);
201
-
202
- if (swaggerComponentProperty.type == null)
203
- return '';
204
-
205
- console.error("unmanaged swaggerMethodInfo", swaggerComponentProperty);
206
- throw new Error("unmanaged swaggerMethodInfo");
207
- }
208
-
209
- parametrizeObject(objectName: string) {
210
- let component = this._swagger.components.schemas[objectName.replace('#/components/schemas/', '')];
211
- if (component == null || component.properties == null) return ``;
212
-
213
- console.debug(component.properties);
214
-
215
- return ``;
216
- }
217
-
218
- retrieveObjectContent(name: string, swaggerComponent: SwaggerComponent) {
219
- if (swaggerComponent.type == 'object')
220
- return this.retrieveObjectProperties(swaggerComponent);
221
- else if (swaggerComponent.type == 'integer')
222
- return this.retrieveEnumValues(name, swaggerComponent);
223
- }
224
-
225
- retrieveObjectProperties(swaggerCopmponent: SwaggerComponent) {
226
- if (swaggerCopmponent.properties == null) return ``;
227
-
228
- // console.debug(`\t\t${Object.getOwnPropertyNames(swaggerCopmponent.properties).length}`);
229
-
230
- let properties = ``;
231
- for (let index = 0; index < Object.getOwnPropertyNames(swaggerCopmponent.properties).length; index++) {
232
- const propertyName = Object.getOwnPropertyNames(swaggerCopmponent.properties)[index];
233
- properties +=
234
- `
223
+ console.debug(component.properties);
224
+
225
+ return ``;
226
+ }
227
+
228
+ isEnum(objectName: string) {
229
+ let component = this._swagger.components.schemas[this.getObjectName(objectName)];
230
+ return component.enum != null;
231
+ }
232
+
233
+ getObjectName(ref: string) {
234
+ return ref.replace('#/components/schemas/', '');
235
+ }
236
+
237
+ retrieveObjectContent(name: string, swaggerComponent: SwaggerComponent) {
238
+ if (swaggerComponent.type == 'object')
239
+ return this.retrieveObjectProperties(swaggerComponent);
240
+ else if (swaggerComponent.type == 'integer')
241
+ return this.retrieveEnumValues(name, swaggerComponent);
242
+ }
243
+
244
+ retrieveObjectProperties(swaggerCopmponent: SwaggerComponent) {
245
+ if (swaggerCopmponent.properties == null) return ``;
246
+
247
+ // console.debug(`\t\t${Object.getOwnPropertyNames(swaggerCopmponent.properties).length}`);
248
+
249
+ let properties = ``;
250
+ for (let index = 0; index < Object.getOwnPropertyNames(swaggerCopmponent.properties).length; index++) {
251
+ const propertyName = Object.getOwnPropertyNames(swaggerCopmponent.properties)[index];
252
+ properties +=
253
+ `
235
254
  ${propertyName}: ${this.retrieveType(swaggerCopmponent.properties[propertyName])} | undefined;`
236
- }
255
+ }
237
256
 
238
- return properties;
239
- }
257
+ return properties;
258
+ }
240
259
 
241
- retrieveEnumValues(name: string, swaggerCopmponent: SwaggerComponent) {
242
- if (swaggerCopmponent.enum == null) return ``;
260
+ retrieveEnumValues(name: string, swaggerCopmponent: SwaggerComponent) {
261
+ if (swaggerCopmponent.enum == null) return ``;
243
262
 
244
- let properties = ``;
245
- for (let index = 0; index < swaggerCopmponent.enum.length; index++) {
246
- const name = swaggerCopmponent.enum[index].split('-')[0].trim();
247
- const value = swaggerCopmponent.enum[index].split('-')[1].trim();
248
- properties +=
249
- `
263
+ let properties = ``;
264
+ for (let index = 0; index < swaggerCopmponent.enum.length; index++) {
265
+ const name = swaggerCopmponent.enum[index].split('-')[0].trim();
266
+ const value = swaggerCopmponent.enum[index].split('-')[1].trim();
267
+ properties +=
268
+ `
250
269
  ${name} = ${value},`;
251
- }
252
-
253
- return properties;
254
- }
255
-
256
- retrieveNestedObjects(usedTypes: string[]) {
257
- for (let i = 0; i < usedTypes.length; i++) {
258
- const swaggerCopmponent = this._swagger.components.schemas[usedTypes[i]];
259
- // const name = usedTypes[i]
260
- // const modelName = <string>Object.getOwnPropertyNames(this._swagger.components.schemas)[name];
261
- this.retrieveNestedObjects2(swaggerCopmponent, usedTypes);
262
- }
263
- }
264
-
265
- retrieveNestedObjects2(swaggerComponent: SwaggerComponent, usedTypes: string[]) {
266
- if (!swaggerComponent.properties) return;
267
-
268
- for (let j = 0; j < Object.getOwnPropertyNames(swaggerComponent.properties).length; j++) {
269
- const propertyName = Object.getOwnPropertyNames(swaggerComponent.properties)[j];
270
- let nestedUsedType = '';
271
- if (swaggerComponent.properties[propertyName].$ref != null) {
272
- nestedUsedType = swaggerComponent.properties[propertyName].$ref.replace('#/components/schemas/', '');
273
- } else if (swaggerComponent.properties[propertyName].type == 'array' && swaggerComponent.properties[propertyName].items.$ref != null) {
274
- nestedUsedType = swaggerComponent.properties[propertyName].items.$ref.replace('#/components/schemas/', '');
275
- }
276
-
277
- if (nestedUsedType != '' && usedTypes.findIndex(x => x == nestedUsedType) == -1) {
278
- usedTypes.push(nestedUsedType);
279
- let nested = this._swagger.components.schemas[nestedUsedType];
280
- this.retrieveNestedObjects2(nested, usedTypes);
281
- }
282
- }
283
- }
284
-
285
- getNativeType(schema: SwaggerSchema): string {
286
- if (schema.type == 'integer') return 'number';
287
- if (schema.type == 'string' && schema.format == null) return 'string';
288
- if (schema.type == 'string' && schema.format == 'date-time') return 'moment.Moment';
289
- if (schema.type == 'string' && schema.format == 'uuid') return 'string';
290
- if (schema.type == 'number') return 'number';
291
- if (schema.type == 'array') return '[]';
292
- if (schema.type == 'boolean') return 'boolean';
293
- if (schema.type == 'object') return 'any';
294
-
295
- console.error("unmanaged schema type", schema);
296
- throw new Error("unmanaged schema");
297
- }
298
-
299
- toFirstLetterLowercase(value: string) {
300
- return value.charAt(0).toLowerCase() + value.slice(1);
301
- }
270
+ }
271
+
272
+ return properties;
273
+ }
274
+
275
+ retrieveNestedObjects(usedTypes: string[]) {
276
+ for (let i = 0; i < usedTypes.length; i++) {
277
+ const swaggerCopmponent = this._swagger.components.schemas[usedTypes[i]];
278
+ // const name = usedTypes[i]
279
+ // const modelName = <string>Object.getOwnPropertyNames(this._swagger.components.schemas)[name];
280
+ this.retrieveNestedObjects2(swaggerCopmponent, usedTypes);
281
+ }
282
+ }
283
+
284
+ retrieveNestedObjects2(swaggerComponent: SwaggerComponent, usedTypes: string[]) {
285
+ if (!swaggerComponent.properties) return;
286
+
287
+ for (let j = 0; j < Object.getOwnPropertyNames(swaggerComponent.properties).length; j++) {
288
+ const propertyName = Object.getOwnPropertyNames(swaggerComponent.properties)[j];
289
+ let nestedUsedType = '';
290
+ if (swaggerComponent.properties[propertyName].$ref != null) {
291
+ nestedUsedType = swaggerComponent.properties[propertyName].$ref.replace('#/components/schemas/', '');
292
+ } else if (swaggerComponent.properties[propertyName].type == 'array' && swaggerComponent.properties[propertyName].items.$ref != null) {
293
+ nestedUsedType = swaggerComponent.properties[propertyName].items.$ref.replace('#/components/schemas/', '');
294
+ }
295
+
296
+ if (nestedUsedType != '' && usedTypes.findIndex(x => x == nestedUsedType) == -1) {
297
+ usedTypes.push(nestedUsedType);
298
+ let nested = this._swagger.components.schemas[nestedUsedType];
299
+ this.retrieveNestedObjects2(nested, usedTypes);
300
+ }
301
+ }
302
+ }
303
+
304
+ getNativeType(schema: SwaggerSchema): string {
305
+ if (schema.type == 'integer') return 'number';
306
+ if (schema.type == 'string' && schema.format == null) return 'string';
307
+ if (schema.type == 'string' && schema.format == 'date-time') return 'moment.Moment';
308
+ if (schema.type == 'string' && schema.format == 'uuid') return 'string';
309
+ if (schema.type == 'number') return 'number';
310
+ if (schema.type == 'array') return '[]';
311
+ if (schema.type == 'boolean') return 'boolean';
312
+ if (schema.type == 'object') return 'any';
313
+
314
+ console.error("unmanaged schema type", schema);
315
+ throw new Error("unmanaged schema");
316
+ }
317
+
318
+ toFirstLetterLowercase(value: string) {
319
+ return value.charAt(0).toLowerCase() + value.slice(1);
320
+ }
302
321
  }