• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

electrode-io / electrode-native / 7564

08 Apr 2026 06:30AM UTC coverage: 56.617% (-11.2%) from 67.856%
7564

push

Azure Pipelines

r0h0gg6
Update lerna publish to avoid npm classic token deprec

3601 of 7762 branches covered (46.39%)

Branch coverage included in aggregate %.

9426 of 15247 relevant lines covered (61.82%)

523.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.69
/ern-api-gen/src/languages/JavascriptClientCodegen.ts
1
/* tslint:disable:variable-name */
2
import CliOption from '../CliOption';
2✔
3
import CodegenConstants from '../CodegenConstants';
2✔
4
import CodegenModel from '../CodegenModel';
2✔
5
import CodegenOperation from '../CodegenOperation';
2✔
6
import CodegenParameter from '../CodegenParameter';
2✔
7
import CodegenProperty from '../CodegenProperty';
2✔
8
import CodegenType from '../CodegenType';
2✔
9
import DefaultCodegen from '../DefaultCodegen';
2✔
10
import SupportingFile from '../SupportingFile';
2✔
11
import ArrayModel from '../models/ArrayModel';
2✔
12
import ModelImpl from '../models/ModelImpl';
2✔
13
import {
2✔
14
  ArrayProperty,
15
  BooleanProperty,
16
  DoubleProperty,
17
  FloatProperty,
18
  IntegerProperty,
19
  LongProperty,
20
  MapProperty,
21
  RefProperty,
22
  StringProperty,
23
} from '../models/properties';
24
import { log } from 'ern-core';
2✔
25
import File from '../java/File';
2✔
26
import { Collections, newHashMap, newHashSet } from '../java/javaUtil';
2✔
27
import StringBuilder from '../java/StringBuilder';
2✔
28
import { isEmpty } from '../java/StringUtils';
2✔
29
import path from 'path';
2✔
30
import { parseBoolean } from '../java/BooleanHelper';
2✔
31

