Skip to content

Commit f8638a7

Browse files
committed
#29, avoid getClass() method on model classes
(cherry picked from commit ebe7411)
1 parent 9b26060 commit f8638a7

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/writer/java/DataTypeWriterPojo.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class DataTypeWriterPojo(
219219
result += ifDeprecated(propDataType)
220220

221221
result += """
222-
| public ${propDataType.getTypeName()} get${propertyName.capitalizeFirstChar()}() {
222+
| public ${propDataType.getTypeName()} get${toAccessor(propertyName.capitalizeFirstChar())}() {
223223
| return ${toIdentifier(propertyName)};
224224
| }
225225
|
@@ -236,7 +236,7 @@ class DataTypeWriterPojo(
236236
val property = toIdentifier(propertyName)
237237

238238
result += """
239-
| public void set${propertyName.capitalizeFirstChar()}(${propDataType.getTypeName()} ${property}) {
239+
| public void set${toAccessor(propertyName.capitalizeFirstChar())}(${propDataType.getTypeName()} ${property}) {
240240
| this.${property} = ${property};
241241
| }
242242
|

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/writer/java/Identifier.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ fun toIdentifier(src: String): String {
5151
return identifier
5252
}
5353

54+
/**
55+
* convert an invalid property accessor to a valid property accessor by adding an "A" prefix.
56+
* This is used to avoid generating a getClass() method that conflicts with Object::getClass().
57+
*
58+
* @param src the source property name without get/set prefix
59+
* @return a valid accessor name without get/set prefix
60+
*/
61+
fun toAccessor(src: String): String {
62+
return when (src) {
63+
"Class" -> "AClass"
64+
else -> src
65+
}
66+
}
67+
5468
/**
5569
* converts a source string to a valid (camel case) java *class* identifier. One way, ie it is
5670
* not reversible.
@@ -82,7 +96,6 @@ fun toClass(src: String): String {
8296
* All words are converted to uppercase and joined by an underscore.
8397
*
8498
* @param src the source "string"
85-
*
8699
* @return a valid upper case enum java identifier
87100
*/
88101
fun toEnum(src: String): String {

openapi-processor-core/src/testInt/resources/tests/keyword-identifier/generated/model/Class.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ public class Class {
99
@JsonProperty("class")
1010
private String aClass;
1111

12-
public String getClass() {
12+
public String getAClass() {
1313
return aClass;
1414
}
1515

16-
public void setClass(String aClass) {
16+
public void setAClass(String aClass) {
1717
this.aClass = aClass;
1818
}
1919

0 commit comments

Comments
 (0)