Skip to content

Commit 833811f

Browse files
author
Luis Fernando Planella Gonzalez
committed
Added an option to prevent model names capitalization
Fixes #319
1 parent 75a957b commit 833811f

7 files changed

+92
-5
lines changed

lib/gen-type.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export abstract class GenType {
4343
this.qualifiedName = this.typeName;
4444
if (this.namespace) {
4545
this.fileName = this.namespace + '/' + this.fileName;
46-
this.qualifiedName = typeName(this.namespace) + this.typeName;
46+
this.qualifiedName = typeName(this.namespace, options) + this.typeName;
4747
}
4848
this._imports = new Imports(options);
4949
}

lib/gen-utils.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ export function ensureNotReserved(name: string): string {
7171
/**
7272
* Returns the type (class) name for a given regular name
7373
*/
74-
export function typeName(name: string): string {
75-
return upperFirst(methodName(name));
74+
export function typeName(name: string, options?: Options): string {
75+
if (options?.camelizeModelNames === false) {
76+
return upperFirst(toBasicChars(name, true));
77+
} else {
78+
return upperFirst(methodName(name));
79+
}
7680
}
7781

7882
/**
@@ -145,14 +149,14 @@ export function tsComments(description: string | undefined, level: number, depre
145149
* Applies the prefix and suffix to a model class name
146150
*/
147151
export function modelClass(baseName: string, options: Options) {
148-
return `${options.modelPrefix || ''}${typeName(baseName)}${options.modelSuffix || ''}`;
152+
return `${options.modelPrefix || ''}${typeName(baseName, options)}${options.modelSuffix || ''}`;
149153
}
150154

151155
/**
152156
* Applies the prefix and suffix to a service class name
153157
*/
154158
export function serviceClass(baseName: string, options: Options) {
155-
return `${options.servicePrefix || ''}${typeName(baseName)}${options.serviceSuffix || 'Service'}`;
159+
return `${options.servicePrefix || ''}${typeName(baseName, options)}${options.serviceSuffix || 'Service'}`;
156160
}
157161

158162
/**

lib/options.ts

+3
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,7 @@ export interface Options {
120120

121121
/** When true, no verbose output will be displayed */
122122
silent?: boolean;
123+
124+
/** When true (default) models names will be camelized, besides having the first letter capitalized. Setting to false will prevent camelizing. */
125+
camelizeModelNames?: boolean;
123126
}

ng-openapi-gen-schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@
217217
"description": "When set to true, no verbose output will be displayed.",
218218
"default": "false",
219219
"type": "boolean"
220+
},
221+
"camelizeModelNames": {
222+
"description": "When true (default) models names will be camelized, besides having the first letter capitalized. Setting to false will prevent camelizing.",
223+
"default": "true",
224+
"type": "boolean"
220225
}
221226
}
222227
}

test/camelize-model-names.config.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"$schema": "../ng-openapi-gen-schema.json",
3+
"input": "keep-model-names.json",
4+
"output": "out/keep-model-names/",
5+
"camelizeModelNames": false,
6+
"ignoreUnusedModels": false,
7+
"modelPrefix": "Pre",
8+
"modelSuffix": "Pos"
9+
}

test/camelize-model-names.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"openapi": "3.0",
3+
"info": {
4+
"title": "Test for keepModelNames",
5+
"version": "1.0"
6+
},
7+
"paths": [],
8+
"components": {
9+
"schemas": {
10+
"snake-case": {
11+
"type": "object",
12+
"properties": {
13+
"name": {
14+
"type": "string"
15+
}
16+
}
17+
},
18+
"camelCase": {
19+
"type": "object",
20+
"properties": {
21+
"name": {
22+
"type": "string"
23+
}
24+
}
25+
}
26+
}
27+
}
28+
}

test/camelize-model-names.spec.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { OpenAPIObject } from '@loopback/openapi-v3-types';
2+
import { InterfaceDeclaration, TypescriptParser } from 'typescript-parser';
3+
import { NgOpenApiGen } from '../lib/ng-openapi-gen';
4+
import { Options } from '../lib/options';
5+
import options from './camelize-model-names.config.json';
6+
import camelizeModelNamesSpec from './camelize-model-names.json';
7+
8+
const gen = new NgOpenApiGen(camelizeModelNamesSpec as OpenAPIObject, options as Options);
9+
gen.generate();
10+
11+
describe('Generation tests using camelize-model-names.json', () => {
12+
it('snake-case model', done => {
13+
const ref = gen.models.get('snake-case');
14+
const ts = gen.templates.apply('model', ref);
15+
const parser = new TypescriptParser();
16+
parser.parseSource(ts).then(ast => {
17+
expect(ast.imports.length).toBe(0);
18+
expect(ast.declarations.length).toBe(1);
19+
expect(ast.declarations[0]).toEqual(jasmine.any(InterfaceDeclaration));
20+
const decl = ast.declarations[0] as InterfaceDeclaration;
21+
expect(decl.name).toBe('PreSnake_casePos');
22+
done();
23+
});
24+
});
25+
it('camelCase model', done => {
26+
const ref = gen.models.get('camelCase');
27+
const ts = gen.templates.apply('model', ref);
28+
const parser = new TypescriptParser();
29+
parser.parseSource(ts).then(ast => {
30+
expect(ast.imports.length).toBe(0);
31+
expect(ast.declarations.length).toBe(1);
32+
expect(ast.declarations[0]).toEqual(jasmine.any(InterfaceDeclaration));
33+
const decl = ast.declarations[0] as InterfaceDeclaration;
34+
expect(decl.name).toBe('PreCamelCasePos');
35+
done();
36+
});
37+
});
38+
});

0 commit comments

Comments
 (0)