32
export default class JavascriptClientCodegen extends DefaultCodegen {
2✔
33
  public static PROJECT_NAME = 'projectName';
2✔
34
  public static MODULE_NAME = 'moduleName';
2✔
35
  public static PROJECT_DESCRIPTION = 'projectDescription';
2✔
36
  public static PROJECT_VERSION = 'projectVersion';
2✔
37
  public static PROJECT_LICENSE_NAME = 'projectLicenseName';
2✔
38
  public static USE_PROMISES = 'usePromises';
2✔
39
  public static USE_INHERITANCE = 'useInheritance';
2✔
40
  public static EMIT_MODEL_METHODS = 'emitModelMethods';
2✔
41
  public static EMIT_JS_DOC = 'emitJSDoc';
2✔
42

43
  public static reconcileInlineEnums(codegenModel, parentCodegenModel) {
44
    if (parentCodegenModel.hasEnums) {
×
45
      const parentModelCodegenProperties = parentCodegenModel.vars;
×
46
      const codegenProperties = codegenModel.vars;
×
47
      let removedChildEnum = false;
×
48
      for (const parentModelCodegenPropery of parentModelCodegenProperties) {
×
49
        if (parentModelCodegenPropery.isEnum) {
×
50
          for (let i = codegenProperties.length; i--; ) {
×
51
            const codegenProperty = codegenProperties[i];
×
52
            if (
×
53
              codegenProperty.isEnum &&
×
54
              codegenProperty.equals(parentModelCodegenPropery)
55
            ) {
56
              codegenProperties.splice(i, 1);
×
57
              removedChildEnum = true;
×
58
            }
59
          }
60
        }
61
      }
62
      if (removedChildEnum) {
×
63
        let codegenProperty;
64
        for (codegenProperty of codegenProperties) {
×
65
          codegenProperty.hasMore = true;
×
66
        }
67
        codegenProperty.hasMore = false;
×
68
        codegenModel.vars = codegenProperties;
×
69
      }
70
    }
71
    return codegenModel;
×
72
  }
73

74
  public sourceFolder = 'src';
366✔
75
  public localVariablePrefix = '';
366✔
76
  public emitJSDoc = true;
366✔
77
  public apiDocPath = 'docs/';
366✔
78
  public modelDocPath = 'docs/';
366✔
79
  public apiTestPath = 'api/';
366✔
80
  public modelTestPath = 'model/';
366✔
81
  public usePromises = false;
366✔
82
  public emitModelMethods = false;
366✔
83
  public __outputFolder = 'generated-code/js';
366✔
84
  public __templateDir = 'Javascript';
366✔
85
  public __apiPackage = 'api';
366✔
86
  public __modelPackage = 'model';
366✔
87
  public __typeMapping = newHashMap(
366✔
88
    ['array', 'Array'],
89
    ['map', 'Object'],
90
    ['List', 'Array'],
91
    ['boolean', 'Boolean'],
92
    ['string', 'String'],
93
    ['int', 'Integer'],
94
    ['float', 'Number'],
95
    ['number', 'Number'],
96
    ['DateTime', 'Date'],
97
    ['date', 'Date'],
98
    ['long', 'Integer'],
99
    ['short', 'Integer'],
100
    ['char', 'String'],
101
    ['double', 'Number'],
102
    ['object', 'Object'],
103
    ['integer', 'Integer'],
104
    ['ByteArray', 'String'],
105
    ['binary', 'String'],
106
    ['UUID', 'String'],
107
  );
108

109
  public _swaggerToFlowMapping = newHashMap(
366✔
110
    ['Integer', 'number'],
111
    ['Number', 'number'],
112
    ['String', 'string'],
113
    ['Boolean', 'boolean'],
114
  );
115

116
  public __languageSpecificPrimitives = newHashSet(
366✔
117
    'String',
118
    'Boolean',
119
    'Integer',
120
    'Number',
121
    'Array',
122
    'Object',
123
    'Date',
124
    'File',
125
  );
126

127
  public projectVersion;
128
  public projectDescription;
129
  public projectName;
130
  public moduleName;
131
  public invokerPackage;
132

133
  constructor() {
134
    super();
366✔
135
    this.__modelTemplateFiles.put('model.mustache', '.js');
366✔
136
    this.__modelTestTemplateFiles.put('model_test.mustache', '.js');
366✔
137
    this.__apiTemplateFiles.put('api.mustache', '.js');
366✔
138
    this.__apiTestTemplateFiles.put('api_test.mustache', '.js');
366✔
139
    this.__modelDocTemplateFiles.put('model_doc.mustache', '.md');
366✔
140
    this.__apiDocTemplateFiles.put('api_doc.mustache', '.md');
366✔
141
    this.__defaultIncludes = newHashSet(...this.__languageSpecificPrimitives);
366✔
142
    this.__instantiationTypes.put('array', 'Array');
366✔
143
    this.__instantiationTypes.put('list', 'Array');
366✔
144
    this.__instantiationTypes.put('map', 'Object');
366✔
145
    this.__supportingFiles.push(
366✔
146
      new SupportingFile('git_push.sh.mustache', '', 'git_push.sh'),
147
    );
148
    this.__supportingFiles.push(
366✔
149
      new SupportingFile('README.mustache', '', 'README.md'),
150
    );
151
    this.__supportingFiles.push(
366✔
152
      new SupportingFile('mocha.opts', '', 'mocha.opts'),
153
    );
154
    this.__supportingFiles.push(
366✔
155
      new SupportingFile('travis.yml', '', '.travis.yml'),
156
    );
157
    // this.__supportingFiles.push(new SupportingFile("package.mustache", "", "package.json"));
158

159
    this.setReservedWordsLowerCase([
366✔
160
      'abstract',
161
      'arguments',
162
      'boolean',
163
      'break',
164
      'byte',
165
      'case',
166
      'catch',
167
      'char',
168
      'class',
169
      'const',
170
      'continue',
171
      'debugger',
172
      'default',
173
      'delete',
174
      'do',
175
      'double',
176
      'else',
177
      'enum',
178
      'eval',
179
      'export',
180
      'extends',
181
      'false',
182
      'final',
183
      'finally',
184
      'float',
185
      'for',
186
      'function',
187
      'goto',
188
      'if',
189
      'implements',
190
      'import',
191
      'in',
192
      'instanceof',
193
      'int',
194
      'interface',
195
      'let',
196
      'long',
197
      'native',
198
      'new',
199
      'null',
200
      'package',
201
      'private',
202
      'protected',
203
      'public',
204
      'return',
205
      'short',
206
      'static',
207
      'super',
208
      'switch',
209
      'synchronized',
210
      'this',
211
      'throw',
212
      'throws',
213
      'transient',
214
      'true',
215
      'try',
216
      'typeof',
217
      'var',
218
      'void',
219
      'volatile',
220
      'while',
221
      'with',
222
      'yield',
223
      'Array',
224
      'Date',
225
      'eval',
226
      'function',
227
      'hasOwnProperty',
228
      'Infinity',
229
      'isFinite',
230
      'isNaN',
231
      'isPrototypeOf',
232
      'Math',
233
      'NaN',
234
      'Number',
235
      'Object',
236
      'prototype',
237
      'String',
238
      'toString',
239
      'undefined',
240
      'valueOf',
241
    ]);
242
  }
243

244
  public initalizeCliOptions() {
245
    super.initalizeCliOptions();
366✔
246
    this.__cliOptions.push(
366✔
247
      new CliOption(
248
        CodegenConstants.SOURCE_FOLDER,
249
        CodegenConstants.SOURCE_FOLDER_DESC,
250
      ).defaultValue('src'),
251
    );
252
    this.__cliOptions.push(
366✔
253
      new CliOption(
254
        CodegenConstants.LOCAL_VARIABLE_PREFIX,
255
        CodegenConstants.LOCAL_VARIABLE_PREFIX_DESC,
256
      ),
257
    );
258
    this.__cliOptions.push(
366✔
259
      new CliOption(
260
        CodegenConstants.INVOKER_PACKAGE,
261
        CodegenConstants.INVOKER_PACKAGE_DESC,
262
      ),
263
    );
264
    this.__cliOptions.push(
366✔
265
      new CliOption(
266
        CodegenConstants.API_PACKAGE,
267
        CodegenConstants.API_PACKAGE_DESC,
268
      ),
269
    );
270
    this.__cliOptions.push(
366✔
271
      new CliOption(
272
        CodegenConstants.MODEL_PACKAGE,
273
        CodegenConstants.MODEL_PACKAGE_DESC,
274
      ),
275
    );
276
    this.__cliOptions.push(
366✔
277
      new CliOption(
278
        JavascriptClientCodegen.PROJECT_NAME,
279
        'name of the project (Default: generated from info.title or "swagger-js-client")',
280
      ),
281
    );
282
    this.__cliOptions.push(
366✔
283
      new CliOption(
284
        JavascriptClientCodegen.MODULE_NAME,
285
        'module name for AMD, Node or globals (Default: generated from <projectName>)',
286
      ),
287
    );
288
    this.__cliOptions.push(
366✔
289
      new CliOption(
290
        JavascriptClientCodegen.PROJECT_DESCRIPTION,
291
        'description of the project (Default: using info.description or "Client library of <projectName>")',
292
      ),
293
    );
294
    this.__cliOptions.push(
366✔
295
      new CliOption(
296
        JavascriptClientCodegen.PROJECT_VERSION,
297
        'version of the project (Default: using info.version or "1.0.0")',
298
      ),
299
    );
300
    this.__cliOptions.push(
366✔
301
      new CliOption(
302
        JavascriptClientCodegen.PROJECT_LICENSE_NAME,
303
        'name of the license the project uses (Default: using info.license.name)',
304
      ),
305
    );
306
    this.__cliOptions.push(
366✔
307
      new CliOption(
308
        JavascriptClientCodegen.USE_PROMISES,
309
        'use Promises as return values from the client API, instead of superagent callbacks',
310
      ).defaultValue('false'),
311
    );
312
    this.__cliOptions.push(
366✔
313
      new CliOption(
314
        JavascriptClientCodegen.EMIT_MODEL_METHODS,
315
        'generate getters and setters for model properties',
316
      ).defaultValue('false'),
317
    );
318
    this.__cliOptions.push(
366✔
319
      new CliOption(
320
        JavascriptClientCodegen.EMIT_JS_DOC,
321
        'generate JSDoc comments',
322
      ).defaultValue('true'),
323
    );
324
    this.__cliOptions.push(
366✔
325
      new CliOption(
326
        JavascriptClientCodegen.USE_INHERITANCE,
327
        'use JavaScript prototype chains & delegation for inheritance',
328
      ).defaultValue('true'),
329
    );
330
  }
331

332
  public getTag() {
333
    return CodegenType.CLIENT;
×
334
  }
335

336
  public getName() {
337
    return 'javascript';
53✔
338
  }
339

340
  public getHelp() {
341
    return 'Generates a JavaScript client library.';
×
342
  }
343

344
  public processOpts() {
345
    super.processOpts();
47✔
346
    if (
47!
347
      this.__additionalProperties.containsKey(
348
        JavascriptClientCodegen.PROJECT_NAME,
349
      )
350
    ) {
351
      (this as any).setProjectName(
×
352
        this.__additionalProperties.get(JavascriptClientCodegen.PROJECT_NAME),
353
      );
354
    }
355
    if (
47!
356
      this.__additionalProperties.containsKey(
357
        JavascriptClientCodegen.MODULE_NAME,
358
      )
359
    ) {
360
      (this as any).setModuleName(
×
361
        this.__additionalProperties.get(JavascriptClientCodegen.MODULE_NAME),
362
      );
363
    }
364
    if (
47!
365
      this.__additionalProperties.containsKey(
366
        JavascriptClientCodegen.PROJECT_DESCRIPTION,
367
      )
368
    ) {
369
      (this as any).setProjectDescription(
×
370
        this.__additionalProperties.get(
371
          JavascriptClientCodegen.PROJECT_DESCRIPTION,
372
        ),
373
      );
374
    }
375
    if (
47!
376
      this.__additionalProperties.containsKey(
377
        JavascriptClientCodegen.PROJECT_VERSION,
378
      )
379
    ) {
380
      (this as any).setProjectVersion(
×
381
        this.__additionalProperties.get(
382
          JavascriptClientCodegen.PROJECT_VERSION,
383
        ),
384
      );
385
    }
386
    if (
47!
387
      this.__additionalProperties.containsKey(
388
        JavascriptClientCodegen.PROJECT_LICENSE_NAME,
389
      )
390
    ) {
391
      (this as any).setProjectLicenseName(
×
392
        this.__additionalProperties.get(
393
          JavascriptClientCodegen.PROJECT_LICENSE_NAME,
394
        ),
395
      );
396
    }
397
    if (
47!
398
      this.__additionalProperties.containsKey(
399
        CodegenConstants.LOCAL_VARIABLE_PREFIX,
400
      )
401
    ) {
402
      (this as any).setLocalVariablePrefix(
×
403
        this.__additionalProperties.get(CodegenConstants.LOCAL_VARIABLE_PREFIX),
404
      );
405
    }
406
    if (
47!
407
      this.__additionalProperties.containsKey(CodegenConstants.SOURCE_FOLDER)
408
    ) {
409
      (this as any).setSourceFolder(
×
410
        this.__additionalProperties.get(CodegenConstants.SOURCE_FOLDER),
411
      );
412
    }
413
    if (
47!
414
      this.__additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)
415
    ) {
416
      (this as any).setInvokerPackage(
×
417
        this.__additionalProperties.get(CodegenConstants.INVOKER_PACKAGE),
418
      );
419
    }
420
    if (
47!
421
      this.__additionalProperties.containsKey(
422
        JavascriptClientCodegen.USE_PROMISES,
423
      )
424
    ) {
425
      this.usePromises = parseBoolean(
×
426
        this.__additionalProperties.get(JavascriptClientCodegen.USE_PROMISES),
427
      );
428
    }
429
    if (
47!
430
      this.__additionalProperties.containsKey(
431
        JavascriptClientCodegen.USE_INHERITANCE,
432
      )
433
    ) {
434
      this.supportsInheritance = parseBoolean(
×
435
        this.__additionalProperties.get(
436
          JavascriptClientCodegen.USE_INHERITANCE,
437
        ),
438
      );
439
    } else {
440
      this.supportsInheritance = true;
47✔
441
    }
442
    if (
47!
443
      this.__additionalProperties.containsKey(
444
        JavascriptClientCodegen.EMIT_MODEL_METHODS,
445
      )
446
    ) {
447
      (this as any).setEmitModelMethods(
×
448
        parseBoolean(
449
          this.__additionalProperties.get(
450
            JavascriptClientCodegen.EMIT_MODEL_METHODS,
451
          ),
452
        ),
453
      );
454
    }
455
    if (
47!
456
      this.__additionalProperties.containsKey(
457
        JavascriptClientCodegen.EMIT_JS_DOC,
458
      )
459
    ) {
460
      (this as any).setEmitJSDoc(
×
461
        parseBoolean(
462
          this.__additionalProperties.get(JavascriptClientCodegen.EMIT_JS_DOC),
463
        ),
464
      );
465
    }
466
  }
