@devlearning/swagger-generator 1.0.10 → 1.0.12

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.
@@ -22,7 +22,7 @@ export abstract class ApiAutogeneratedService {
22
22
  protected abstract _handleError(error: any, obs: any): Observable<never>;
23
23
 
24
24
 
25
- public catalog_Product_Read(idProduct?: number): Observable<Models.Product> {
25
+ public catalogProductRead(idProduct?: number): Observable<Models.Product> {
26
26
  let idProductParam: string = idProduct != null && idProduct != undefined ? encodeURIComponent('' + idProduct) : '';
27
27
  return this._http.get<Models.Product>(`${this._baseUrl}/catalog/Product/Read?idProduct=${idProductParam}`, httpOptions)
28
28
  .pipe(
@@ -31,7 +31,7 @@ export abstract class ApiAutogeneratedService {
31
31
  );
32
32
  }
33
33
 
34
- public catalog_Product_List(): Observable<Models.Product[]> {
34
+ public catalogProductList(): Observable<Models.Product[]> {
35
35
  return this._http.get<Models.Product[]>(`${this._baseUrl}/catalog/Product/List`, httpOptions)
36
36
  .pipe(
37
37
  map(x => this._handleResponse(x)),
@@ -39,7 +39,52 @@ export abstract class ApiAutogeneratedService {
39
39
  );
40
40
  }
41
41
 
42
- public tenant_Tenant(): Observable<string> {
42
+ public identityToken(request: Models.AuthTokenCommand): Observable<Models.AuthenticationToken> {
43
+ let wrappedRequest = this._handleRequest(request);
44
+ return this._http.post<Models.AuthenticationToken>(`${this._baseUrl}/identity/token`, wrappedRequest, httpOptions)
45
+ .pipe(
46
+ map(x => this._handleResponse(x)),
47
+ catchError((err, obs) => this._handleError(err, <Observable<any>>obs))
48
+ );
49
+ }
50
+
51
+ public identityRefreshToken(request: Models.AuthRefreshTokenCommand): Observable<Models.AuthenticationToken> {
52
+ let wrappedRequest = this._handleRequest(request);
53
+ return this._http.post<Models.AuthenticationToken>(`${this._baseUrl}/identity/refreshToken`, wrappedRequest, httpOptions)
54
+ .pipe(
55
+ map(x => this._handleResponse(x)),
56
+ catchError((err, obs) => this._handleError(err, <Observable<any>>obs))
57
+ );
58
+ }
59
+
60
+ public identityGenerateResetPasswordCode(request: Models.AuthGenerateResetPasswordCodeCommand): Observable<Models.Result> {
61
+ let wrappedRequest = this._handleRequest(request);
62
+ return this._http.post<Models.Result>(`${this._baseUrl}/identity/generateResetPasswordCode`, wrappedRequest, httpOptions)
63
+ .pipe(
64
+ map(x => this._handleResponse(x)),
65
+ catchError((err, obs) => this._handleError(err, <Observable<any>>obs))
66
+ );
67
+ }
68
+
69
+ public identityResetPassword(request: Models.AuthResetPasswordCommand): Observable<Models.Result> {
70
+ let wrappedRequest = this._handleRequest(request);
71
+ return this._http.post<Models.Result>(`${this._baseUrl}/identity/resetPassword`, wrappedRequest, httpOptions)
72
+ .pipe(
73
+ map(x => this._handleResponse(x)),
74
+ catchError((err, obs) => this._handleError(err, <Observable<any>>obs))
75
+ );
76
+ }
77
+
78
+ public identityVerifyResetPasswordCode(request: Models.AuthVerifyResetPasswordCodeCommand): Observable<Models.Result> {
79
+ let wrappedRequest = this._handleRequest(request);
80
+ return this._http.post<Models.Result>(`${this._baseUrl}/identity/verifyResetPasswordCode`, wrappedRequest, httpOptions)
81
+ .pipe(
82
+ map(x => this._handleResponse(x)),
83
+ catchError((err, obs) => this._handleError(err, <Observable<any>>obs))
84
+ );
85
+ }
86
+
87
+ public tenantTenant(): Observable<string> {
43
88
  return this._http.get<string>(`${this._baseUrl}/tenant/Tenant`, httpOptions)
44
89
  .pipe(
45
90
  map(x => this._handleResponse(x)),
@@ -1,10 +1,69 @@
1
1
  import * as moment from 'moment';
2
2
 
3
3
 
4
+ export class ApplicationException {
5
+ message?: string;
6
+ stackTrace?: string;
7
+ }
8
+
9
+ export class AuthGenerateResetPasswordCodeCommand {
10
+ usernameOrEmail?: string;
11
+ host?: string;
12
+ urlResetPassword?: string;
13
+ }
14
+
15
+ export class AuthRefreshTokenCommand {
16
+ username: string;
17
+ refreshToken: string;
18
+ ipAddress?: string;
19
+ }
20
+
21
+ export class AuthResetPasswordCommand {
22
+ usernameOrEmail: string;
23
+ verificationCode: string;
24
+ password: string;
25
+ confirmPassword: string;
26
+ }
27
+
28
+ export class AuthTokenCommand {
29
+ username: string;
30
+ password: string;
31
+ ipAddress?: string;
32
+ }
33
+
34
+ export class AuthVerifyResetPasswordCodeCommand {
35
+ usernameOrEmail?: string;
36
+ verificationCode?: string;
37
+ }
38
+
39
+ export class AuthenticationToken {
40
+ idAuthUser: string;
41
+ username: string;
42
+ accessToken: string;
43
+ expiresIn: number;
44
+ refreshToken: string;
45
+ refreshTokenExpiresIn: number;
46
+ }
47
+
48
+ export class ProblemDetails {
49
+ type?: string;
50
+ title?: string;
51
+ status?: number;
52
+ detail?: string;
53
+ instance?: string;
54
+ }
55
+
4
56
  export class Product {
5
- idProduct?: string;
57
+ idProduct: string;
6
58
  name?: string;
7
- quantity?: number;
59
+ quantity: number;
60
+ }
61
+
62
+ export class Result {
63
+ isSuccess: boolean;
64
+ message?: string;
65
+ stacktrace?: string;
66
+ exception: ApplicationException;
8
67
  }
9
68
 
10
69
 
package/dist/generator.js CHANGED
@@ -54,7 +54,7 @@ export class Generator {
54
54
  url: apiName.replace('{version}', '1'),
55
55
  method: apiMethod,
56
56
  parameters: this.computeParameters(apiName, apiSwaggerMethod),
57
- returnType: this.computeReturnType(apiSwaggerMethod),
57
+ returnType: this.computeResponseType(apiSwaggerMethod),
58
58
  swaggerMethodKey: apiSwaggerMethodKey,
59
59
  swaggerMethod: apiSwaggerMethod,
60
60
  haveRequest: apiSwaggerMethod.requestBody != null,
@@ -67,31 +67,38 @@ export class Generator {
67
67
  this._barModels.start(Object.getOwnPropertyNames(this._swagger.paths).length, 0);
68
68
  let usedTypes = [];
69
69
  let usedMultiPart = [];
70
- for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
71
- const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
72
- const apiSwaggerMethodKey = this._swagger.paths[apiName];
73
- const apiMethod = Object.getOwnPropertyNames(apiSwaggerMethodKey)[0];
74
- const apiSwaggerMethod = apiSwaggerMethodKey[apiMethod];
75
- // if (apiName == "/api/v{version}/TicketFile/Create") {
76
- // debugger
77
- // }
78
- let parametersRefType = apiSwaggerMethod.parameters?.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''));
79
- if (parametersRefType) {
80
- usedTypes = usedTypes.concat(parametersRefType);
81
- this._barModels.update(index, { message: `Models parsing... ${usedTypes}` });
82
- if (apiSwaggerMethod.responses[200].content[contentTypeApplicationJson]?.schema.$ref != null) {
83
- usedTypes.push(apiSwaggerMethod.responses[200].content[contentTypeApplicationJson].schema.$ref.replace('#/components/schemas/', ''));
84
- }
85
- if (apiSwaggerMethod.requestBody?.content[contentTypeApplicationJson]?.schema?.$ref) {
86
- usedTypes.push(apiSwaggerMethod.requestBody?.content[contentTypeApplicationJson]?.schema?.$ref.replace('#/components/schemas/', ''));
87
- }
88
- if (apiSwaggerMethod.requestBody?.content[contentTypeMultipartFormData]?.schema != null) {
89
- usedMultiPart.push(apiName);
90
- }
70
+ // for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
71
+ // const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
72
+ // const apiSwaggerMethodKey = this._swagger.paths[apiName];
73
+ // const apiMethod = Object.getOwnPropertyNames(apiSwaggerMethodKey)[0];
74
+ // const apiSwaggerMethod = apiSwaggerMethodKey[apiMethod];
75
+ // const parametersRefType = apiSwaggerMethod.parameters?.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''));
76
+ // if (parametersRefType) {
77
+ // usedTypes = usedTypes.concat(parametersRefType);
78
+ // }
79
+ // const responseRefType = this.computeRequestBodyType(apiSwaggerMethod!);
80
+ // if (responseRefType && !responseRefType.isVoid && !responseRefType.isNativeType) {
81
+ // usedTypes = usedTypes.concat(responseRefType.typeName);
82
+ // }
83
+ // const contentRefType = this.computeResponseType(apiSwaggerMethod!);
84
+ // if (contentRefType && !contentRefType.isVoid && !contentRefType.isNativeType) {
85
+ // usedTypes = usedTypes.concat(contentRefType.typeName);
86
+ // }
87
+ // }
88
+ for (let index = 0; index < this._apis.length; index++) {
89
+ if (this._apis[index].returnType && this._apis[index].returnType.isTypeReference) {
90
+ usedTypes.push(this._apis[index].returnType);
91
+ }
92
+ if (this._apis[index].parameters) {
93
+ this._apis[index].parameters.forEach(parameter => {
94
+ if (parameter.isTypeReference) {
95
+ usedTypes.push(parameter);
96
+ }
97
+ });
91
98
  }
92
99
  }
100
+ usedTypes = [...new Set(usedTypes.map(item => item))]; // distinct
93
101
  this.retrieveNestedObjects(usedTypes);
94
- usedTypes = [...new Set(usedTypes.map(item => item))]; // [ 'A', 'B']
95
102
  let models = ``;
96
103
  if (this._swagger.components != null
97
104
  && this._swagger.components != undefined
@@ -99,11 +106,6 @@ export class Generator {
99
106
  && this._swagger.components.schemas != undefined) {
100
107
  for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.components.schemas).length; index++) {
101
108
  const modelName = Object.getOwnPropertyNames(this._swagger.components.schemas)[index];
102
- if (usedTypes.indexOf(modelName) < 0) {
103
- // console.debug(`\tModel SKIP - ${modelName}`);
104
- continue;
105
- }
106
- ;
107
109
  const swaggerComponent = this._swagger.components.schemas[modelName];
108
110
  // console.debug(`\tModel - ${modelName}`);
109
111
  this._models.push({
@@ -158,68 +160,99 @@ export class Generator {
158
160
  if (!swaggerMethod || swaggerMethod == null)
159
161
  return [];
160
162
  let parameters = [];
161
- if (swaggerMethod.requestBody != null && swaggerMethod.requestBody.content[contentTypeMultipartFormData] != null) {
162
- var modelName = this.getApiNameNormalized(apiName);
163
- parameters.push({
164
- name: 'request',
165
- typeName: modelName,
166
- nullable: false,
167
- isQuery: false,
168
- isEnum: false,
169
- isNativeType: false,
170
- });
171
- }
172
- else {
173
- if (swaggerMethod.requestBody != null) {
163
+ try {
164
+ if (swaggerMethod.requestBody != null && swaggerMethod.requestBody.content[contentTypeMultipartFormData] != null) {
165
+ var modelName = this.getApiNameNormalized(apiName);
174
166
  parameters.push({
175
167
  name: 'request',
176
- typeName: swaggerMethod.requestBody.content[contentTypeApplicationJson].schema.$ref.replace('#/components/schemas/', ''),
168
+ typeName: modelName,
177
169
  nullable: false,
178
170
  isQuery: false,
179
171
  isEnum: false,
172
+ isTypeReference: true,
180
173
  isNativeType: false,
174
+ isArray: false,
175
+ isVoid: false,
181
176
  });
182
177
  }
183
178
  else {
184
- swaggerMethod.parameters?.filter(x => x.in == 'query').forEach(parameter => {
185
- if (parameter.schema.$ref != null) {
186
- parameters.push({
187
- name: this.toFirstLetterLowercase(parameter.name),
188
- typeName: parameter.schema.$ref.replace('#/components/schemas/', ''),
189
- nullable: !parameter.required,
190
- swaggerParameter: parameter,
191
- isQuery: true,
192
- isEnum: this.isEnum(parameter.schema.$ref),
193
- isNativeType: false,
194
- });
195
- }
196
- else {
197
- parameters.push({
198
- name: this.toFirstLetterLowercase(parameter.name),
199
- typeName: this.getNativeType(parameter.schema),
200
- nullable: !parameter.required,
201
- swaggerParameter: parameter,
202
- isQuery: true,
203
- isEnum: false,
204
- isNativeType: true,
205
- });
206
- }
207
- });
179
+ if (swaggerMethod.requestBody != null) {
180
+ parameters.push({
181
+ name: 'request',
182
+ typeName: swaggerMethod.requestBody.content[contentTypeApplicationJson].schema.$ref.replace('#/components/schemas/', ''),
183
+ nullable: false,
184
+ isQuery: false,
185
+ isEnum: false,
186
+ isTypeReference: true,
187
+ isNativeType: false,
188
+ isArray: false,
189
+ isVoid: false,
190
+ });
191
+ }
192
+ else {
193
+ swaggerMethod.parameters?.filter(x => x.in == 'query').forEach(parameter => {
194
+ if (parameter.schema.$ref != null) {
195
+ parameters.push({
196
+ name: this.toFirstLetterLowercase(parameter.name),
197
+ typeName: parameter.schema.$ref.replace('#/components/schemas/', ''),
198
+ nullable: !parameter.required,
199
+ swaggerParameter: parameter,
200
+ isQuery: true,
201
+ isEnum: this.isEnum(parameter.schema.$ref),
202
+ isTypeReference: false,
203
+ isNativeType: false,
204
+ isArray: false,
205
+ isVoid: false,
206
+ });
207
+ }
208
+ else {
209
+ parameters.push({
210
+ name: this.toFirstLetterLowercase(parameter.name),
211
+ typeName: this.getNativeType(parameter.schema),
212
+ nullable: !parameter.required,
213
+ swaggerParameter: parameter,
214
+ isQuery: true,
215
+ isEnum: false,
216
+ isTypeReference: false,
217
+ isNativeType: true,
218
+ isArray: false,
219
+ isVoid: false,
220
+ });
221
+ }
222
+ });
223
+ }
208
224
  }
209
225
  }
226
+ catch (error) {
227
+ console.error(`\t\tError in ${apiName} - Attenzione forse hai dimenticato IActionResult e non hai tipizzato il tipo restituito dal servizio`);
228
+ }
210
229
  return parameters;
211
230
  }
212
- computeReturnType(swaggerMethod) {
231
+ computeResponseType(swaggerMethod) {
213
232
  if (swaggerMethod.responses[200] == null) {
214
233
  return {
215
234
  typeName: 'void',
216
235
  nullable: false,
236
+ isVoid: true,
237
+ };
238
+ }
239
+ return this.computeSwaggerContentType(swaggerMethod.responses[200].content[contentTypeApplicationJson]);
240
+ }
241
+ computeRequestBodyType(swaggerMethod) {
242
+ if (swaggerMethod.requestBody == null) {
243
+ return {
244
+ typeName: 'void',
245
+ nullable: false,
246
+ isVoid: true,
217
247
  };
218
248
  }
249
+ return this.computeSwaggerContentType(swaggerMethod.requestBody.content[contentTypeApplicationJson]);
250
+ }
251
+ computeSwaggerContentType(swaggerContent) {
219
252
  try {
220
- if (swaggerMethod.responses[200].content[contentTypeApplicationJson].schema.$ref != null)
253
+ if (swaggerContent.schema.$ref != null)
221
254
  return {
222
- typeName: this.getObjectName(swaggerMethod.responses[200]?.content[contentTypeApplicationJson].schema.$ref),
255
+ typeName: this.getObjectName(swaggerContent.schema.$ref),
223
256
  nullable: false,
224
257
  isNativeType: false,
225
258
  };
@@ -229,20 +262,21 @@ export class Generator {
229
262
  console.error(`%c${errorMessage}`, 'color: red');
230
263
  throw new Error(errorMessage);
231
264
  }
232
- if (swaggerMethod.responses[200]?.content[contentTypeApplicationJson].schema.type != null) {
233
- let schema = swaggerMethod.responses[200]?.content[contentTypeApplicationJson].schema;
265
+ if (swaggerContent.schema.type != null) {
266
+ let schema = swaggerContent.schema;
234
267
  if (schema.type == 'array') {
235
268
  if (schema.items.$ref != null) {
236
269
  return {
237
- typeName: `${this.getObjectName(schema.items.$ref)}[]`,
270
+ typeName: `${this.getObjectName(schema.items.$ref)}`,
238
271
  nullable: false,
239
272
  isNativeType: false,
273
+ isArray: true,
240
274
  };
241
275
  }
242
276
  }
243
277
  else {
244
278
  return {
245
- typeName: this.getNativeType(swaggerMethod.responses[200]?.content[contentTypeApplicationJson].schema),
279
+ typeName: this.getNativeType(swaggerContent.schema),
246
280
  nullable: false,
247
281
  isNativeType: true,
248
282
  };
@@ -252,7 +286,7 @@ export class Generator {
252
286
  }
253
287
  else {
254
288
  return {
255
- typeName: this.getNativeType(swaggerMethod.responses[200]?.content[contentTypeApplicationJson].schema),
289
+ typeName: this.getNativeType(swaggerContent.schema),
256
290
  nullable: false,
257
291
  isNativeType: true,
258
292
  };
@@ -262,33 +296,48 @@ export class Generator {
262
296
  if (swaggerComponentProperty.$ref != null)
263
297
  return {
264
298
  typeName: swaggerComponentProperty.$ref.replace('#/components/schemas/', ''),
299
+ isTypeReference: true,
265
300
  isNativeType: false,
266
- nullable: false,
301
+ nullable: swaggerComponentProperty.nullable ?? false,
302
+ isArray: false,
303
+ isVoid: false,
267
304
  };
268
305
  if (swaggerComponentProperty.type != null && swaggerComponentProperty.type == 'array')
269
306
  if (swaggerComponentProperty.items.$ref != null)
270
307
  return {
271
- typeName: `${this.getObjectName(swaggerComponentProperty.items.$ref)}[]`,
308
+ typeName: `${this.getObjectName(swaggerComponentProperty.items.$ref)}`,
309
+ isTypeReference: true,
272
310
  isNativeType: false,
273
- nullable: false,
311
+ nullable: swaggerComponentProperty.nullable ?? false,
312
+ isArray: true,
313
+ isVoid: false,
274
314
  };
275
315
  else
276
316
  return {
277
317
  typeName: this.getNativeType(swaggerComponentProperty),
278
- isNativeType: false,
279
- nullable: true,
318
+ isTypeReference: false,
319
+ isNativeType: true,
320
+ nullable: swaggerComponentProperty.nullable ?? false,
321
+ isArray: false,
322
+ isVoid: false,
280
323
  };
281
324
  if (swaggerComponentProperty.type != null)
282
325
  return {
283
326
  typeName: this.getNativeType(swaggerComponentProperty),
284
- isNativeType: false,
285
- nullable: true,
327
+ isTypeReference: false,
328
+ isNativeType: true,
329
+ nullable: swaggerComponentProperty.nullable ?? false,
330
+ isArray: false,
331
+ isVoid: false,
286
332
  };
287
333
  if (swaggerComponentProperty.type == null)
288
334
  return {
289
- typeName: 'null',
335
+ typeName: 'void',
336
+ isTypeReference: false,
290
337
  isNativeType: false,
291
- nullable: true,
338
+ nullable: swaggerComponentProperty.nullable ?? false,
339
+ isArray: false,
340
+ isVoid: true,
292
341
  };
293
342
  console.error("unmanaged swaggerMethodInfo", swaggerComponentProperty);
294
343
  throw new Error("unmanaged swaggerMethodInfo");
@@ -320,7 +369,7 @@ export class Generator {
320
369
  properties.push({
321
370
  ...type,
322
371
  name: propertyName,
323
- nullable: true,
372
+ nullable: type.nullable,
324
373
  });
325
374
  }
326
375
  return properties;
@@ -350,30 +399,41 @@ export class Generator {
350
399
  }
351
400
  retrieveNestedObjects(usedTypes) {
352
401
  for (let i = 0; i < usedTypes.length; i++) {
353
- const swaggerCopmponent = this._swagger.components.schemas[usedTypes[i]];
354
- // const name = usedTypes[i]
355
- // const modelName = <string>Object.getOwnPropertyNames(this._swagger.components.schemas)[name];
402
+ const swaggerCopmponent = this._swagger.components.schemas[usedTypes[i].typeName];
356
403
  this.retrieveNestedObjectsRecursive(swaggerCopmponent, usedTypes);
357
404
  }
358
405
  }
359
406
  retrieveNestedObjectsRecursive(swaggerComponent, usedTypes) {
360
- if (!swaggerComponent.properties)
361
- return;
362
- for (let j = 0; j < Object.getOwnPropertyNames(swaggerComponent.properties).length; j++) {
363
- const propertyName = Object.getOwnPropertyNames(swaggerComponent.properties)[j];
364
- let nestedUsedType = '';
365
- if (swaggerComponent.properties[propertyName].$ref != null) {
366
- nestedUsedType = swaggerComponent.properties[propertyName].$ref.replace('#/components/schemas/', '');
367
- }
368
- else if (swaggerComponent.properties[propertyName].type == 'array' && swaggerComponent.properties[propertyName].items.$ref != null) {
369
- nestedUsedType = swaggerComponent.properties[propertyName].items.$ref.replace('#/components/schemas/', '');
370
- }
371
- if (nestedUsedType != '' && usedTypes.findIndex(x => x == nestedUsedType) == -1) {
372
- usedTypes.push(nestedUsedType);
373
- let nested = this._swagger.components.schemas[nestedUsedType];
374
- this.retrieveNestedObjectsRecursive(nested, usedTypes);
407
+ try {
408
+ if (!swaggerComponent.properties)
409
+ return;
410
+ for (let j = 0; j < Object.getOwnPropertyNames(swaggerComponent.properties).length; j++) {
411
+ const propertyName = Object.getOwnPropertyNames(swaggerComponent.properties)[j];
412
+ let nestedUsedType = '';
413
+ if (swaggerComponent.properties[propertyName].$ref != null) {
414
+ nestedUsedType = swaggerComponent.properties[propertyName].$ref.replace('#/components/schemas/', '');
415
+ }
416
+ else if (swaggerComponent.properties[propertyName].type == 'array' && swaggerComponent.properties[propertyName].items.$ref != null) {
417
+ nestedUsedType = swaggerComponent.properties[propertyName].items.$ref.replace('#/components/schemas/', '');
418
+ }
419
+ if (nestedUsedType != '' && usedTypes.findIndex(x => x.typeName == nestedUsedType) == -1) {
420
+ let nested = this._swagger.components.schemas[nestedUsedType];
421
+ usedTypes.push({
422
+ typeName: nestedUsedType,
423
+ isTypeReference: nested.type == 'object',
424
+ isNativeType: nested.type != 'object',
425
+ nullable: false,
426
+ isArray: false,
427
+ isVoid: false,
428
+ //potrebbe essere un enum
429
+ });
430
+ this.retrieveNestedObjectsRecursive(nested, usedTypes);
431
+ }
375
432
  }
376
433
  }
434
+ catch (error) {
435
+ debugger;
436
+ }
377
437
  }
378
438
  getNativeType(schema) {
379
439
  let nativeType = 'n.d.';
@@ -46,7 +46,8 @@ export class ApiAngularWriter {
46
46
  }
47
47
  _returnType(api) {
48
48
  const prefixType = !api.returnType?.isNativeType ? 'Models.' : '';
49
- return api.returnType ? `${prefixType}${api.returnType.typeName}` : 'any';
49
+ const isArray = api.returnType?.isArray ? '[]' : '';
50
+ return api.returnType ? `${prefixType}${api.returnType.typeName}${isArray}` : 'any';
50
51
  }
51
52
  _queryParametersPreparation(api) {
52
53
  let queryParametersPreparation = '';
@@ -4,7 +4,10 @@ export class Utils {
4
4
  if (normalizedApiName.charAt(0) == '_') {
5
5
  normalizedApiName = normalizedApiName.slice(1);
6
6
  }
7
- return this.toFirstLetterLowercase(normalizedApiName);
7
+ normalizedApiName = this.toCamelCase(normalizedApiName);
8
+ normalizedApiName = this.toFirstLetterLowercase(normalizedApiName);
9
+ normalizedApiName = normalizedApiName.replaceAll('_', '');
10
+ return normalizedApiName;
8
11
  }
9
12
  static toFirstLetterLowercase(value) {
10
13
  return value.charAt(0).toLowerCase() + value.slice(1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devlearning/swagger-generator",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "Swagger generator apis and models for Angular and NextJS",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/src/generator.ts CHANGED
@@ -15,6 +15,7 @@ import { ModelNextJsWriter } from './generators-writers/nextjs/model-nextjs-writ
15
15
  import { ModelAngularWriter } from './generators-writers/angular/model-angular-writer.js';
16
16
  import { PropertyDto } from './models/property-dto.js';
17
17
  import { EnumValueDto } from './models/enum-value-dto.js';
18
+ import { SwaggerContent } from './models/swagger/swagger-content.js';
18
19
 
19
20
  const contentTypeApplicationJson = 'application/json';
20
21
  const contentTypeMultipartFormData = 'multipart/form-data';
@@ -79,7 +80,7 @@ export class Generator {
79
80
  url: apiName.replace('{version}', '1'),
80
81
  method: apiMethod,
81
82
  parameters: this.computeParameters(apiName, apiSwaggerMethod),
82
- returnType: this.computeReturnType(apiSwaggerMethod),
83
+ returnType: this.computeResponseType(apiSwaggerMethod),
83
84
  swaggerMethodKey: apiSwaggerMethodKey,
84
85
  swaggerMethod: apiSwaggerMethod,
85
86
  haveRequest: apiSwaggerMethod.requestBody != null,
@@ -92,41 +93,48 @@ export class Generator {
92
93
  computeModel() {
93
94
  this._barModels.start(Object.getOwnPropertyNames(this._swagger.paths).length, 0);
94
95
 
95
- let usedTypes: string[] = [];
96
+ let usedTypes: TypeDto[] = [];
96
97
  let usedMultiPart: string[] = [];
97
98
 
98
- for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
99
- const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
100
- const apiSwaggerMethodKey = this._swagger.paths[apiName];
101
- const apiMethod = Object.getOwnPropertyNames(apiSwaggerMethodKey)[0];
102
- const apiSwaggerMethod = apiSwaggerMethodKey[apiMethod];
103
-
104
- // if (apiName == "/api/v{version}/TicketFile/Create") {
105
- // debugger
106
- // }
107
-
108
- let parametersRefType = apiSwaggerMethod.parameters?.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''))
109
- if (parametersRefType) {
110
- usedTypes = usedTypes.concat(parametersRefType);
111
- this._barModels.update(index, { message: `Models parsing... ${usedTypes}` });
112
-
113
- if (apiSwaggerMethod.responses[200].content[contentTypeApplicationJson]?.schema.$ref != null) {
114
- usedTypes.push(apiSwaggerMethod.responses[200].content[contentTypeApplicationJson].schema.$ref.replace('#/components/schemas/', ''));
115
- }
116
-
117
- if (apiSwaggerMethod.requestBody?.content[contentTypeApplicationJson]?.schema?.$ref) {
118
- usedTypes.push(apiSwaggerMethod.requestBody?.content[contentTypeApplicationJson]?.schema?.$ref.replace('#/components/schemas/', ''));
119
- }
99
+ // for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.paths).length; index++) {
100
+ // const apiName = Object.getOwnPropertyNames(this._swagger.paths)[index];
101
+ // const apiSwaggerMethodKey = this._swagger.paths[apiName];
102
+ // const apiMethod = Object.getOwnPropertyNames(apiSwaggerMethodKey)[0];
103
+ // const apiSwaggerMethod = apiSwaggerMethodKey[apiMethod];
104
+
105
+ // const parametersRefType = apiSwaggerMethod.parameters?.filter(x => x.in == 'query' && x.schema?.$ref != null).map(x => x.schema.$ref.replace('#/components/schemas/', ''));
106
+ // if (parametersRefType) {
107
+ // usedTypes = usedTypes.concat(parametersRefType);
108
+ // }
109
+
110
+ // const responseRefType = this.computeRequestBodyType(apiSwaggerMethod!);
111
+ // if (responseRefType && !responseRefType.isVoid && !responseRefType.isNativeType) {
112
+ // usedTypes = usedTypes.concat(responseRefType.typeName);
113
+ // }
114
+
115
+ // const contentRefType = this.computeResponseType(apiSwaggerMethod!);
116
+ // if (contentRefType && !contentRefType.isVoid && !contentRefType.isNativeType) {
117
+ // usedTypes = usedTypes.concat(contentRefType.typeName);
118
+ // }
119
+ // }
120
+
121
+ for (let index = 0; index < this._apis.length; index++) {
122
+ if (this._apis[index].returnType && this._apis[index].returnType!.isTypeReference) {
123
+ usedTypes.push(this._apis[index].returnType!);
124
+ }
120
125
 
121
- if (apiSwaggerMethod.requestBody?.content[contentTypeMultipartFormData]?.schema != null) {
122
- usedMultiPart.push(apiName);
123
- }
126
+ if (this._apis[index].parameters) {
127
+ this._apis[index].parameters.forEach(parameter => {
128
+ if (parameter.isTypeReference) {
129
+ usedTypes.push(parameter);
130
+ }
131
+ });
124
132
  }
125
133
  }
126
134
 
127
- this.retrieveNestedObjects(usedTypes);
135
+ usedTypes = [...new Set(usedTypes.map(item => item))]; // distinct
128
136
 
129
- usedTypes = [...new Set(usedTypes.map(item => item))]; // [ 'A', 'B']
137
+ this.retrieveNestedObjects(usedTypes);
130
138
 
131
139
  let models = ``;
132
140
  if (this._swagger.components != null
@@ -137,11 +145,6 @@ export class Generator {
137
145
  for (let index = 0; index < Object.getOwnPropertyNames(this._swagger.components.schemas).length; index++) {
138
146
  const modelName = Object.getOwnPropertyNames(this._swagger.components.schemas)[index];
139
147
 
140
- if (usedTypes.indexOf(modelName) < 0) {
141
- // console.debug(`\tModel SKIP - ${modelName}`);
142
- continue
143
- };
144
-
145
148
  const swaggerComponent = this._swagger.components.schemas[modelName];
146
149
 
147
150
  // console.debug(`\tModel - ${modelName}`);
@@ -207,68 +210,100 @@ export class Generator {
207
210
  if (!swaggerMethod || swaggerMethod == null) return [];
208
211
 
209
212
  let parameters: ParameterDto[] = [];
210
-
211
- if (swaggerMethod.requestBody != null && swaggerMethod.requestBody.content[contentTypeMultipartFormData] != null) {
212
- var modelName = this.getApiNameNormalized(apiName);
213
- parameters.push({
214
- name: 'request',
215
- typeName: modelName,
216
- nullable: false,
217
- isQuery: false,
218
- isEnum: false,
219
- isNativeType: false,
220
- });
221
- } else {
222
- if (swaggerMethod.requestBody != null) {
213
+ try {
214
+ if (swaggerMethod.requestBody != null && swaggerMethod.requestBody.content[contentTypeMultipartFormData] != null) {
215
+ var modelName = this.getApiNameNormalized(apiName);
223
216
  parameters.push({
224
217
  name: 'request',
225
- typeName: swaggerMethod.requestBody.content[contentTypeApplicationJson].schema.$ref.replace('#/components/schemas/', ''),
218
+ typeName: modelName,
226
219
  nullable: false,
227
220
  isQuery: false,
228
221
  isEnum: false,
222
+ isTypeReference: true,
229
223
  isNativeType: false,
224
+ isArray: false,
225
+ isVoid: false,
230
226
  });
231
227
  } else {
232
- swaggerMethod.parameters?.filter(x => x.in == 'query').forEach(parameter => {
233
- if (parameter.schema.$ref != null) {
234
- parameters.push({
235
- name: this.toFirstLetterLowercase(parameter.name),
236
- typeName: parameter.schema.$ref.replace('#/components/schemas/', ''),
237
- nullable: !parameter.required,
238
- swaggerParameter: parameter,
239
- isQuery: true,
240
- isEnum: this.isEnum(parameter.schema.$ref),
241
- isNativeType: false,
242
- });
243
- } else {
244
- parameters.push({
245
- name: this.toFirstLetterLowercase(parameter.name),
246
- typeName: this.getNativeType(parameter.schema),
247
- nullable: !parameter.required,
248
- swaggerParameter: parameter,
249
- isQuery: true,
250
- isEnum: false,
251
- isNativeType: true,
252
- });
253
- }
254
- });
228
+ if (swaggerMethod.requestBody != null) {
229
+ parameters.push({
230
+ name: 'request',
231
+ typeName: swaggerMethod.requestBody.content[contentTypeApplicationJson].schema.$ref.replace('#/components/schemas/', ''),
232
+ nullable: false,
233
+ isQuery: false,
234
+ isEnum: false,
235
+ isTypeReference: true,
236
+ isNativeType: false,
237
+ isArray: false,
238
+ isVoid: false,
239
+ });
240
+ } else {
241
+ swaggerMethod.parameters?.filter(x => x.in == 'query').forEach(parameter => {
242
+ if (parameter.schema.$ref != null) {
243
+ parameters.push({
244
+ name: this.toFirstLetterLowercase(parameter.name),
245
+ typeName: parameter.schema.$ref.replace('#/components/schemas/', ''),
246
+ nullable: !parameter.required,
247
+ swaggerParameter: parameter,
248
+ isQuery: true,
249
+ isEnum: this.isEnum(parameter.schema.$ref),
250
+ isTypeReference: false,
251
+ isNativeType: false,
252
+ isArray: false,
253
+ isVoid: false,
254
+ });
255
+ } else {
256
+ parameters.push({
257
+ name: this.toFirstLetterLowercase(parameter.name),
258
+ typeName: this.getNativeType(parameter.schema),
259
+ nullable: !parameter.required,
260
+ swaggerParameter: parameter,
261
+ isQuery: true,
262
+ isEnum: false,
263
+ isTypeReference: false,
264
+ isNativeType: true,
265
+ isArray: false,
266
+ isVoid: false,
267
+ });
268
+ }
269
+ });
270
+ }
255
271
  }
272
+ } catch (error) {
273
+ console.error(`\t\tError in ${apiName} - Attenzione forse hai dimenticato IActionResult e non hai tipizzato il tipo restituito dal servizio`);
256
274
  }
257
275
  return parameters;
258
276
  }
259
277
 
260
- computeReturnType(swaggerMethod: SwaggerMethod) {
278
+ computeResponseType(swaggerMethod: SwaggerMethod) {
261
279
  if (swaggerMethod.responses[200] == null) {
262
280
  return <TypeDto>{
263
281
  typeName: 'void',
264
282
  nullable: false,
283
+ isVoid: true,
284
+ }
285
+ }
286
+
287
+ return this.computeSwaggerContentType(swaggerMethod.responses[200].content[contentTypeApplicationJson]);
288
+ }
289
+
290
+ computeRequestBodyType(swaggerMethod: SwaggerMethod) {
291
+ if (swaggerMethod.requestBody == null) {
292
+ return <TypeDto>{
293
+ typeName: 'void',
294
+ nullable: false,
295
+ isVoid: true,
265
296
  }
266
297
  }
267
298
 
299
+ return this.computeSwaggerContentType(swaggerMethod.requestBody.content[contentTypeApplicationJson]);
300
+ }
301
+
302
+ computeSwaggerContentType(swaggerContent: SwaggerContent) {
268
303
  try {
269
- if (swaggerMethod.responses[200].content[contentTypeApplicationJson].schema.$ref != null)
304
+ if (swaggerContent.schema.$ref != null)
270
305
  return <TypeDto>{
271
- typeName: this.getObjectName(swaggerMethod.responses[200]?.content[contentTypeApplicationJson].schema.$ref),
306
+ typeName: this.getObjectName(swaggerContent.schema.$ref),
272
307
  nullable: false,
273
308
  isNativeType: false,
274
309
  };
@@ -278,19 +313,20 @@ export class Generator {
278
313
  throw new Error(errorMessage);
279
314
  }
280
315
 
281
- if (swaggerMethod.responses[200]?.content[contentTypeApplicationJson].schema.type != null) {
282
- let schema = swaggerMethod.responses[200]?.content[contentTypeApplicationJson].schema;
316
+ if (swaggerContent.schema.type != null) {
317
+ let schema = swaggerContent.schema;
283
318
  if (schema.type == 'array') {
284
319
  if (schema.items.$ref != null) {
285
320
  return <TypeDto>{
286
- typeName: `${this.getObjectName(schema.items.$ref)}[]`,
321
+ typeName: `${this.getObjectName(schema.items.$ref)}`,
287
322
  nullable: false,
288
323
  isNativeType: false,
324
+ isArray: true,
289
325
  };
290
326
  }
291
327
  } else {
292
328
  return <TypeDto>{
293
- typeName: this.getNativeType(swaggerMethod.responses[200]?.content[contentTypeApplicationJson].schema),
329
+ typeName: this.getNativeType(swaggerContent.schema),
294
330
  nullable: false,
295
331
  isNativeType: true,
296
332
  };
@@ -301,7 +337,7 @@ export class Generator {
301
337
 
302
338
  } else {
303
339
  return <TypeDto>{
304
- typeName: this.getNativeType(swaggerMethod.responses[200]?.content[contentTypeApplicationJson].schema),
340
+ typeName: this.getNativeType(swaggerContent.schema),
305
341
  nullable: false,
306
342
  isNativeType: true,
307
343
  };
@@ -312,36 +348,51 @@ export class Generator {
312
348
  if (swaggerComponentProperty.$ref != null)
313
349
  return {
314
350
  typeName: swaggerComponentProperty.$ref.replace('#/components/schemas/', ''),
351
+ isTypeReference: true,
315
352
  isNativeType: false,
316
- nullable: false,
353
+ nullable: swaggerComponentProperty.nullable ?? false,
354
+ isArray: false,
355
+ isVoid: false,
317
356
  };
318
357
 
319
358
  if (swaggerComponentProperty.type != null && swaggerComponentProperty.type == 'array')
320
359
  if (swaggerComponentProperty.items.$ref != null)
321
360
  return {
322
- typeName: `${this.getObjectName(swaggerComponentProperty.items.$ref)}[]`,
361
+ typeName: `${this.getObjectName(swaggerComponentProperty.items.$ref)}`,
362
+ isTypeReference: true,
323
363
  isNativeType: false,
324
- nullable: false,
364
+ nullable: swaggerComponentProperty.nullable ?? false,
365
+ isArray: true,
366
+ isVoid: false,
325
367
  };
326
368
  else
327
369
  return {
328
370
  typeName: this.getNativeType(swaggerComponentProperty),
329
- isNativeType: false,
330
- nullable: true,
371
+ isTypeReference: false,
372
+ isNativeType: true,
373
+ nullable: swaggerComponentProperty.nullable ?? false,
374
+ isArray: false,
375
+ isVoid: false,
331
376
  };
332
377
 
333
378
  if (swaggerComponentProperty.type != null)
334
379
  return {
335
380
  typeName: this.getNativeType(swaggerComponentProperty),
336
- isNativeType: false,
337
- nullable: true,
381
+ isTypeReference: false,
382
+ isNativeType: true,
383
+ nullable: swaggerComponentProperty.nullable ?? false,
384
+ isArray: false,
385
+ isVoid: false,
338
386
  };
339
387
 
340
388
  if (swaggerComponentProperty.type == null)
341
389
  return {
342
- typeName: 'null',
390
+ typeName: 'void',
391
+ isTypeReference: false,
343
392
  isNativeType: false,
344
- nullable: true,
393
+ nullable: swaggerComponentProperty.nullable ?? false,
394
+ isArray: false,
395
+ isVoid: true,
345
396
  };
346
397
 
347
398
  console.error("unmanaged swaggerMethodInfo", swaggerComponentProperty);
@@ -381,7 +432,7 @@ export class Generator {
381
432
  properties.push({
382
433
  ...type,
383
434
  name: propertyName,
384
- nullable: true,
435
+ nullable: type.nullable,
385
436
  });
386
437
  }
387
438
 
@@ -416,32 +467,44 @@ export class Generator {
416
467
  return values;
417
468
  }
418
469
 
419
- retrieveNestedObjects(usedTypes: string[]) {
470
+ retrieveNestedObjects(usedTypes: TypeDto[]) {
420
471
  for (let i = 0; i < usedTypes.length; i++) {
421
- const swaggerCopmponent = this._swagger.components.schemas[usedTypes[i]];
422
- // const name = usedTypes[i]
423
- // const modelName = <string>Object.getOwnPropertyNames(this._swagger.components.schemas)[name];
472
+ const swaggerCopmponent = this._swagger.components.schemas[usedTypes[i].typeName];
424
473
  this.retrieveNestedObjectsRecursive(swaggerCopmponent, usedTypes);
425
474
  }
426
475
  }
427
476
 
428
- retrieveNestedObjectsRecursive(swaggerComponent: SwaggerComponent, usedTypes: string[]) {
429
- if (!swaggerComponent.properties) return;
477
+ retrieveNestedObjectsRecursive(swaggerComponent: SwaggerComponent, usedTypes: TypeDto[]) {
478
+ try {
479
+ if (!swaggerComponent.properties) return;
430
480
 
431
- for (let j = 0; j < Object.getOwnPropertyNames(swaggerComponent.properties).length; j++) {
432
- const propertyName = Object.getOwnPropertyNames(swaggerComponent.properties)[j];
433
- let nestedUsedType = '';
434
- if (swaggerComponent.properties[propertyName].$ref != null) {
435
- nestedUsedType = swaggerComponent.properties[propertyName].$ref.replace('#/components/schemas/', '');
436
- } else if (swaggerComponent.properties[propertyName].type == 'array' && swaggerComponent.properties[propertyName].items.$ref != null) {
437
- nestedUsedType = swaggerComponent.properties[propertyName].items.$ref.replace('#/components/schemas/', '');
438
- }
481
+ for (let j = 0; j < Object.getOwnPropertyNames(swaggerComponent.properties).length; j++) {
482
+ const propertyName = Object.getOwnPropertyNames(swaggerComponent.properties)[j];
439
483
 
440
- if (nestedUsedType != '' && usedTypes.findIndex(x => x == nestedUsedType) == -1) {
441
- usedTypes.push(nestedUsedType);
442
- let nested = this._swagger.components.schemas[nestedUsedType];
443
- this.retrieveNestedObjectsRecursive(nested, usedTypes);
484
+ let nestedUsedType = '';
485
+
486
+ if (swaggerComponent.properties[propertyName].$ref != null) {
487
+ nestedUsedType = swaggerComponent.properties[propertyName].$ref.replace('#/components/schemas/', '');
488
+ } else if (swaggerComponent.properties[propertyName].type == 'array' && swaggerComponent.properties[propertyName].items.$ref != null) {
489
+ nestedUsedType = swaggerComponent.properties[propertyName].items.$ref.replace('#/components/schemas/', '');
490
+ }
491
+
492
+ if (nestedUsedType != '' && usedTypes.findIndex(x => x.typeName == nestedUsedType) == -1) {
493
+ let nested = this._swagger.components.schemas[nestedUsedType];
494
+ usedTypes.push({
495
+ typeName: nestedUsedType,
496
+ isTypeReference: nested.type == 'object',
497
+ isNativeType: nested.type != 'object',
498
+ nullable: false,
499
+ isArray: false,
500
+ isVoid: false,
501
+ //potrebbe essere un enum
502
+ });
503
+ this.retrieveNestedObjectsRecursive(nested, usedTypes);
504
+ }
444
505
  }
506
+ } catch (error) {
507
+ debugger
445
508
  }
446
509
  }
447
510
 
@@ -63,7 +63,8 @@ export class ApiAngularWriter {
63
63
 
64
64
  private _returnType(api: ApiDto) {
65
65
  const prefixType = !api.returnType?.isNativeType ? 'Models.' : '';
66
- return api.returnType ? `${prefixType}${api.returnType.typeName}` : 'any';
66
+ const isArray = api.returnType?.isArray ? '[]' : '';
67
+ return api.returnType ? `${prefixType}${api.returnType.typeName}${isArray}` : 'any';
67
68
  }
68
69
 
69
70
  private _queryParametersPreparation(api: ApiDto) {
@@ -8,7 +8,14 @@ export class Utils {
8
8
  if (normalizedApiName.charAt(0) == '_') {
9
9
  normalizedApiName = normalizedApiName.slice(1);
10
10
  }
11
- return this.toFirstLetterLowercase(normalizedApiName);
11
+
12
+ normalizedApiName = this.toCamelCase(normalizedApiName);
13
+
14
+ normalizedApiName = this.toFirstLetterLowercase(normalizedApiName);
15
+
16
+ normalizedApiName = normalizedApiName.replaceAll('_', '');
17
+
18
+ return normalizedApiName;
12
19
  }
13
20
 
14
21
  public static toFirstLetterLowercase(value: string) {
@@ -6,4 +6,6 @@ export interface SwaggerComponentProperty {
6
6
  format: string;
7
7
  items: SwaggerSchema;
8
8
  properties: { [key: string]: SwaggerComponentProperty; };
9
+ nullable: boolean;
10
+ minLength: number;
9
11
  }
@@ -1,5 +1,8 @@
1
- export interface TypeDto{
1
+ export interface TypeDto {
2
2
  typeName: string;
3
3
  nullable: boolean;
4
4
  isNativeType: boolean;
5
+ isTypeReference: boolean;
6
+ isVoid: boolean;
7
+ isArray: boolean;
5
8
  }