Skip to content

[#173210783] generate camel cased prop names #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# .prettierrc
parser: typescript
24 changes: 16 additions & 8 deletions src/gen-api-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ function renderAsync(
env: nunjucks.Environment,
definition: OpenAPIV2.DefinitionsObject,
definitionName: string,
strictInterfaces: boolean
strictInterfaces: boolean,
camelCasedPropNames: boolean
): Promise<string> {
return new Promise((accept, reject) => {
env.render(
"model.ts.njk",
{
camelCasedPropNames,
definition,
definitionName,
strictInterfaces
Expand All @@ -37,18 +39,19 @@ export async function renderDefinitionCode(
env: nunjucks.Environment,
definitionName: string,
definition: OpenAPIV2.DefinitionsObject,
strictInterfaces: boolean
strictInterfaces: boolean,
camelCasedPropNames: boolean
): Promise<string> {
const code = await renderAsync(
env,
definition,
definitionName,
strictInterfaces
strictInterfaces,
camelCasedPropNames
);
const prettifiedCode = prettier.format(code, {
return prettier.format(code, {
parser: "typescript"
});
return prettifiedCode;
}

function capitalize(s: string): string {
Expand Down Expand Up @@ -97,6 +100,7 @@ function getDecoderForResponse(status: string, type: string): string {
}
}

// tslint:disable-next-line: parameters-max-number cognitive-complexity
export function renderOperation(
method: string,
operationId: string,
Expand Down Expand Up @@ -295,6 +299,7 @@ export function isOpenAPIV2(
return specs.hasOwnProperty("swagger");
}

// tslint:disable-next-line: parameters-max-number cognitive-complexity
export async function generateApi(
env: nunjucks.Environment,
specFilePath: string | OpenAPIV2.Document,
Expand All @@ -304,7 +309,8 @@ export async function generateApi(
generateRequestTypes: boolean,
defaultSuccessType: string,
defaultErrorType: string,
generateResponseDecoders: boolean
generateResponseDecoders: boolean,
camelCasedPropNames: boolean
): Promise<void> {
const api = await SwaggerParser.bundle(specFilePath);

Expand Down Expand Up @@ -345,7 +351,8 @@ export async function generateApi(
env,
definitionName,
definition,
strictInterfaces
strictInterfaces,
camelCasedPropNames
);
await fs.writeFile(outPath, code);
}
Expand Down Expand Up @@ -454,6 +461,7 @@ export async function generateApi(
import * as r from "italia-ts-commons/lib/requests";

${Array.from(operationsImports.values())
// tslint:disable-next-line: no-nested-template-literals
.map(i => `import { ${i} } from "./${i}";`)
.join("\n\n")}

Expand Down Expand Up @@ -504,7 +512,7 @@ export function initNunJucksEnvironment(): nunjucks.Environment {
});

let imports: { [key: string]: true } = {};
env.addFilter("resetImports", (item: string) => {
env.addFilter("resetImports", (_: string) => {
imports = {};
});
env.addFilter("addImport", (item: string) => {
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ const argv = yargs
normalize: true,
string: true
})
.option("camel-cased", {
boolean: false,
default: false,
description: "Generate camelCased properties name (default: false)"
})
.help().argv;

//
Expand All @@ -72,6 +77,7 @@ generateApi(
argv["request-types"],
argv["default-success-type"],
argv["default-error-type"],
argv["response-decoders"]
argv["response-decoders"],
argv["camel-cased"]
// tslint:disable-next-line:no-console
).then(() => console.log("done"), err => console.log(`Error: ${err}`));
22 changes: 13 additions & 9 deletions templates/macros.njk
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,13 @@
{##
# defines an object property of some prop.type
#}
{% macro defineObjectProperty(propName, prop, parentPropName) -%}
{% macro defineObjectProperty(propName, prop, parentPropName, camelCasedPropNames) -%}
{% if propName %}
"{{ propName }}":
{% if camelCasedPropNames %}
"{{ propName | camelCase }}":
{% else %}
"{{ propName }}":
{% endif %}
{% endif %}
{%- if prop.$ref %}
{{ defineRef(prop.$ref, prop, true) }}
Expand Down Expand Up @@ -253,7 +257,7 @@
# define object properties recursively,
# supports additionaProperties, allOf and oneOf.
#}
{% macro defineObject(definitionName, definition, strictInterfaces) -%}
{% macro defineObject(definitionName, definition, strictInterfaces, camelCasedPropNames) -%}
{{ importLocalProps(definition) }}
{%- if definition.description %}
{{ definition.description | comment | safe }}
Expand All @@ -266,7 +270,7 @@
{% for propName, prop in definition.properties -%}
{% if prop.type == "object" %}
{% set composedPropName %}{{ definitionName }}{{ propName | capitalizeFirst }}{% endset %}
{{ defineObject(composedPropName, prop, strictInterfaces) }}
{{ defineObject(composedPropName, prop, strictInterfaces, camelCasedPropNames) }}
{% endif %}
{% endfor %}

Expand All @@ -276,7 +280,7 @@
{% set typedef %}t.dictionary(t.string, t.any, "{{ definitionName }}"){% endset %}
{% else %}
{% set typedef %}t.dictionary(t.string,
{{ defineObjectProperty(false, definition.additionalProperties) }} "{{ definitionName }}")
{{ defineObjectProperty(false, definition.additionalProperties, camelCasedPropNames) }} "{{ definitionName }}")
{% endset %}
{%- endif %}
{% set defaultValue = definition.default | dump | safe if definition.default else undefined %}
Expand All @@ -286,7 +290,7 @@
const {{ definitionName }}R = t.interface({
{% for propName, prop in definition.properties -%}
{% if definition.required and (definition.required | contains(propName)) %}
{{ defineObjectProperty(propName, prop, definitionName) }}
{{ defineObjectProperty(propName, prop, definitionName, camelCasedPropNames) }}
{% endif %}
{% endfor %}
});
Expand All @@ -295,7 +299,7 @@
const {{ definitionName }}O = t.partial({
{% for propName, prop in definition.properties -%}
{% if (not definition.required) or (definition.required and not (definition.required | contains(propName))) %}
{{ defineObjectProperty(propName, prop, definitionName) }}
{{ defineObjectProperty(propName, prop, definitionName, camelCasedPropNames) }}
{% endif %}
{% endfor %}
});
Expand All @@ -318,7 +322,7 @@

{% for schema in definition.allOf -%}
{% if schema.type == "object" %}
{{ defineObject(definitionName + loop.index, schema, strictInterfaces) }}
{{ defineObject(definitionName + loop.index, schema, strictInterfaces, camelCasedPropNames) }}
{% elif schema.$ref %}
{%- set realPropName = schema.$ref | replace("#/definitions/", "") -%}
{{ importLocalProp(realPropName) }}
Expand Down Expand Up @@ -349,7 +353,7 @@

{% for schema in oneOfProps -%}
{% if schema.type == "object" %}
{{ defineObject(definitionName + loop.index, schema, strictInterfaces) }}
{{ defineObject(definitionName + loop.index, schema, strictInterfaces, camelCasedPropNames) }}
{% elif schema.$ref %}
{%- set realPropName = schema.$ref | replace("#/definitions/", "") -%}
{{ importLocalProp(realPropName) }}
Expand Down
2 changes: 1 addition & 1 deletion templates/model.ts.njk
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{{- null | resetTypeAliases -}}

{% set definition %}
{{ macro.defineObject(definitionName, definition, strictInterfaces) }}
{{ macro.defineObject(definitionName, definition, strictInterfaces, camelCasedPropNames) }}
{% endset %}

{{ null | getImports | safe }}
Expand Down