467

468
  public preprocessSwagger(swagger) {
469
    super.preprocessSwagger(swagger);
47✔
470
    if (swagger.getInfo() != null) {
47!
471
      const info = swagger.getInfo();
47✔
472
      if (isEmpty(this.projectName) && info.getTitle() != null) {
47✔
473
        this.projectName = this.sanitizeName(this.dashize(info.getTitle()));
46✔
474
      }
475
      if (isEmpty(this.projectVersion)) {
47✔
476
        this.projectVersion = this.escapeUnsafeCharacters(
46✔
477
          this.escapeQuotationMark(info.getVersion()),
478
        );
479
      }
480
      if (this.projectDescription == null && info.getDescription()) {
47✔
481
        this.projectDescription = this.sanitizeName(info.getDescription());
44✔
482
      }
483
      if (
47!
484
        this.__additionalProperties.get(
485
          JavascriptClientCodegen.PROJECT_LICENSE_NAME,
486
        ) == null
487
      ) {
488
        const license = info.getLicense();
47✔
489
        if (license && license.getName()) {
47✔
490
          this.__additionalProperties.put(
15✔
491
            JavascriptClientCodegen.PROJECT_LICENSE_NAME,
492
            this.sanitizeName(license.getName()),
493
          );
494
        }
495
      }
496
    }
497
    if (isEmpty(this.projectName)) {
47!
498
      this.projectName = 'swagger-js-client';
×
499
    }
500
    if (isEmpty(this.moduleName)) {
47✔
501
      this.moduleName = DefaultCodegen.camelize(
46✔
502
        DefaultCodegen.underscore(this.projectName),
503
      );
504
    }
505
    if (isEmpty(this.projectVersion)) {
47!
506
      this.projectVersion = '1.0.0';
×
507
    }
508
    if (this.projectDescription == null) {
47✔
509
      this.projectDescription = 'Client library of ' + this.projectName;
3✔
510
    }
511
    this.__additionalProperties.put(
47✔
512
      JavascriptClientCodegen.PROJECT_NAME,
513
      this.projectName,
514
    );
515
    this.__additionalProperties.put(
47✔
516
      JavascriptClientCodegen.MODULE_NAME,
517
      this.moduleName,
518
    );
519
    this.__additionalProperties.put(
47✔
520
      JavascriptClientCodegen.PROJECT_DESCRIPTION,
521
      this.escapeText(this.projectDescription),
522
    );
523
    this.__additionalProperties.put(
47✔
524
      JavascriptClientCodegen.PROJECT_VERSION,
525
      this.projectVersion,
526
    );
527
    this.__additionalProperties.put(
47✔
528
      CodegenConstants.API_PACKAGE,
529
      this.__apiPackage,
530
    );
531
    this.__additionalProperties.put(
47✔
532
      CodegenConstants.INVOKER_PACKAGE,
533
      this.invokerPackage,
534
    );
535
    this.__additionalProperties.put(
47✔
536
      CodegenConstants.LOCAL_VARIABLE_PREFIX,
537
      this.localVariablePrefix,
538
    );
539
    this.__additionalProperties.put(
47✔
540
      CodegenConstants.MODEL_PACKAGE,
541
      this.__modelPackage,
542
    );
543
    this.__additionalProperties.put(
47✔
544
      CodegenConstants.SOURCE_FOLDER,
545
      this.sourceFolder,
546
    );
547
    this.__additionalProperties.put(
47✔
548
      JavascriptClientCodegen.USE_PROMISES,
549
      this.usePromises,
550
    );
551
    this.__additionalProperties.put(
47✔
552
      JavascriptClientCodegen.USE_INHERITANCE,
553
      this.supportsInheritance,
554
    );
555
    this.__additionalProperties.put(
47✔
556
      JavascriptClientCodegen.EMIT_MODEL_METHODS,
557
      this.emitModelMethods,
558
    );
559
    this.__additionalProperties.put(
47✔
560
      JavascriptClientCodegen.EMIT_JS_DOC,
561
      this.emitJSDoc,
562
    );
563
    this.__additionalProperties.put('apiDocPath', this.apiDocPath);
47✔
564
    this.__additionalProperties.put('modelDocPath', this.modelDocPath);
47✔
565
    this.__supportingFiles.push(
47✔
566
      new SupportingFile(
567
        'index.mustache',
568
        this.createPath(this.sourceFolder, this.invokerPackage),
569
        'index.js',
570
      ),
571
    );
572
    this.__supportingFiles.push(
47✔
573
      new SupportingFile(
574
        'ApiClient.mustache',
575
        this.createPath(this.sourceFolder, this.invokerPackage),
576
        'ApiClient.js',
577
      ),
578
    );
579
  }
