Skip to content

Commit

Permalink
Added an option to prevent model names capitalization
Browse files Browse the repository at this point in the history
Fixes #319
  • Loading branch information
Luis Fernando Planella Gonzalez committed Sep 6, 2024
1 parent 75a957b commit 833811f
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/gen-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export abstract class GenType {
this.qualifiedName = this.typeName;
if (this.namespace) {
this.fileName = this.namespace + '/' + this.fileName;
this.qualifiedName = typeName(this.namespace) + this.typeName;
this.qualifiedName = typeName(this.namespace, options) + this.typeName;
}
this._imports = new Imports(options);
}
Expand Down
12 changes: 8 additions & 4 deletions lib/gen-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ export function ensureNotReserved(name: string): string {
/**
* Returns the type (class) name for a given regular name
*/
export function typeName(name: string): string {
return upperFirst(methodName(name));
export function typeName(name: string, options?: Options): string {
if (options?.camelizeModelNames === false) {
return upperFirst(toBasicChars(name, true));
} else {
return upperFirst(methodName(name));
}
}

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

/**
* Applies the prefix and suffix to a service class name
*/
export function serviceClass(baseName: string, options: Options) {
return `${options.servicePrefix || ''}${typeName(baseName)}${options.serviceSuffix || 'Service'}`;
return `${options.servicePrefix || ''}${typeName(baseName, options)}${options.serviceSuffix || 'Service'}`;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,7 @@ export interface Options {

/** When true, no verbose output will be displayed */
silent?: boolean;

/** When true (default) models names will be camelized, besides having the first letter capitalized. Setting to false will prevent camelizing. */
camelizeModelNames?: boolean;
}
5 changes: 5 additions & 0 deletions ng-openapi-gen-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@
"description": "When set to true, no verbose output will be displayed.",
"default": "false",
"type": "boolean"
},
"camelizeModelNames": {
"description": "When true (default) models names will be camelized, besides having the first letter capitalized. Setting to false will prevent camelizing.",
"default": "true",
"type": "boolean"
}
}
}
9 changes: 9 additions & 0 deletions test/camelize-model-names.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "../ng-openapi-gen-schema.json",
"input": "keep-model-names.json",
"output": "out/keep-model-names/",
"camelizeModelNames": false,
"ignoreUnusedModels": false,
"modelPrefix": "Pre",
"modelSuffix": "Pos"
}
28 changes: 28 additions & 0 deletions test/camelize-model-names.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"openapi": "3.0",
"info": {
"title": "Test for keepModelNames",
"version": "1.0"
},
"paths": [],
"components": {
"schemas": {
"snake-case": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
},
"camelCase": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
}
}
}
38 changes: 38 additions & 0 deletions test/camelize-model-names.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { OpenAPIObject } from '@loopback/openapi-v3-types';
import { InterfaceDeclaration, TypescriptParser } from 'typescript-parser';
import { NgOpenApiGen } from '../lib/ng-openapi-gen';
import { Options } from '../lib/options';
import options from './camelize-model-names.config.json';
import camelizeModelNamesSpec from './camelize-model-names.json';

const gen = new NgOpenApiGen(camelizeModelNamesSpec as OpenAPIObject, options as Options);
gen.generate();

describe('Generation tests using camelize-model-names.json', () => {
it('snake-case model', done => {
const ref = gen.models.get('snake-case');
const ts = gen.templates.apply('model', ref);
const parser = new TypescriptParser();
parser.parseSource(ts).then(ast => {
expect(ast.imports.length).toBe(0);
expect(ast.declarations.length).toBe(1);
expect(ast.declarations[0]).toEqual(jasmine.any(InterfaceDeclaration));
const decl = ast.declarations[0] as InterfaceDeclaration;
expect(decl.name).toBe('PreSnake_casePos');
done();
});
});
it('camelCase model', done => {
const ref = gen.models.get('camelCase');
const ts = gen.templates.apply('model', ref);
const parser = new TypescriptParser();
parser.parseSource(ts).then(ast => {
expect(ast.imports.length).toBe(0);
expect(ast.declarations.length).toBe(1);
expect(ast.declarations[0]).toEqual(jasmine.any(InterfaceDeclaration));
const decl = ast.declarations[0] as InterfaceDeclaration;
expect(decl.name).toBe('PreCamelCasePos');
done();
});
});
});

0 comments on commit 833811f

Please sign in to comment.