@@ -6,7 +6,16 @@ import SwaggerParser from "@apidevtools/swagger-parser";
6
6
import { parse } from "ts-command-line-args";
7
7
import * as ejs from "ejs";
8
8
import * as path from "path";
9
- import * as _ from "lodash";
9
+ import _get from "lodash/get";
10
+ import _set from "lodash/set";
11
+ import _trim from "lodash/trim";
12
+ import _upperFirst from "lodash/upperFirst";
13
+ import _camelCase from "lodash/camelCase";
14
+ import _snakeCase from "lodash/snakeCase";
15
+ import _kebabCase from "lodash/kebabCase";
16
+ import _orderBy from "lodash/orderBy";
17
+ import _uniq from "lodash/uniq";
18
+ import _uniqBy from "lodash/uniqBy";
10
19
import { OpenAPIV3 } from "openapi-types";
11
20
import * as parseOpenapi from "parse-openapi";
12
21
import { getOperationResponses } from "parse-openapi/dist/parser/getOperationResponses";
@@ -79,7 +88,7 @@ const splitRef = (ref: string): string[] =>
79
88
*/
80
89
const resolveRef = (spec: OpenAPIV3.Document, ref: string): any => {
81
90
const refParts = splitRef(ref);
82
- const resolved = _.get (spec, refParts);
91
+ const resolved = _get (spec, refParts);
83
92
if (!resolved) {
84
93
throw new Error(`Unable to resolve ref ${ref} in spec`);
85
94
}
@@ -236,12 +245,12 @@ const toTypeScriptType = (property: parseOpenapi.Model): string => {
236
245
* Mutates the given model to add language specific types and names
237
246
*/
238
247
const mutateModelWithAdditionalTypes = (model: parseOpenapi.Model) => {
239
- (model as any).typescriptName = _.camelCase( model.name) ;
248
+ (model as any).typescriptName = model.name;
240
249
(model as any).typescriptType = toTypeScriptType(model);
241
250
(model as any).isPrimitive = PRIMITIVE_TYPES.has(model.type);
242
251
243
252
// Trim any surrounding quotes from name
244
- model.name = _.trim (model.name, `"'`);
253
+ model.name = _trim (model.name, `"'`);
245
254
};
246
255
247
256
const mutateWithOpenapiSchemaProperties = (spec: OpenAPIV3.Document, model: parseOpenapi.Model, schema: OpenAPIV3.SchemaObject, visited: Set<parseOpenapi.Model> = new Set()) => {
@@ -292,7 +301,7 @@ const hoistInlineObjectSubSchemas = (nameParts: string[], schema: OpenAPIV3.Sche
292
301
293
302
// Clone the object subschemas to build the refs
294
303
const refs = inlineSubSchemas.filter(s => s.schema.type === "object" && ["items", "additionalProperties"].includes(s.prop)).map(s => {
295
- const name = s.nameParts.map(_.upperFirst ).join('');
304
+ const name = s.nameParts.map(_upperFirst ).join('');
296
305
const $ref = `#/components/schemas/${name}`;
297
306
const ref = {
298
307
$ref,
@@ -301,7 +310,7 @@ const hoistInlineObjectSubSchemas = (nameParts: string[], schema: OpenAPIV3.Sche
301
310
};
302
311
303
312
// Replace each subschema with a ref in the spec
304
- _.set (schema, s.prop, { $ref });
313
+ _set (schema, s.prop, { $ref });
305
314
306
315
return ref;
307
316
});
@@ -341,7 +350,7 @@ const buildData = (inSpec: OpenAPIV3.Document, metadata: any) => {
341
350
const response = resolveIfRef(spec, res);
342
351
const jsonResponseSchema = response?.content?.['application/json']?.schema;
343
352
if (jsonResponseSchema && !isRef(jsonResponseSchema) && ["object", "array"].includes(jsonResponseSchema.type!)) {
344
- const schemaName = `${_.upperFirst(_.camelCase (operation.operationId ?? `${path}-${method}`))}${code}Response`;
353
+ const schemaName = `${_upperFirst(_camelCase (operation.operationId ?? `${path}-${method}`))}${code}Response`;
345
354
spec.components!.schemas![schemaName] = jsonResponseSchema;
346
355
response!.content!['application/json'].schema = {
347
356
$ref: `#/components/schemas/${schemaName}`,
@@ -413,7 +422,7 @@ const buildData = (inSpec: OpenAPIV3.Document, metadata: any) => {
413
422
// If the operation didn't specify an operationId, we need to generate one in a backwards compatible way
414
423
// which matches openapi generator
415
424
if (!specOp.operationId) {
416
- (op as any).name = _.camelCase (`${op.path.replace(/{(.*?)}/g, 'by-$1').replace(/[/:]/g, '-')}-${op.method}`);
425
+ (op as any).name = _camelCase (`${op.path.replace(/{(.*?)}/g, 'by-$1').replace(/[/:]/g, '-')}-${op.method}`);
417
426
}
418
427
}
419
428
@@ -434,7 +443,7 @@ const buildData = (inSpec: OpenAPIV3.Document, metadata: any) => {
434
443
435
444
if (parameter.in === "body") {
436
445
// Parameter name for the body is it's type in camelCase
437
- parameter.name = parameter.export === "reference" ? _.camelCase (parameter.type) : "body";
446
+ parameter.name = parameter.export === "reference" ? _camelCase (parameter.type) : "body";
438
447
439
448
// The request body is not in the "parameters" section of the openapi spec so we won't have added the schema
440
449
// properties above. Find it here.
@@ -469,16 +478,16 @@ const buildData = (inSpec: OpenAPIV3.Document, metadata: any) => {
469
478
[...((op as any).responses ?? []), ...op.results].forEach(mutateModelWithAdditionalTypes);
470
479
471
480
// Add variants of operation name
472
- (op as any).operationIdPascalCase = _.upperFirst (op.name);
473
- (op as any).operationIdKebabCase = _.kebabCase (op.name);
474
- (op as any).operationIdSnakeCase = _.snakeCase (op.name);
481
+ (op as any).operationIdPascalCase = _upperFirst (op.name);
482
+ (op as any).operationIdKebabCase = _kebabCase (op.name);
483
+ (op as any).operationIdSnakeCase = _snakeCase (op.name);
475
484
});
476
485
477
486
// Lexicographical ordering of operations to match openapi generator
478
- service.operations = _.orderBy (service.operations, (op) => op.name);
487
+ service.operations = _orderBy (service.operations, (op) => op.name);
479
488
480
489
// Add the models to import
481
- (service as any).modelImports = _.orderBy(_.uniq ([...service.imports, ...responseModelImports]));
490
+ (service as any).modelImports = _orderBy(_uniq ([...service.imports, ...responseModelImports]));
482
491
483
492
// Add the service class name
484
493
(service as any).className = `${service.name}Api`;
@@ -492,7 +501,7 @@ const buildData = (inSpec: OpenAPIV3.Document, metadata: any) => {
492
501
const specModel = isRef(matchingSpecModel) ? resolveRef(spec, matchingSpecModel.$ref) as OpenAPIV3.SchemaObject : matchingSpecModel;
493
502
494
503
// Add unique imports
495
- (model as any).uniqueImports = _.orderBy(_.uniq (model.imports));
504
+ (model as any).uniqueImports = _orderBy(_uniq (model.imports));
496
505
497
506
// Add deprecated flag if present
498
507
(model as any).deprecated = specModel.deprecated || false;
@@ -521,13 +530,13 @@ const buildData = (inSpec: OpenAPIV3.Document, metadata: any) => {
521
530
});
522
531
523
532
// Order models lexicographically by name
524
- data.models = _.orderBy (data.models, d => d.name);
533
+ data.models = _orderBy (data.models, d => d.name);
525
534
526
535
// Order services so default appears first, then otherwise by name
527
- data.services = _.orderBy (data.services, (s => s.name === "Default" ? "" : s.name));
536
+ data.services = _orderBy (data.services, (s => s.name === "Default" ? "" : s.name));
528
537
529
538
// All operations across all services
530
- const allOperations = _.uniqBy (data.services.flatMap(s => s.operations), o => o.name);
539
+ const allOperations = _uniqBy (data.services.flatMap(s => s.operations), o => o.name);
531
540
532
541
return {
533
542
...data,
0 commit comments