580

581
  public escapeReservedWord(name) {
582
    return '_' + name;
×
583
  }
584

585
  /**
586
   * Concatenates an array of path segments into a path string.
587
   * @param segments The path segments to concatenate. A segment may contain either of the file separator characters '\' or '/'.
588
   * A segment is ignored if it is <code>null</code>, empty or &quot;.&quot;.
589
   * @return A path string using the correct platform-specific file separator character.
590
   */
591
  public createPath(...segments) {
592
    const buf = StringBuilder();
299✔
593
    for (const segment of segments) {
299✔
594
      if (!isEmpty(segment) && !(segment === '.')) {
782✔
595
        if (buf.length() !== 0) {
596✔
596
          buf.append(File.separatorChar);
297✔
597
        }
598
        buf.append(segment);
596✔
599
      }
600
    }
601

602
    return path.normalize(buf.toString());
299✔
603
  }
604

605
  public apiTestFileFolder() {
606
    return (
73✔
607
      this.__outputFolder +
608
      path.sep +
609
      'test' +
610
      path.sep +
611
      this.apiTestPath
612
    )
613
      .split(path.sep)
614
      .join(File.separatorChar);
615
  }
616

617
  public modelTestFileFolder() {
618
    return (
67✔
619
      this.__outputFolder +
620
      path.sep +
621
      'test' +
622
      path.sep +
623
      this.modelTestPath
624
    )
625
      .split(path.sep)
626
      .join(File.separatorChar);
627
  }
