Skip to content

Commit 173a20a

Browse files
authored
[#173210783] generate camel cased prop names (#183)
1 parent 4dd190e commit 173a20a

File tree

5 files changed

+39
-19
lines changed

5 files changed

+39
-19
lines changed

.prettierrc

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# .prettierrc
2+
parser: typescript

src/gen-api-models.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ function renderAsync(
1313
env: nunjucks.Environment,
1414
definition: OpenAPIV2.DefinitionsObject,
1515
definitionName: string,
16-
strictInterfaces: boolean
16+
strictInterfaces: boolean,
17+
camelCasedPropNames: boolean
1718
): Promise<string> {
1819
return new Promise((accept, reject) => {
1920
env.render(
2021
"model.ts.njk",
2122
{
23+
camelCasedPropNames,
2224
definition,
2325
definitionName,
2426
strictInterfaces
@@ -37,18 +39,19 @@ export async function renderDefinitionCode(
3739
env: nunjucks.Environment,
3840
definitionName: string,
3941
definition: OpenAPIV2.DefinitionsObject,
40-
strictInterfaces: boolean
42+
strictInterfaces: boolean,
43+
camelCasedPropNames: boolean
4144
): Promise<string> {
4245
const code = await renderAsync(
4346
env,
4447
definition,
4548
definitionName,
46-
strictInterfaces
49+
strictInterfaces,
50+
camelCasedPropNames
4751
);
48-
const prettifiedCode = prettier.format(code, {
52+
return prettier.format(code, {
4953
parser: "typescript"
5054
});
51-
return prettifiedCode;
5255
}
5356

5457
function capitalize(s: string): string {
@@ -97,6 +100,7 @@ function getDecoderForResponse(status: string, type: string): string {
97100
}
98101
}
99102

103+
// tslint:disable-next-line: parameters-max-number cognitive-complexity
100104
export function renderOperation(
101105
method: string,
102106
operationId: string,
@@ -295,6 +299,7 @@ export function isOpenAPIV2(
295299
return specs.hasOwnProperty("swagger");
296300
}
297301

302+
// tslint:disable-next-line: parameters-max-number cognitive-complexity
298303
export async function generateApi(
299304
env: nunjucks.Environment,
300305
specFilePath: string | OpenAPIV2.Document,
@@ -304,7 +309,8 @@ export async function generateApi(
304309
generateRequestTypes: boolean,
305310
defaultSuccessType: string,
306311
defaultErrorType: string,
307-
generateResponseDecoders: boolean
312+
generateResponseDecoders: boolean,
313+
camelCasedPropNames: boolean
308314
): Promise<void> {
309315
const api = await SwaggerParser.bundle(specFilePath);
310316

@@ -345,7 +351,8 @@ export async function generateApi(
345351
env,
346352
definitionName,
347353
definition,
348-
strictInterfaces
354+
strictInterfaces,
355+
camelCasedPropNames
349356
);
350357
await fs.writeFile(outPath, code);
351358
}
@@ -454,6 +461,7 @@ export async function generateApi(
454461
import * as r from "italia-ts-commons/lib/requests";
455462
456463
${Array.from(operationsImports.values())
464+
// tslint:disable-next-line: no-nested-template-literals
457465
.map(i => `import { ${i} } from "./${i}";`)
458466
.join("\n\n")}
459467
@@ -504,7 +512,7 @@ export function initNunJucksEnvironment(): nunjucks.Environment {
504512
});
505513

506514
let imports: { [key: string]: true } = {};
507-
env.addFilter("resetImports", (item: string) => {
515+
env.addFilter("resetImports", (_: string) => {
508516
imports = {};
509517
});
510518
env.addFilter("addImport", (item: string) => {

src/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ const argv = yargs
5656
normalize: true,
5757
string: true
5858
})
59+
.option("camel-cased", {
60+
boolean: false,
61+
default: false,
62+
description: "Generate camelCased properties name (default: false)"
63+
})
5964
.help().argv;
6065

6166
//
@@ -72,6 +77,7 @@ generateApi(
7277
argv["request-types"],
7378
argv["default-success-type"],
7479
argv["default-error-type"],
75-
argv["response-decoders"]
80+
argv["response-decoders"],
81+
argv["camel-cased"]
7682
// tslint:disable-next-line:no-console
7783
).then(() => console.log("done"), err => console.log(`Error: ${err}`));

templates/macros.njk

+13-9
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,13 @@
198198
{##
199199
# defines an object property of some prop.type
200200
#}
201-
{% macro defineObjectProperty(propName, prop, parentPropName) -%}
201+
{% macro defineObjectProperty(propName, prop, parentPropName, camelCasedPropNames) -%}
202202
{% if propName %}
203-
"{{ propName }}":
203+
{% if camelCasedPropNames %}
204+
"{{ propName | camelCase }}":
205+
{% else %}
206+
"{{ propName }}":
207+
{% endif %}
204208
{% endif %}
205209
{%- if prop.$ref %}
206210
{{ defineRef(prop.$ref, prop, true) }}
@@ -253,7 +257,7 @@
253257
# define object properties recursively,
254258
# supports additionaProperties, allOf and oneOf.
255259
#}
256-
{% macro defineObject(definitionName, definition, strictInterfaces) -%}
260+
{% macro defineObject(definitionName, definition, strictInterfaces, camelCasedPropNames) -%}
257261
{{ importLocalProps(definition) }}
258262
{%- if definition.description %}
259263
{{ definition.description | comment | safe }}
@@ -266,7 +270,7 @@
266270
{% for propName, prop in definition.properties -%}
267271
{% if prop.type == "object" %}
268272
{% set composedPropName %}{{ definitionName }}{{ propName | capitalizeFirst }}{% endset %}
269-
{{ defineObject(composedPropName, prop, strictInterfaces) }}
273+
{{ defineObject(composedPropName, prop, strictInterfaces, camelCasedPropNames) }}
270274
{% endif %}
271275
{% endfor %}
272276

@@ -276,7 +280,7 @@
276280
{% set typedef %}t.dictionary(t.string, t.any, "{{ definitionName }}"){% endset %}
277281
{% else %}
278282
{% set typedef %}t.dictionary(t.string,
279-
{{ defineObjectProperty(false, definition.additionalProperties) }} "{{ definitionName }}")
283+
{{ defineObjectProperty(false, definition.additionalProperties, camelCasedPropNames) }} "{{ definitionName }}")
280284
{% endset %}
281285
{%- endif %}
282286
{% set defaultValue = definition.default | dump | safe if definition.default else undefined %}
@@ -286,7 +290,7 @@
286290
const {{ definitionName }}R = t.interface({
287291
{% for propName, prop in definition.properties -%}
288292
{% if definition.required and (definition.required | contains(propName)) %}
289-
{{ defineObjectProperty(propName, prop, definitionName) }}
293+
{{ defineObjectProperty(propName, prop, definitionName, camelCasedPropNames) }}
290294
{% endif %}
291295
{% endfor %}
292296
});
@@ -295,7 +299,7 @@
295299
const {{ definitionName }}O = t.partial({
296300
{% for propName, prop in definition.properties -%}
297301
{% if (not definition.required) or (definition.required and not (definition.required | contains(propName))) %}
298-
{{ defineObjectProperty(propName, prop, definitionName) }}
302+
{{ defineObjectProperty(propName, prop, definitionName, camelCasedPropNames) }}
299303
{% endif %}
300304
{% endfor %}
301305
});
@@ -318,7 +322,7 @@
318322

319323
{% for schema in definition.allOf -%}
320324
{% if schema.type == "object" %}
321-
{{ defineObject(definitionName + loop.index, schema, strictInterfaces) }}
325+
{{ defineObject(definitionName + loop.index, schema, strictInterfaces, camelCasedPropNames) }}
322326
{% elif schema.$ref %}
323327
{%- set realPropName = schema.$ref | replace("#/definitions/", "") -%}
324328
{{ importLocalProp(realPropName) }}
@@ -349,7 +353,7 @@
349353

350354
{% for schema in oneOfProps -%}
351355
{% if schema.type == "object" %}
352-
{{ defineObject(definitionName + loop.index, schema, strictInterfaces) }}
356+
{{ defineObject(definitionName + loop.index, schema, strictInterfaces, camelCasedPropNames) }}
353357
{% elif schema.$ref %}
354358
{%- set realPropName = schema.$ref | replace("#/definitions/", "") -%}
355359
{{ importLocalProp(realPropName) }}

templates/model.ts.njk

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
{{- null | resetTypeAliases -}}
1111

1212
{% set definition %}
13-
{{ macro.defineObject(definitionName, definition, strictInterfaces) }}
13+
{{ macro.defineObject(definitionName, definition, strictInterfaces, camelCasedPropNames) }}
1414
{% endset %}
1515

1616
{{ null | getImports | safe }}

0 commit comments

Comments
 (0)