Skip to content

Commit 5a63762

Browse files
committed
[kotlin] Use simple class name instead of canonical names in the datatype model property (#19942)
1 parent 67af02c commit 5a63762

15 files changed

+359
-147
lines changed

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

Lines changed: 134 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,23 @@ public AbstractKotlinCodegen() {
9292
setSortModelPropertiesByRequiredFlag(true);
9393

9494
languageSpecificPrimitives = new HashSet<>(Arrays.asList(
95-
"kotlin.Byte",
96-
"kotlin.ByteArray",
97-
"kotlin.Short",
98-
"kotlin.Int",
99-
"kotlin.Long",
100-
"kotlin.Float",
101-
"kotlin.Double",
102-
"kotlin.Boolean",
103-
"kotlin.Char",
104-
"kotlin.String",
105-
"kotlin.Array",
106-
"kotlin.collections.List",
107-
"kotlin.collections.MutableList",
108-
"kotlin.collections.Map",
109-
"kotlin.collections.MutableMap",
110-
"kotlin.collections.Set",
111-
"kotlin.collections.MutableSet"
95+
"Byte",
96+
"ByteArray",
97+
"Short",
98+
"Int",
99+
"Long",
100+
"Float",
101+
"Double",
102+
"Boolean",
103+
"Char",
104+
"String",
105+
"Array",
106+
"List",
107+
"MutableList",
108+
"Map",
109+
"MutableMap",
110+
"Set",
111+
"MutableSet"
112112
));
113113

114114
// this includes hard reserved words defined by https://github.com/JetBrains/kotlin/blob/master/core/descriptors/src/org/jetbrains/kotlin/renderer/KeywordStringsGenerated.java
@@ -209,26 +209,26 @@ public AbstractKotlinCodegen() {
209209
));
210210

211211
typeMapping = new HashMap<>();
212-
typeMapping.put("string", "kotlin.String");
213-
typeMapping.put("boolean", "kotlin.Boolean");
214-
typeMapping.put("integer", "kotlin.Int");
215-
typeMapping.put("float", "kotlin.Float");
216-
typeMapping.put("long", "kotlin.Long");
217-
typeMapping.put("double", "kotlin.Double");
218-
typeMapping.put("ByteArray", "kotlin.ByteArray");
219-
typeMapping.put("number", "java.math.BigDecimal");
220-
typeMapping.put("decimal", "java.math.BigDecimal");
221-
typeMapping.put("date-time", "java.time.OffsetDateTime");
222-
typeMapping.put("date", "java.time.LocalDate");
223-
typeMapping.put("file", "java.io.File");
224-
typeMapping.put("array", "kotlin.Array");
225-
typeMapping.put("list", "kotlin.collections.List");
226-
typeMapping.put("set", "kotlin.collections.Set");
227-
typeMapping.put("map", "kotlin.collections.Map");
228-
typeMapping.put("object", "kotlin.Any");
229-
typeMapping.put("binary", "kotlin.ByteArray");
230-
typeMapping.put("Date", "java.time.LocalDate");
231-
typeMapping.put("DateTime", "java.time.OffsetDateTime");
212+
typeMapping.put("string", "String");
213+
typeMapping.put("boolean", "Boolean");
214+
typeMapping.put("integer", "Int");
215+
typeMapping.put("float", "Float");
216+
typeMapping.put("long", "Long");
217+
typeMapping.put("double", "Double");
218+
typeMapping.put("ByteArray", "ByteArray");
219+
typeMapping.put("number", "BigDecimal");
220+
typeMapping.put("decimal", "BigDecimal");
221+
typeMapping.put("date-time", "OffsetDateTime");
222+
typeMapping.put("date", "LocalDate");
223+
typeMapping.put("file", "File");
224+
typeMapping.put("array", "Array");
225+
typeMapping.put("list", "List");
226+
typeMapping.put("set", "Set");
227+
typeMapping.put("map", "Map");
228+
typeMapping.put("object", "Any");
229+
typeMapping.put("binary", "ByteArray");
230+
typeMapping.put("Date", "LocalDate");
231+
typeMapping.put("DateTime", "OffsetDateTime");
232232
typeMapping.put("AnyType", "kotlin.Any");
233233

234234
instantiationTypes.put("array", "kotlin.collections.ArrayList");
@@ -246,6 +246,18 @@ public AbstractKotlinCodegen() {
246246
importMapping.put("LocalDateTime", "java.time.LocalDateTime");
247247
importMapping.put("LocalDate", "java.time.LocalDate");
248248
importMapping.put("LocalTime", "java.time.LocalTime");
249+
importMapping.put("String", "kotlin.String");
250+
importMapping.put("Boolean", "kotlin.Boolean");
251+
importMapping.put("Integer", "kotlin.Int");
252+
importMapping.put("Float", "kotlin.Float");
253+
importMapping.put("Long", "kotlin.Long");
254+
importMapping.put("Double", "kotlin.Double");
255+
importMapping.put("ByteArray", "kotlin.ByteArray");
256+
importMapping.put("Array", "kotlin.Array");
257+
importMapping.put("List", "kotlin.collections.List");
258+
importMapping.put("Set", "kotlin.collections.Set");
259+
importMapping.put("Map", "kotlin.collections.Map");
260+
importMapping.put("Any", "kotlin.Any");
249261

250262
specialCharReplacements.put(";", "Semicolon");
251263

@@ -336,10 +348,7 @@ public String getSchemaType(Schema p) {
336348
String type;
337349
// This maps, for example, long -> kotlin.Long based on hashes in this type's constructor
338350
if (typeMapping.containsKey(openAPIType)) {
339-
type = typeMapping.get(openAPIType);
340-
if (languageSpecificPrimitives.contains(type)) {
341-
return toModelName(type);
342-
}
351+
return typeMapping.get(openAPIType);
343352
} else {
344353
type = openAPIType;
345354
}
@@ -374,7 +383,7 @@ public String getTypeDeclaration(Schema p) {
374383
inner = new StringSchema().description("TODO default missing map inner type to string");
375384
p.setAdditionalProperties(inner);
376385
}
377-
return getSchemaType(target) + "<kotlin.String, " + getTypeDeclaration(inner) + ">";
386+
return getSchemaType(target) + "<String, " + getTypeDeclaration(inner) + ">";
378387
}
379388
return super.getTypeDeclaration(target);
380389
}
@@ -522,9 +531,12 @@ public void processOpts() {
522531
additionalProperties.put("modelDocPath", modelDocPath);
523532

524533
if (isModelMutable()) {
525-
typeMapping.put("list", "kotlin.collections.MutableList");
526-
typeMapping.put("set", "kotlin.collections.MutableSet");
527-
typeMapping.put("map", "kotlin.collections.MutableMap");
534+
typeMapping.put("list", "MutableList");
535+
typeMapping.put("set", "MutableSet");
536+
typeMapping.put("map", "MutableMap");
537+
importMapping.put("MutableList", "kotlin.collections.MutableList");
538+
importMapping.put("MutableSet", "kotlin.collections.MutableSet");
539+
importMapping.put("MutableMap", "kotlin.collections.MutableMap");
528540
}
529541

530542
if (additionalProperties.containsKey(USE_JAKARTA_EE)) {
@@ -852,9 +864,36 @@ public CodegenModel fromModel(String name, Schema schema) {
852864
.flatMap(List::stream)
853865
.filter(p -> allVarsMap.containsKey(p.baseName))
854866
.forEach(p -> p.isInherited = true);
867+
868+
// additional import for different cases
869+
addAdditionalImports(m, m.getComposedSchemas());
870+
855871
return m;
856872
}
857873

874+
private void addAdditionalImports(CodegenModel model, CodegenComposedSchemas composedSchemas) {
875+
if(composedSchemas == null) {
876+
return;
877+
}
878+
879+
final List<List<CodegenProperty>> propertyLists = Arrays.asList(
880+
composedSchemas.getAnyOf(),
881+
composedSchemas.getOneOf(),
882+
composedSchemas.getAllOf());
883+
for(final List<CodegenProperty> propertyList : propertyLists){
884+
if(propertyList == null)
885+
{
886+
continue;
887+
}
888+
for (CodegenProperty cp : propertyList) {
889+
final String dataType = cp.baseType;
890+
if (null != importMapping().get(dataType) && needToImport(dataType)) {
891+
model.imports.add(dataType);
892+
}
893+
}
894+
}
895+
}
896+
858897
@Override
859898
public String toEnumValue(String value, String datatype) {
860899
if ("kotlin.Int".equals(datatype) || "kotlin.Long".equals(datatype)) {
@@ -874,7 +913,12 @@ public String toEnumValue(String value, String datatype) {
874913

875914
@Override
876915
public boolean isDataTypeString(final String dataType) {
877-
return "String".equals(dataType) || "kotlin.String".equals(dataType);
916+
return isDataType("String", dataType);
917+
}
918+
919+
private boolean isDataType(final String simpleDataType, String dataType) {
920+
Objects.requireNonNull(simpleDataType);
921+
return simpleDataType.equals(dataType) || String.format(Locale.ROOT, "kotlin.%s", simpleDataType).equals(dataType);
878922
}
879923

880924
@Override
@@ -1134,30 +1178,52 @@ protected interface DataTypeAssigner {
11341178
protected void doDataTypeAssignment(final String returnType, DataTypeAssigner dataTypeAssigner) {
11351179
if (returnType == null) {
11361180
dataTypeAssigner.setReturnType("Unit");
1137-
} else if (returnType.startsWith("kotlin.collections.List")) {
1138-
int end = returnType.lastIndexOf(">");
1139-
if (end > 0) {
1140-
dataTypeAssigner.setReturnType(returnType.substring("kotlin.collections.List<".length(), end).trim());
1181+
} else if (isList(returnType) || isMutableList(returnType)) {
1182+
var type = getGenericListType(returnType);
1183+
type.ifPresent(t -> {
1184+
dataTypeAssigner.setReturnType(t);
11411185
dataTypeAssigner.setReturnContainer("List");
1142-
}
1143-
} else if (returnType.startsWith("kotlin.collections.MutableList")) {
1144-
int end = returnType.lastIndexOf(">");
1145-
if (end > 0) {
1146-
dataTypeAssigner.setReturnType(returnType.substring("kotlin.collections.MutableList<".length(), end).trim());
1147-
dataTypeAssigner.setReturnContainer("List");
1148-
}
1149-
} else if (returnType.startsWith("kotlin.collections.Map")) {
1150-
int end = returnType.lastIndexOf(">");
1151-
if (end > 0) {
1152-
dataTypeAssigner.setReturnType(returnType.substring("kotlin.collections.Map<".length(), end).split(",")[1].trim());
1153-
dataTypeAssigner.setReturnContainer("Map");
1154-
}
1155-
} else if (returnType.startsWith("kotlin.collections.MutableMap")) {
1156-
int end = returnType.lastIndexOf(">");
1157-
if (end > 0) {
1158-
dataTypeAssigner.setReturnType(returnType.substring("kotlin.collections.MutableMap<".length(), end).split(",")[1].trim());
1186+
});
1187+
} else if (isMap(returnType) || isMutableMap(returnType)) {
1188+
var type = getGenericMapType(returnType);
1189+
type.ifPresent(t -> {
1190+
dataTypeAssigner.setReturnType(t);
11591191
dataTypeAssigner.setReturnContainer("Map");
1160-
}
1192+
});
1193+
}
1194+
}
1195+
1196+
private boolean isMutableMap(String returnType) {
1197+
return returnType.startsWith("kotlin.collections.MutableMap") || returnType.startsWith("MutableMap");
1198+
}
1199+
1200+
private boolean isMap(String returnType) {
1201+
return returnType.startsWith("kotlin.collections.Map") || returnType.startsWith("Map");
1202+
}
1203+
1204+
private boolean isMutableList(String returnType) {
1205+
return returnType.startsWith("kotlin.collections.MutableList") || returnType.startsWith("MutableList");
1206+
}
1207+
1208+
private boolean isList(String returnType) {
1209+
return returnType.startsWith("kotlin.collections.List") || returnType.startsWith("List");
1210+
}
1211+
1212+
private Optional<String> getGenericListType(String returnType) {
1213+
int end = returnType.lastIndexOf(">");
1214+
int start = returnType.indexOf("<");
1215+
if (start > 0 && end > 0) {
1216+
return Optional.of(returnType.substring(start + 1, end).trim());
1217+
}
1218+
return Optional.empty();
1219+
}
1220+
1221+
private Optional<String> getGenericMapType(String returnType) {
1222+
int end = returnType.lastIndexOf(">");
1223+
int start = returnType.indexOf("<");
1224+
if (start > 0 && end > 0) {
1225+
return Optional.of(returnType.substring(start + 1, end).split(",")[1].trim());
11611226
}
1227+
return Optional.empty();
11621228
}
11631229
}

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

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,9 @@ public void processOpts() {
452452
}
453453
additionalProperties.put(MAP_FILE_BINARY_TO_BYTE_ARRAY, mapFileBinaryToByteArray);
454454
if (mapFileBinaryToByteArray) {
455-
typeMapping.put("file", "kotlin.ByteArray");
456-
typeMapping.put("binary", "kotlin.ByteArray");
455+
typeMapping.put("file", "ByteArray");
456+
typeMapping.put("binary", "ByteArray");
457+
importMapping.put("ByteArray", "kotlin.ByteArray");
457458
}
458459

459460
if (additionalProperties.containsKey(GENERATE_ONEOF_ANYOF_WRAPPERS)) {
@@ -507,11 +508,13 @@ public void processOpts() {
507508

508509
if (CollectionType.LIST.value.equals(collectionType)) {
509510
if (isModelMutable()) {
510-
typeMapping.put("array", "kotlin.collections.MutableList");
511-
typeMapping.put("list", "kotlin.collections.MutableList");
511+
typeMapping.put("array", "MutableList");
512+
typeMapping.put("list", "MutableList");
513+
importMapping.put("MutableList", "kotlin.collections.MutableList");
512514
} else {
513-
typeMapping.put("array", "kotlin.collections.List");
514-
typeMapping.put("list", "kotlin.collections.List");
515+
typeMapping.put("array", "List");
516+
typeMapping.put("list", "List");
517+
importMapping.put("List", "kotlin.collections.List");
515518
}
516519
additionalProperties.put("isList", true);
517520
}
@@ -571,37 +574,36 @@ private void processThreeTenBpDate(String dateLibrary) {
571574
additionalProperties.put(DateLibrary.THREETENBP.value, true);
572575
typeMapping.put("date", "LocalDate");
573576
importMapping.put("LocalDate", "org.threeten.bp.LocalDate");
574-
defaultIncludes.add("org.threeten.bp.LocalDate");
575577

576578
if (dateLibrary.equals(DateLibrary.THREETENBP.value)) {
577-
typeMapping.put("date-time", "org.threeten.bp.OffsetDateTime");
579+
typeMapping.put("date-time", "OffsetDateTime");
578580
typeMapping.put("DateTime", "OffsetDateTime");
579581
importMapping.put("OffsetDateTime", "org.threeten.bp.OffsetDateTime");
580-
defaultIncludes.add("org.threeten.bp.OffsetDateTime");
581582
} else if (dateLibrary.equals(DateLibrary.THREETENBP_LOCALDATETIME.value)) {
582-
typeMapping.put("date-time", "org.threeten.bp.LocalDateTime");
583+
typeMapping.put("date-time", "LocalDateTime");
583584
typeMapping.put("DateTime", "LocalDateTime");
584585
importMapping.put("LocalDateTime", "org.threeten.bp.LocalDateTime");
585-
defaultIncludes.add("org.threeten.bp.LocalDateTime");
586586
}
587587
}
588588

589589
private void processStringDate() {
590-
typeMapping.put("date-time", "kotlin.String");
591-
typeMapping.put("date", "kotlin.String");
592-
typeMapping.put("Date", "kotlin.String");
593-
typeMapping.put("DateTime", "kotlin.String");
590+
typeMapping.put("date-time", "String");
591+
typeMapping.put("date", "String");
592+
typeMapping.put("Date", "String");
593+
typeMapping.put("DateTime", "String");
594+
595+
importMapping.put("String", "kotlin.String");
594596
}
595597

596598
private void processJava8Date(String dateLibrary) {
597599
additionalProperties.put(DateLibrary.JAVA8.value, true);
598600

599601
if (dateLibrary.equals(DateLibrary.JAVA8.value)) {
600-
typeMapping.put("date-time", "java.time.OffsetDateTime");
602+
typeMapping.put("date-time", "OffsetDateTime");
601603
typeMapping.put("DateTime", "OffsetDateTime");
602604
importMapping.put("OffsetDateTime", "java.time.OffsetDateTime");
603605
} else if (dateLibrary.equals(DateLibrary.JAVA8_LOCALDATETIME.value)) {
604-
typeMapping.put("date-time", "java.time.LocalDateTime");
606+
typeMapping.put("date-time", "LocalDateTime");
605607
typeMapping.put("DateTime", "LocalDateTime");
606608
importMapping.put("LocalDateTime", "java.time.LocalDateTime");
607609
}
@@ -832,14 +834,16 @@ private void processMultiplatformLibrary(final String infrastructureFolder) {
832834
defaultIncludes.add(packageName + ".infrastructure.OctetByteArray");
833835

834836
// multiplatform type mapping
835-
typeMapping.put("number", "kotlin.Double");
837+
typeMapping.put("number", "Double");
836838
typeMapping.put("file", "OctetByteArray");
837839
typeMapping.put("binary", "OctetByteArray");
838840
typeMapping.put("ByteArray", "Base64ByteArray");
839-
typeMapping.put("object", "kotlin.String"); // kotlin.Any not serializable
841+
typeMapping.put("object", "String"); // kotlin.Any not serializable
840842

841843
// multiplatform import mapping
842844
importMapping.put("BigDecimal", "kotlin.Double");
845+
importMapping.put("String", "kotlin.String");
846+
importMapping.put("Double", "kotlin.Double");
843847
importMapping.put("UUID", "kotlin.String");
844848
importMapping.put("URI", "kotlin.String");
845849
importMapping.put("InputProvider", "io.ktor.client.request.forms.InputProvider");

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ public KotlinServerCodegen() {
113113
artifactId = "kotlin-server";
114114
packageName = "org.openapitools.server";
115115

116-
typeMapping.put("array", "kotlin.collections.List");
116+
typeMapping.put("array", "List");
117+
importMapping.put("List", "kotlin.collections.List");
117118

118119
// cliOptions default redefinition need to be updated
119120
updateOption(CodegenConstants.ARTIFACT_ID, this.artifactId);
@@ -173,7 +174,8 @@ public void processOpts() {
173174
super.processOpts();
174175

175176
if (isModelMutable()) {
176-
typeMapping.put("array", "kotlin.collections.MutableList");
177+
typeMapping.put("array", "MutableList");
178+
importMapping.put("MutableList", "kotlin.collections.MutableList");
177179
}
178180

179181
if (additionalProperties.containsKey(CodegenConstants.LIBRARY)) {

0 commit comments

Comments
 (0)