628

629
  public apiFileFolder() {
630
    return this.createPath(
20✔
631
      this.__outputFolder,
632
      this.sourceFolder,
633
      this.invokerPackage,
634
      this.apiPackage(),
635
    );
636
  }
637

638
  public modelFileFolder() {
639
    return this.createPath(
72✔
640
      this.__outputFolder,
641
      this.sourceFolder,
642
      this.invokerPackage,
643
      this.modelPackage(),
644
    );
645
  }
646

647
  public apiDocFileFolder() {
648
    return this.createPath(this.__outputFolder, this.apiDocPath);
46✔
649
  }
650

651
  public modelDocFileFolder() {
652
    return this.createPath(this.__outputFolder, this.modelDocPath);
67✔
653
  }
654

655
  public toApiDocFilename(name) {
656
    return this.toApiName(name);
46✔
657
  }
658

659
  public toModelDocFilename(name) {
660
    return this.toModelName(name);
67✔
661
  }
662

663
  public toApiTestFilename(name) {
664
    return this.toApiName(name) + '.spec';
73✔
665
  }
666

667
  public toModelTestFilename(name) {
668
    return this.toModelName(name) + '.spec';
67✔
669
  }
670

671
  public toVarName(name) {
672
    name = this.sanitizeName(name);
7,627✔
673
    if ('_' === name) {
7,627!
674
      name = '_u';
×
675
    }
676
    if (name.match('^[A-Z_]*$')) {
7,627!
677
      return name;
×
678
    }
679
    name = DefaultCodegen.camelize(name, true);
7,627✔
680
    if (this.isReservedWord(name) || name.match('^\\d.*')) {
7,627!
681
      name = this.escapeReservedWord(name);
×
682
    }
683
    return name;
7,627✔
684
  }
685

686
  public toParamName(name) {
687
    return this.toVarName(name);
429✔
688
  }
689

690
  public toModelName(name) {
691
    name = this.sanitizeName(name);
3,744✔
692
    if (!isEmpty(this.modelNamePrefix)) {
3,744!
693
      name = this.modelNamePrefix + '_' + name;
×
694
    }
695
    if (!isEmpty(this.modelNameSuffix)) {
3,744!
696
      name = name + '_' + this.modelNameSuffix;
×
697
    }
698
    name = DefaultCodegen.camelize(name);
3,744✔
699
    if (this.isReservedWord(name)) {
3,744✔
700
      const modelName = 'Model' + name;
4✔
701
      log.warn(
4✔
702
        `${name} (reserved word) cannot be used as model name. Renamed to ${modelName}`,
703
      );
704
      return modelName;
4✔
705
    }
706
    if (name.match('^\\d.*')) {
3,740!
707
      const modelName = 'Model' + name;
×
708
      log.warn(
×
709
        `${name} (model name starts with number) cannot be used as model name. Renamed to ${modelName}`,
710
      );
711
      return modelName;
×
712
    }
713
    return name;
3,740✔
714
  }
715

716
  public toModelFilename(name) {
717
    return this.toModelName(name);
313✔
718
  }
719

720
  public toModelImport(name) {
721
    return name;
224✔
722
  }
723

724
  public toApiImport(name) {
725
    return this.toApiName(name);
73✔
726
  }
727

728
  public getTypeDeclaration(p) {
729
    if (p != null && p instanceof ArrayProperty) {
5,316✔
730
      const inner = p.getItems();
560✔
731
      return '[' + this.getTypeDeclaration(inner) + ']';
560✔
732
    } else if (p != null && p instanceof MapProperty) {
4,756✔
733
      const inner = p.getAdditionalProperties();
48✔
734
      return '{String: ' + this.getTypeDeclaration(inner) + '}';
48✔
735
    }
736
    return super.getTypeDeclaration(p);
4,708✔
737
  }
738

739
  public toDefaultValue(p) {
740
    const dp = p.getDefault();
2,482✔
741
    if (dp != null) {
2,482✔
742
      if (p instanceof StringProperty) {
15✔
743
        return JSON.stringify(dp) + '';
6✔
744
      }
745
      if (
9!
746
        p instanceof BooleanProperty ||
9!
747
        p instanceof DoubleProperty ||
748
        p instanceof FloatProperty ||
749
        p instanceof IntegerProperty ||
750
        p instanceof LongProperty
751
      ) {
752
        return dp.toString();
9✔
753
      }
754
    }
755

756
    return dp;
2,467✔
757
  }
