Skip to content

Commit 44f482f

Browse files
andrewemerywing328
authored andcommitted
Fixes invalid Kotlin client variable names for reserved words (#3993)
The Kotlin Multiplatform client introduced an bug that rendered variable names that were also Kotlin reserved words incorrectly. This change reverts the template to use the previous mechanism for rendering variable names. #3992
1 parent c8ac41c commit 44f482f

4 files changed

Lines changed: 3 additions & 72 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,17 @@
1919

2020
import org.openapitools.codegen.CliOption;
2121
import org.openapitools.codegen.CodegenConstants;
22-
import org.openapitools.codegen.CodegenModel;
2322
import org.openapitools.codegen.CodegenOperation;
24-
import org.openapitools.codegen.CodegenProperty;
2523
import org.openapitools.codegen.CodegenType;
2624
import org.openapitools.codegen.SupportingFile;
2725

2826
import java.io.File;
2927
import java.util.HashMap;
30-
import java.util.HashSet;
31-
import java.util.Iterator;
3228
import java.util.List;
3329
import java.util.Map;
34-
import java.util.Set;
35-
import java.util.regex.Pattern;
3630

3731
public class KotlinClientCodegen extends AbstractKotlinCodegen {
3832

39-
protected static final String VENDOR_EXTENSION_ESCAPED_NAME = "x-escapedName";
40-
4133
protected static final String JVM = "jvm";
4234
protected static final String MULTIPLATFORM = "multiplatform";
4335

@@ -47,14 +39,6 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
4739
protected String dateLibrary = DateLibrary.JAVA8.value;
4840
protected String collectionType = CollectionType.ARRAY.value;
4941

50-
// https://kotlinlang.org/docs/reference/grammar.html#Identifier
51-
protected static final Pattern IDENTIFIER_PATTERN =
52-
Pattern.compile("[\\p{Ll}\\p{Lm}\\p{Lo}\\p{Lt}\\p{Lu}\\p{Nl}_][\\p{Ll}\\p{Lm}\\p{Lo}\\p{Lt}\\p{Lu}\\p{Nl}\\p{Nd}_]*");
53-
54-
// https://kotlinlang.org/docs/reference/grammar.html#Identifier
55-
protected static final String IDENTIFIER_REPLACEMENTS =
56-
"[.;:/\\[\\]<>]";
57-
5842
public enum DateLibrary {
5943
STRING("string"),
6044
THREETENBP("threetenbp"),
@@ -212,7 +196,6 @@ public void processOpts() {
212196
supportingFiles.add(new SupportingFile("infrastructure/HttpResponse.kt.mustache", infrastructureFolder, "HttpResponse.kt"));
213197

214198
// multiplatform specific testing files
215-
final String testFolder = (sourceFolder + File.separator + packageName + File.separator + "infrastructure").replace(".", "/");
216199
supportingFiles.add(new SupportingFile("commonTest/coroutine.mustache", "src/commonTest/kotlin/util", "Coroutine.kt"));
217200
supportingFiles.add(new SupportingFile("iosTest/coroutine.mustache", "src/iosTest/kotlin/util", "Coroutine.kt"));
218201
supportingFiles.add(new SupportingFile("jvmTest/coroutine.mustache", "src/jvmTest/kotlin/util", "Coroutine.kt"));
@@ -253,58 +236,6 @@ public void processOpts() {
253236
}
254237
}
255238

256-
@Override
257-
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
258-
objs = super.postProcessModels(objs);
259-
return postProcessModelsEscapeNames(objs);
260-
}
261-
262-
@SuppressWarnings("unchecked")
263-
private static Map<String, Object> postProcessModelsEscapeNames(Map<String, Object> objs) {
264-
List<Object> models = (List<Object>) objs.get("models");
265-
for (Object _mo : models) {
266-
Map<String, Object> mo = (Map<String, Object>) _mo;
267-
CodegenModel cm = (CodegenModel) mo.get("model");
268-
269-
if (cm.vars != null) {
270-
for (CodegenProperty var : cm.vars) {
271-
var.vendorExtensions.put(VENDOR_EXTENSION_ESCAPED_NAME, escapeIdentifier(var.name));
272-
}
273-
}
274-
if (cm.requiredVars != null) {
275-
for (CodegenProperty var : cm.requiredVars) {
276-
var.vendorExtensions.put(VENDOR_EXTENSION_ESCAPED_NAME, escapeIdentifier(var.name));
277-
}
278-
}
279-
if (cm.optionalVars != null) {
280-
for (CodegenProperty var : cm.optionalVars) {
281-
var.vendorExtensions.put(VENDOR_EXTENSION_ESCAPED_NAME, escapeIdentifier(var.name));
282-
}
283-
}
284-
}
285-
return objs;
286-
}
287-
288-
private static String escapeIdentifier(String identifier) {
289-
290-
// the kotlin grammar permits a wider set of characters in their identifiers that all target
291-
// platforms permit (namely jvm). in order to remain compatible with target platforms, we
292-
// initially replace all illegal target characters before escaping the identifier if required.
293-
identifier = identifier.replaceAll(IDENTIFIER_REPLACEMENTS, "_");
294-
if (IDENTIFIER_PATTERN.matcher(identifier).matches()) return identifier;
295-
return '`' + identifier + '`';
296-
}
297-
298-
private static void removeDuplicates(List<CodegenProperty> list) {
299-
Set<String> set = new HashSet<>();
300-
Iterator<CodegenProperty> iterator = list.iterator();
301-
while (iterator.hasNext()) {
302-
CodegenProperty item = iterator.next();
303-
if (set.contains(item.name)) iterator.remove();
304-
else set.add(item.name);
305-
}
306-
}
307-
308239
@Override
309240
@SuppressWarnings("unchecked")
310241
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {

modules/openapi-generator/src/main/resources/kotlin-client/data_class.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import kotlinx.serialization.internal.CommonEnumSerializer
1818
/**
1919
* {{{description}}}
2020
{{#vars}}
21-
* @param {{{vendorExtensions.x-escapedName}}} {{{description}}}
21+
* @param {{{name}}} {{{description}}}
2222
{{/vars}}
2323
*/
2424
{{#parcelizeModels}}

modules/openapi-generator/src/main/resources/kotlin-client/data_class_opt_var.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
@SerializedName("{{name}}")
1010
{{/gson}}
1111
{{/jvm}}
12-
{{#multiplatform}}@SerialName(value = "{{name}}") {{/multiplatform}}val {{{vendorExtensions.x-escapedName}}}: {{#isEnum}}{{#isListContainer}}{{#isList}}kotlin.collections.List{{/isList}}{{^isList}}kotlin.Array{{/isList}}<{{classname}}.{{{nameInCamelCase}}}>{{/isListContainer}}{{^isListContainer}}{{classname}}.{{{nameInCamelCase}}}{{/isListContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}
12+
{{#multiplatform}}@SerialName(value = "{{baseName}}") {{/multiplatform}}val {{{name}}}: {{#isEnum}}{{#isListContainer}}{{#isList}}kotlin.collections.List{{/isList}}{{^isList}}kotlin.Array{{/isList}}<{{classname}}.{{{nameInCamelCase}}}>{{/isListContainer}}{{^isListContainer}}{{classname}}.{{{nameInCamelCase}}}{{/isListContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}

modules/openapi-generator/src/main/resources/kotlin-client/data_class_req_var.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
@SerializedName("{{name}}")
1010
{{/gson}}
1111
{{/jvm}}
12-
{{#multiplatform}}@SerialName(value = "{{name}}") @Required {{/multiplatform}}val {{{vendorExtensions.x-escapedName}}}: {{#isEnum}}{{#isListContainer}}{{#isList}}kotlin.collections.List{{/isList}}{{^isList}}kotlin.Array{{/isList}}<{{classname}}.{{{nameInCamelCase}}}>{{/isListContainer}}{{^isListContainer}}{{classname}}.{{{nameInCamelCase}}}{{/isListContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}
12+
{{#multiplatform}}@SerialName(value = "{{baseName}}") @Required {{/multiplatform}}val {{{name}}}: {{#isEnum}}{{#isListContainer}}{{#isList}}kotlin.collections.List{{/isList}}{{^isList}}kotlin.Array{{/isList}}<{{classname}}.{{{nameInCamelCase}}}>{{/isListContainer}}{{^isListContainer}}{{classname}}.{{{nameInCamelCase}}}{{/isListContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}

0 commit comments

Comments
 (0)