From 232836c6d6c6e3a5ccd6bf7e101d4e17b909c70e Mon Sep 17 00:00:00 2001 From: gvensan <4477169+gvensan@users.noreply.github.com> Date: Wed, 8 Jan 2025 13:43:55 +0530 Subject: [PATCH 1/2] Fix for issue-346 --- filters/all.js | 5 +++++ lib/scsLib.js | 12 ++++++++++++ package.json | 2 +- partials/all-args-constructor | 6 ++++++ partials/java-class | 14 ++++++++++++-- test/__snapshots__/integration.test.js.snap | 17 ++++++++--------- 6 files changed, 44 insertions(+), 12 deletions(-) diff --git a/filters/all.js b/filters/all.js index 03344f79..54a2011d 100644 --- a/filters/all.js +++ b/filters/all.js @@ -295,6 +295,11 @@ function identifierName(str) { } filter.identifierName = identifierName; +function methodName(str) { + return scsLib.getMethodName(str); +} +filter.methodName = methodName; + function indent1(numTabs) { return indent(numTabs); } diff --git a/lib/scsLib.js b/lib/scsLib.js index 2e84d12e..7da23bb7 100644 --- a/lib/scsLib.js +++ b/lib/scsLib.js @@ -27,6 +27,18 @@ class ScsLib { return ret; } + // This returns a valid method name fro an identifier. + getMethodName(name) { + let ret = _.camelCase(name); + ret = _.upperFirst(ret); + + if (ScsLib.javaKeywords.has(ret)) { + ret = `_${ret}`; + } + + return ret; + } + // This returns the value of a param, or specification extension if the param isn't set. // If neither is set and the required flag is true, it throws an error. getParamOrExtension(info, params, paramName, extensionName, description, example, required) { diff --git a/package.json b/package.json index 2812f303..9c5aefd5 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "jest": "^27.0.4" }, "generator": { - "generator": ">=1.8.6 <=2.3.0", + "generator": ">=1.8.6 <=2.6.0", "parameters": { "actuator": { "description": "If present, it adds the dependencies for spring-boot-starter-web, spring-boot-starter-actuator and micrometer-registry-prometheus.", diff --git a/partials/all-args-constructor b/partials/all-args-constructor index e0ee40d5..d7f202b0 100644 --- a/partials/all-args-constructor +++ b/partials/all-args-constructor @@ -10,6 +10,9 @@ {%- set propModelClass = {schema: prop, schemaName: name} | getModelClass %} {%- set realClassName = propModelClass.getClassName() %} {%- set variableName = realClassName | identifierName %} +{%- if name != variableName -%} +{%- set variableName = name | identifierName -%} +{%- endif -%} {%- set typeInfo = [name, realClassName, prop] | fixType %} {%- set type = typeInfo[0] -%} {%- if first -%} @@ -23,6 +26,9 @@ {%- set propModelClass = {schema: prop, schemaName: name} | getModelClass %} {%- set realClassName = propModelClass.getClassName() %} {%- set variableName = realClassName | identifierName -%} +{%- if name != variableName -%} +{%- set variableName = name | identifierName -%} +{%- endif -%} {{ indent3 }}this.{{ variableName }} = {{ variableName }}; {% endfor -%} {{ indent2 }}} diff --git a/partials/java-class b/partials/java-class index ba5e968f..9f11b482 100644 --- a/partials/java-class +++ b/partials/java-class @@ -31,6 +31,9 @@ {%- set propModelClass = {schema: prop, schemaName: name} | getModelClass %} {%- set realClassName = propModelClass.getClassName() %} {%- set variableName = realClassName | identifierName -%} +{%- if name != variableName -%} +{%- set variableName = name | identifierName -%} +{%- endif -%} {%- set typeInfo = [realClassName, realClassName, prop] | fixType -%} {%- set type = typeInfo[0] %} {% if variableName !== name -%} @@ -43,15 +46,19 @@ {%- for name, prop in properties %} {%- set propModelClass = {schema: prop, schemaName: name} | getModelClass %} {%- set realClassName = propModelClass.getClassName() %} +{%- set realMethodName = name | methodName -%} {%- set variableName = realClassName | identifierName -%} +{%- if name != variableName -%} +{%- set variableName = name | identifierName -%} +{%- endif -%} {%- set typeInfo = [name, realClassName, prop] | fixType -%} {% set type = typeInfo[0] -%} {% set isArrayOfObjects = typeInfo[1] %} -{{ indent2 }}public {{ type }} get{{- realClassName }}() { +{{ indent2 }}public {{ type }} get{{- realMethodName }}() { {{ indent3 }}return {{ variableName }}; {{ indent2 }}} -{{ indent2 }}public {{ className }} set{{- realClassName }}({{ type }} {{ variableName }}) { +{{ indent2 }}public {{ className }} set{{- realMethodName }}({{ type }} {{ variableName }}) { {{ indent3 }}this.{{-variableName }} = {{ variableName }}; {{ indent3 }}return this; {{ indent2 }}} @@ -75,6 +82,9 @@ {%- set propModelClass = {schema: prop, schemaName: name} | getModelClass %} {%- set realClassName = propModelClass.getClassName() %} {%- set variableName = realClassName | identifierName %} +{%- if name != variableName -%} +{%- set variableName = name | identifierName -%} +{%- endif -%} {%- set typeInfo = [realClassName, realClassName, prop] | fixType -%} {%- set type = typeInfo[0] %} {{ indent3 }}+ " {{ variableName }}: " + {{ variableName }} diff --git a/test/__snapshots__/integration.test.js.snap b/test/__snapshots__/integration.test.js.snap index 2bb74dff..67bc59dd 100644 --- a/test/__snapshots__/integration.test.js.snap +++ b/test/__snapshots__/integration.test.js.snap @@ -1007,24 +1007,23 @@ public class SentAt { } public SentAt ( - SubObject subObject) { - this.subObject = subObject; + SubObject propertySubobject) { + this.propertySubobject = propertySubobject; } - @JsonProperty(\\"propertySubobject\\") - private SubObject subObject; - public SubObject getSubObject() { - return subObject; + private SubObject propertySubobject; + public SubObject getPropertySubobject() { + return propertySubobject; } - public SentAt setSubObject(SubObject subObject) { - this.subObject = subObject; + public SentAt setPropertySubobject(SubObject propertySubobject) { + this.propertySubobject = propertySubobject; return this; } public String toString() { return \\"SentAt [\\" - + \\" subObject: \\" + subObject + + \\" propertySubobject: \\" + propertySubobject + \\" ]\\"; } } From 0b7bb6be30a62959e65bf811da482575569af95e Mon Sep 17 00:00:00 2001 From: gvensan <4477169+gvensan@users.noreply.github.com> Date: Wed, 8 Jan 2025 14:49:44 +0530 Subject: [PATCH 2/2] Fixing enum delaration statement (in its own line as a statement) --- partials/java-class | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/partials/java-class b/partials/java-class index 9f11b482..bc62b72e 100644 --- a/partials/java-class +++ b/partials/java-class @@ -72,9 +72,12 @@ {%- endif %} {# Enums #} {%- if prop.enum() %} -{{ indent2 }}public static enum {{ type }} { {{ prop.enum() }} } +{{ indent2 }}public static enum {{ type }} { +{{ indent3 }} {{ prop.enum() }} +{{ indent2 }}}; {%- endif %} {%- endfor -%} +{{ indent2 }} {{ indent2 }}public String toString() { {{ indent3 }}return "{{ className }} ["