758

759
  public toDefaultValueWithParam(name, p) {
760
    const type = this.normalizeType(this.getTypeDeclaration(p));
2,319✔
761
    if (p instanceof RefProperty) {
2,319✔
762
      return ' = ' + type + ".constructFromObject(data['" + name + "']);";
486✔
763
    } else {
764
      return " = ApiClient.convertToType(data['" + name + "'], " + type + ');';
1,833✔
765
    }
766
  }
767

768
  public setParameterExampleValue(p) {
769
    let example;
770
    if (p.defaultValue == null) {
429✔
771
      example = p.example;
417✔
772
    } else {
773
      example = p.defaultValue;
12✔
774
    }
775
    let type = p.baseType;
429✔
776
    if (type == null) {
429✔
777
      type = p.dataType;
284✔
778
    }
779
    if ('String' === type) {
429✔
780
      if (example == null) {
228✔
781
        example = p.paramName + '_example';
18✔
782
      }
783
      example = '"' + this.escapeText(example) + '"';
228✔
784
    } else if ('Integer' === type) {
201✔
785
      if (example == null) {
75✔
786
        example = '56';
6✔
787
      }
788
    } else if ('Number' === type) {
126✔
789
      if (example == null) {
32!
790
        example = '3.4';
×
791
      }
792
    } else if ('Boolean' === type) {
94!
793
      if (example == null) {
×
794
        example = 'true';
×
795
      }
796
    } else if ('File' === type) {
94!
797
      if (example == null) {
×
798
        example = '/path/to/file';
×
799
      }
800
      example = '"' + this.escapeText(example) + '"';
×
801
    } else if ('Date' === type) {
94!
802
      if (example == null) {
×
803
        example = '2013-10-20T19:20:30+01:00';
×
804
      }
805
      example = 'new Date("' + this.escapeText(example) + '")';
×
806
    } else if (!this.__languageSpecificPrimitives.contains(type)) {
94!
807
      example = 'new ' + this.moduleName + '.' + type + '()';
94✔
808
    }
809
    if (example == null) {
429!
810
      example = 'null';
×
811
    } else if (p.isListContainer) {
429✔
812
      example = '[' + example + ']';
57✔
813
    } else if (p.isMapContainer) {
372!
814
      example = '{key: ' + example + '}';
×
815
    }
816
    p.example = example;
429✔
817
  }
818

819
  /**
820
   * Normalize type by wrapping primitive types with single quotes.
821
   *
822
   * @param type Primitive type
823
   * @return Normalized type
824
   */
825
  public normalizeType(type) {
826
    return type.replace(
2,420✔
827
      new RegExp('\\b(Boolean|Integer|Number|String|Date)\\b', 'g'),
828
      "'$1'",
829
    );
830
  }
831

832
  public getSwaggerType(p) {
833
    const swaggerType = super.getSwaggerType(p);
9,580✔
834
    let type = null;
9,580✔
835
    if (this.__typeMapping.containsKey(swaggerType)) {
9,580✔
836
      type = this.__typeMapping.get(swaggerType);
7,292✔
837
      if (!this.needToImport(type)) {
7,292!
838
        return type;
7,292✔
839
      }
840
    } else {
841
      type = swaggerType;
2,288✔
842
    }
843
    if (null == type) {
2,288!
844
      log.error(`No Type defined for Property ${p}`);
×
845
    }
846
    return this.toModelName(type);
2,288✔
847
  }
848

849
  public toOperationId(operationId) {
850
    if (isEmpty(operationId)) {
353!
851
      throw new Error('Empty method/operation name (operationId) not allowed');
×
852
    }
853
    operationId = DefaultCodegen.camelize(this.sanitizeName(operationId), true);
353✔
854
    if (this.isReservedWord(operationId)) {
353!
855
      const newOperationId = DefaultCodegen.camelize(
×
856
        'call_' + operationId,
857
        true,
858
      );
859
      log.warn(
×
860
        `${operationId} (reserved word) cannot be used as method name. Renamed to ${newOperationId}`,
861
      );
862
      return newOperationId;
×
863
    }
864
    return operationId;
353✔
865
  }
866

867
  public fromOperation(pp, httpMethod, operation, definitions, swagger) {
868
    if (arguments.length > 4) {
353✔
869
      const op = super.fromOperation(
214✔
870
        pp,
871
        httpMethod,
872
        operation,
873
        definitions,
874
        swagger,
875
      );
876
      if (op.returnType != null) {
214✔
877
        op.returnType = this.normalizeType(op.returnType);
101✔
878
      }
879
      op.path = this.sanitizePath(op.path);
214✔
880
      let lastRequired = null;
214✔
881
      let lastOptional = null;
214✔
882
      for (const p of op.allParams) {
214✔
883
        if (p.required) {
260✔
884
          lastRequired = p;
151✔
885
        } else {
886
          lastOptional = p;
109✔
887
        }
888
      }
889
      for (const p of op.allParams) {
214✔
890
        if (p === lastRequired) {
260✔
891
          p._hasMoreRequired = false;
132✔
892
        } else if (p === lastOptional) {
128✔
893
          p._hasMoreOptional = false;
79✔
894
        } else {
895
          p._hasMoreRequired = true;
49✔
896
          p._hasMoreOptional = true;
49✔
897
        }
898
      }
899
      op._hasRequiredParams = lastRequired != null;
214✔
900
      return op;
214✔
901
    }
902
    return super.fromOperation(pp, httpMethod, operation, definitions);
139✔
903
  }
904

905
  public fromModel(name, model, allDefinitions) {
906
    if (arguments.length > 2) {
241✔
907
      let codegenModel = super.fromModel(name, model, allDefinitions);
223✔
908
      if (
223!
909
        allDefinitions != null &&
669!
910
        codegenModel != null &&
911
        codegenModel.parent != null &&
912
        codegenModel.hasEnums
913
      ) {
914
        const parentModel = allDefinitions.get(codegenModel.parentSchema);
×
915
        const parentCodegenModel = super.fromModel(
×
916
          codegenModel.parent,
917
          parentModel,
918
          allDefinitions,
919
        );
920
        codegenModel = JavascriptClientCodegen.reconcileInlineEnums(
×
921
          codegenModel,
922
          parentCodegenModel,
923
        );
924
      }
925
      if (model != null && model instanceof ArrayModel) {
223!
926
        const am = model;
×
927
        if (am.getItems() != null) {
×
928
          codegenModel._isArray = true;
×
929
          codegenModel._itemType = this.getSwaggerType(am.getItems());
×
930
        }
931
      } else if (model != null && model instanceof ModelImpl) {
223✔
932
        const mm = model;
211✔
933
        if (mm.getAdditionalProperties() != null) {
211!
934
          codegenModel._isMap = true;
×
935
          codegenModel._itemType = this.getSwaggerType(
×
936
            mm.getAdditionalProperties(),
937
          );
938
        }
939
      }
940
      return codegenModel;
223✔
941
    } else {
942
      return super.fromModel(name, model);
18✔
943
    }
944
  }
945

946
  public sanitizePath(p) {
947
    return p.replace(new RegExp("'", 'g'), '%27');
214✔
948
  }
949

950
  public trimBrackets(s) {
951
    if (s != null) {
1,248✔
952
      const beginIdx = s[0] === '[' ? 1 : 0;
1,058✔
953
      let endIdx = s.length;
1,058✔
954
      if (s[endIdx - 1] === ']') {
1,058✔
955
        endIdx--;
103✔
956
      }
957
      return s.substring(beginIdx, endIdx);
1,058✔
958
    }
959
    return null;
190✔
960
  }
961

962
  public getModelledType(dataType) {
963
    return (
275✔
964
      'module:' +
965
      (isEmpty(this.invokerPackage) ? '' : this.invokerPackage + '/') +
275!
966
      (isEmpty(this.__modelPackage) ? '' : this.__modelPackage + '/') +
275!
967
      dataType
968
    );
969
  }
970

971
  public getJSDocType(cm, cp?: any) {
972
    if (
1,276✔
973
      ((cm != null && cm instanceof CodegenModel) || cm === null) &&
4,322!
974
      ((cp != null && cp instanceof CodegenProperty) || cp === null)
975
    ) {
976
      if (cp.isContainer) {
494✔
977
        if (cp.containerType === 'array') {
28!
978
          return 'Array.<' + this.getJSDocType(cm, cp.items) + '>';
28✔
979
        } else if (cp.containerType === 'map') {
×
980
          return 'Object.<String, ' + this.getJSDocType(cm, cp.items) + '>';
×
981
        }
982
      }
983
      let dataType = this.trimBrackets(cp.datatypeWithEnum);
466✔
984
      if (cp.isEnum) {
466✔
985
        dataType = cm.classname + '.' + dataType;
24✔
986
      }
987
      if (this.isModelledType(cp)) {
466✔
988
        dataType = this.getModelledType(dataType);
52✔
989
      }
990
      return dataType;
466✔
991
    } else if (
782✔
992
      ((cm != null && cm instanceof CodegenParameter) || cm === null) &&
2,346✔
993
      cp === undefined
994
    ) {
995
      let dataType = this.trimBrackets(cm.dataType);
429✔
996
      if (this.isModelledType(cm)) {
429✔
997
        dataType = this.getModelledType(dataType);
106✔
998
      }
999
      if (cm.isListContainer) {
429✔
1000
        return 'Array.<' + dataType + '>';
57✔
1001
      } else if (cm.isMapContainer) {
372!
1002
        return 'Object.<String, ' + dataType + '>';
×
1003
      }
1004
      return dataType;
372✔
1005
    } else if (
353!
1006
      ((cm != null && cm instanceof CodegenOperation) || cm === null) &&
1,059!
1007
      cp === undefined
1008
    ) {
1009
      let returnType = this.trimBrackets(cm.returnType);
353✔
1010
      if (returnType != null) {
353✔
1011
        if (this.isModelledType(cm)) {
163✔
1012
          returnType = this.getModelledType(returnType);
117✔
1013
        }
1014
        if (cm.isListContainer) {
163✔
1015
          return 'Array.<' + returnType + '>';
46✔
1016
        } else if (cm.isMapContainer) {
117✔
1017
          return 'Object.<String, ' + returnType + '>';
12✔
1018
        }
1019
      }
1020
      return returnType;
295✔
1021
    } else {
1022
      throw new Error('invalid overload');
×
1023
    }
1024
  }
1025

1026
  public isModelledType(cp) {
1027
    if ((cp != null && cp instanceof CodegenProperty) || cp === null) {
1,058✔
1028
      return (
466✔
1029
        cp.isEnum ||
908✔
1030
        !this.__languageSpecificPrimitives.contains(
1031
          cp.baseType == null ? cp.datatype : cp.baseType,
442!
1032
        )
1033
      );
1034
    } else if ((cp != null && cp instanceof CodegenParameter) || cp === null) {
592✔
1035
      return (
429✔
1036
        cp.isEnum ||
846✔
1037
        !this.__languageSpecificPrimitives.contains(
1038
          cp.baseType == null ? cp.dataType : cp.baseType,
417✔
1039
        )
1040
      );
1041
    } else if ((cp != null && cp instanceof CodegenOperation) || cp === null) {
163!
1042
      return !cp.returnTypeIsPrimitive;
163✔
1043
    }
1044
    throw new Error('invalid overload');
×
1045
  }
1046

1047
  public postProcessOperations(objs) {
1048
    const operations = objs.get('operations');
73✔
1049
    if (operations != null) {
73!
1050
      const ops = operations.get('operation');
73✔
1051
      for (const operation of ops) {
73✔
1052
        const argList: any[] = [];
353✔
1053
        let hasOptionalParams = false;
353✔
1054
        for (const p of operation.allParams) {
353✔
1055
          if (p.required) {
429✔
1056
            let flowsify = p.paramName;
241✔
1057
            const swaggerType = p.dataType;
241✔
1058
            if (this._swaggerToFlowMapping.containsKey(swaggerType)) {
241✔
1059
              flowsify += `: ${this._swaggerToFlowMapping.get(swaggerType)}`;
198✔
1060
            } else {
1061
              flowsify += ': any';
43✔
1062
            }
1063
            argList.push(flowsify);
241✔
1064
          } else {
1065
            hasOptionalParams = true;
188✔
1066
          }
1067
        }
1068
        if (hasOptionalParams) {
353✔
1069
          argList.push('opts: any');
138✔
1070
        }
1071
        if (!this.usePromises) {
353✔
1072
          argList.push('callback: Function');
94✔
1073
        }
1074
        operation._argList = argList.join(', ');
353✔
1075
        for (const cp of operation.allParams) {
353✔
1076
          cp._jsDocType = this.getJSDocType(cp);
429✔
1077
        }
1078
        operation.hasOptionalParams = hasOptionalParams;
353✔
1079
        operation.hasParams = operation.allParams.length > 0;
353✔
1080
        operation._jsDocType = this.getJSDocType(operation);
353✔
1081
      }
1082
    }
1083
    Collections.sort(objs.get('imports'), (a, b) => {
73✔
1084
      const a1 = a.get('import');
29✔
1085
      const b1 = b.get('import');
29✔
1086
      return a1.localeCompare(b1);
29✔
1087
    });
1088
    return objs;
73✔
1089
  }
1090

1091
  public postProcessModels(objs) {
1092
    objs = super.postProcessModelsEnum(objs);
106✔
1093
    const models = objs.get('models');
106✔
1094
    for (const mo of models) {
106✔
1095
      const cm = mo.get('model');
106✔
1096
      const required: any[] = [];
106✔
1097
      const allRequired = this.supportsInheritance ? [] : required;
106!
1098
      cm._required = required;
106✔
1099

1100
      cm._allRequired = allRequired;
106✔
1101

1102
      for (const __var of cm.vars) {
106✔
1103
        const jsDocType = this.getJSDocType(cm, __var);
466✔
1104
        __var._jsDocType = jsDocType;
466✔
1105

1106
        if (__var.required) {
466✔
1107
          required.push(__var);
53✔
1108
        }
1109
      }
1110
      if (this.supportsInheritance) {
106!
1111
        for (const vars of cm.allVars) {
106✔
1112
          if (vars.required) {
472✔
1113
            allRequired.push(vars);
56✔
1114
          }
1115
        }
1116
      }
1117
      let lastRequired = null;
106✔
1118
      for (const vars of cm.vars) {
106✔
1119
        if (vars.required != null && vars.required) {
466✔
1120
          lastRequired = vars;
53✔
1121
        }
1122
      }
1123
      for (const vars of cm.vars) {
106✔
1124
        if (vars === lastRequired) {
466✔
1125
          vars._hasMoreRequired = false;
31✔
1126
        } else if (vars.required != null && vars.required) {
435✔
1127
          vars._hasMoreRequired = true;
22✔
1128
        }
1129
      }
1130
    }
1131
    return objs;
106✔
1132
  }
1133

1134
  public needToImport(type) {
1135
    return (
9,023✔
1136
      !this.__defaultIncludes.contains(type) &&
9,474✔
1137
      !this.__languageSpecificPrimitives.contains(type)
1138
    );
1139
  }
1140

1141
  public staticsanitizePackageName(packageName) {
1142
    packageName = packageName.trim();
×
1143
    packageName = packageName.replace(new RegExp('[^a-zA-Z0-9_\\.]', 'g'), '_');
×
1144
    if (isEmpty(packageName)) {
×
1145
      return 'invalidPackageName';
×
1146
    }
1147
    return packageName;
×
1148
  }
1149

1150
  public toEnumName(property) {
1151
    return this.sanitizeName(DefaultCodegen.camelize(property.name)) + 'Enum';
264✔
1152
  }
1153

1154
  public toEnumVarName(value, datatype) {
1155
    return value;
108✔
1156
  }
1157

1158
  public toEnumValue(value, datatype) {
1159
    if ('Integer' === datatype || 'Number' === datatype) {
108!
1160
      return value;
×
1161
    } else {
1162
      return '"' + this.escapeText(value) + '"';
108✔
1163
    }
1164
  }
1165

1166
  public escapeQuotationMark(input) {
1167
    return input && input.split('"').join('').split("'").join('');
726✔
1168
  }
1169

1170
  public escapeUnsafeCharacters(input) {
1171
    return input && input.split('*/').join('*_/').split('/*').join('/_*');
3,666✔
1172
  }
1173
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc