Skip to content

Commit

Permalink
[kotlin] Use simple class name instead of canonical names in the data…
Browse files Browse the repository at this point in the history
…type model property (#19942)
  • Loading branch information
CaptainAye committed Nov 4, 2024
1 parent 67af02c commit b8eeedd
Show file tree
Hide file tree
Showing 14 changed files with 349 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,23 @@ public AbstractKotlinCodegen() {
setSortModelPropertiesByRequiredFlag(true);

languageSpecificPrimitives = new HashSet<>(Arrays.asList(
"kotlin.Byte",
"kotlin.ByteArray",
"kotlin.Short",
"kotlin.Int",
"kotlin.Long",
"kotlin.Float",
"kotlin.Double",
"kotlin.Boolean",
"kotlin.Char",
"kotlin.String",
"kotlin.Array",
"kotlin.collections.List",
"kotlin.collections.MutableList",
"kotlin.collections.Map",
"kotlin.collections.MutableMap",
"kotlin.collections.Set",
"kotlin.collections.MutableSet"
"Byte",
"ByteArray",
"Short",
"Int",
"Long",
"Float",
"Double",
"Boolean",
"Char",
"String",
"Array",
"List",
"MutableList",
"Map",
"MutableMap",
"Set",
"MutableSet"
));

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

typeMapping = new HashMap<>();
typeMapping.put("string", "kotlin.String");
typeMapping.put("boolean", "kotlin.Boolean");
typeMapping.put("integer", "kotlin.Int");
typeMapping.put("float", "kotlin.Float");
typeMapping.put("long", "kotlin.Long");
typeMapping.put("double", "kotlin.Double");
typeMapping.put("ByteArray", "kotlin.ByteArray");
typeMapping.put("number", "java.math.BigDecimal");
typeMapping.put("decimal", "java.math.BigDecimal");
typeMapping.put("date-time", "java.time.OffsetDateTime");
typeMapping.put("date", "java.time.LocalDate");
typeMapping.put("file", "java.io.File");
typeMapping.put("array", "kotlin.Array");
typeMapping.put("list", "kotlin.collections.List");
typeMapping.put("set", "kotlin.collections.Set");
typeMapping.put("map", "kotlin.collections.Map");
typeMapping.put("object", "kotlin.Any");
typeMapping.put("binary", "kotlin.ByteArray");
typeMapping.put("Date", "java.time.LocalDate");
typeMapping.put("DateTime", "java.time.OffsetDateTime");
typeMapping.put("string", "String");
typeMapping.put("boolean", "Boolean");
typeMapping.put("integer", "Int");
typeMapping.put("float", "Float");
typeMapping.put("long", "Long");
typeMapping.put("double", "Double");
typeMapping.put("ByteArray", "ByteArray");
typeMapping.put("number", "BigDecimal");
typeMapping.put("decimal", "BigDecimal");
typeMapping.put("date-time", "OffsetDateTime");
typeMapping.put("date", "LocalDate");
typeMapping.put("file", "File");
typeMapping.put("array", "Array");
typeMapping.put("list", "List");
typeMapping.put("set", "Set");
typeMapping.put("map", "Map");
typeMapping.put("object", "Any");
typeMapping.put("binary", "ByteArray");
typeMapping.put("Date", "LocalDate");
typeMapping.put("DateTime", "OffsetDateTime");
typeMapping.put("AnyType", "kotlin.Any");

instantiationTypes.put("array", "kotlin.collections.ArrayList");
Expand All @@ -246,6 +246,18 @@ public AbstractKotlinCodegen() {
importMapping.put("LocalDateTime", "java.time.LocalDateTime");
importMapping.put("LocalDate", "java.time.LocalDate");
importMapping.put("LocalTime", "java.time.LocalTime");
importMapping.put("String", "kotlin.String");
importMapping.put("Boolean", "kotlin.Boolean");
importMapping.put("Integer", "kotlin.Int");
importMapping.put("Float", "kotlin.Float");
importMapping.put("Long", "kotlin.Long");
importMapping.put("Double", "kotlin.Double");
importMapping.put("ByteArray", "kotlin.ByteArray");
importMapping.put("Array", "kotlin.Array");
importMapping.put("List", "kotlin.collections.List");
importMapping.put("Set", "kotlin.collections.Set");
importMapping.put("Map", "kotlin.collections.Map");
importMapping.put("Any", "kotlin.Any");

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

Expand Down Expand Up @@ -336,10 +348,7 @@ public String getSchemaType(Schema p) {
String type;
// This maps, for example, long -> kotlin.Long based on hashes in this type's constructor
if (typeMapping.containsKey(openAPIType)) {
type = typeMapping.get(openAPIType);
if (languageSpecificPrimitives.contains(type)) {
return toModelName(type);
}
return typeMapping.get(openAPIType);
} else {
type = openAPIType;
}
Expand Down Expand Up @@ -374,7 +383,7 @@ public String getTypeDeclaration(Schema p) {
inner = new StringSchema().description("TODO default missing map inner type to string");
p.setAdditionalProperties(inner);
}
return getSchemaType(target) + "<kotlin.String, " + getTypeDeclaration(inner) + ">";
return getSchemaType(target) + "<String, " + getTypeDeclaration(inner) + ">";
}
return super.getTypeDeclaration(target);
}
Expand Down Expand Up @@ -522,9 +531,12 @@ public void processOpts() {
additionalProperties.put("modelDocPath", modelDocPath);

if (isModelMutable()) {
typeMapping.put("list", "kotlin.collections.MutableList");
typeMapping.put("set", "kotlin.collections.MutableSet");
typeMapping.put("map", "kotlin.collections.MutableMap");
typeMapping.put("list", "MutableList");
typeMapping.put("set", "MutableSet");
typeMapping.put("map", "MutableMap");
importMapping.put("MutableList", "kotlin.collections.MutableList");
importMapping.put("MutableSet", "kotlin.collections.MutableSet");
importMapping.put("MutableMap", "kotlin.collections.MutableMap");
}

if (additionalProperties.containsKey(USE_JAKARTA_EE)) {
Expand Down Expand Up @@ -852,9 +864,36 @@ public CodegenModel fromModel(String name, Schema schema) {
.flatMap(List::stream)
.filter(p -> allVarsMap.containsKey(p.baseName))
.forEach(p -> p.isInherited = true);

// additional import for different cases
addAdditionalImports(m, m.getComposedSchemas());

return m;
}

private void addAdditionalImports(CodegenModel model, CodegenComposedSchemas composedSchemas) {
if(composedSchemas == null) {
return;
}

final List<List<CodegenProperty>> propertyLists = Arrays.asList(
composedSchemas.getAnyOf(),
composedSchemas.getOneOf(),
composedSchemas.getAllOf());
for(final List<CodegenProperty> propertyList : propertyLists){
if(propertyList == null)
{
continue;
}
for (CodegenProperty cp : propertyList) {
final String dataType = cp.baseType;
if (null != importMapping().get(dataType) && needToImport(dataType)) {
model.imports.add(dataType);
}
}
}
}

@Override
public String toEnumValue(String value, String datatype) {
if ("kotlin.Int".equals(datatype) || "kotlin.Long".equals(datatype)) {
Expand Down Expand Up @@ -1134,30 +1173,52 @@ protected interface DataTypeAssigner {
protected void doDataTypeAssignment(final String returnType, DataTypeAssigner dataTypeAssigner) {
if (returnType == null) {
dataTypeAssigner.setReturnType("Unit");
} else if (returnType.startsWith("kotlin.collections.List")) {
int end = returnType.lastIndexOf(">");
if (end > 0) {
dataTypeAssigner.setReturnType(returnType.substring("kotlin.collections.List<".length(), end).trim());
dataTypeAssigner.setReturnContainer("List");
}
} else if (returnType.startsWith("kotlin.collections.MutableList")) {
int end = returnType.lastIndexOf(">");
if (end > 0) {
dataTypeAssigner.setReturnType(returnType.substring("kotlin.collections.MutableList<".length(), end).trim());
} else if (isList(returnType) || isMutableList(returnType)) {
var type = getGenericListType(returnType);
type.ifPresent(t -> {
dataTypeAssigner.setReturnType(t);
dataTypeAssigner.setReturnContainer("List");
}
} else if (returnType.startsWith("kotlin.collections.Map")) {
int end = returnType.lastIndexOf(">");
if (end > 0) {
dataTypeAssigner.setReturnType(returnType.substring("kotlin.collections.Map<".length(), end).split(",")[1].trim());
dataTypeAssigner.setReturnContainer("Map");
}
} else if (returnType.startsWith("kotlin.collections.MutableMap")) {
int end = returnType.lastIndexOf(">");
if (end > 0) {
dataTypeAssigner.setReturnType(returnType.substring("kotlin.collections.MutableMap<".length(), end).split(",")[1].trim());
});
} else if (isMap(returnType) || isMutableMap(returnType)) {
var type = getGenericMapType(returnType);
type.ifPresent(t -> {
dataTypeAssigner.setReturnType(t);
dataTypeAssigner.setReturnContainer("Map");
}
});
}
}

private boolean isMutableMap(String returnType) {
return returnType.startsWith("kotlin.collections.MutableMap") || returnType.startsWith("MutableMap");
}

private boolean isMap(String returnType) {
return returnType.startsWith("kotlin.collections.Map") || returnType.startsWith("Map");
}

private boolean isMutableList(String returnType) {
return returnType.startsWith("kotlin.collections.MutableList") || returnType.startsWith("MutableList");
}

private boolean isList(String returnType) {
return returnType.startsWith("kotlin.collections.List") || returnType.startsWith("List");
}

private Optional<String> getGenericListType(String returnType) {
int end = returnType.lastIndexOf(">");
int start = returnType.indexOf("<");
if (start > 0 && end > 0) {
return Optional.of(returnType.substring(start + 1, end).trim());
}
return Optional.empty();
}

private Optional<String> getGenericMapType(String returnType) {
int end = returnType.lastIndexOf(">");
int start = returnType.indexOf("<");
if (start > 0 && end > 0) {
return Optional.of(returnType.substring(start + 1, end).split(",")[1].trim());
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,9 @@ public void processOpts() {
}
additionalProperties.put(MAP_FILE_BINARY_TO_BYTE_ARRAY, mapFileBinaryToByteArray);
if (mapFileBinaryToByteArray) {
typeMapping.put("file", "kotlin.ByteArray");
typeMapping.put("binary", "kotlin.ByteArray");
typeMapping.put("file", "ByteArray");
typeMapping.put("binary", "ByteArray");
importMapping.put("ByteArray", "kotlin.ByteArray");
}

if (additionalProperties.containsKey(GENERATE_ONEOF_ANYOF_WRAPPERS)) {
Expand Down Expand Up @@ -507,11 +508,13 @@ public void processOpts() {

if (CollectionType.LIST.value.equals(collectionType)) {
if (isModelMutable()) {
typeMapping.put("array", "kotlin.collections.MutableList");
typeMapping.put("list", "kotlin.collections.MutableList");
typeMapping.put("array", "MutableList");
typeMapping.put("list", "MutableList");
importMapping.put("MutableList", "kotlin.collections.MutableList");
} else {
typeMapping.put("array", "kotlin.collections.List");
typeMapping.put("list", "kotlin.collections.List");
typeMapping.put("array", "List");
typeMapping.put("list", "List");
importMapping.put("List", "kotlin.collections.List");
}
additionalProperties.put("isList", true);
}
Expand Down Expand Up @@ -571,37 +574,36 @@ private void processThreeTenBpDate(String dateLibrary) {
additionalProperties.put(DateLibrary.THREETENBP.value, true);
typeMapping.put("date", "LocalDate");
importMapping.put("LocalDate", "org.threeten.bp.LocalDate");
defaultIncludes.add("org.threeten.bp.LocalDate");

if (dateLibrary.equals(DateLibrary.THREETENBP.value)) {
typeMapping.put("date-time", "org.threeten.bp.OffsetDateTime");
typeMapping.put("date-time", "OffsetDateTime");
typeMapping.put("DateTime", "OffsetDateTime");
importMapping.put("OffsetDateTime", "org.threeten.bp.OffsetDateTime");
defaultIncludes.add("org.threeten.bp.OffsetDateTime");
} else if (dateLibrary.equals(DateLibrary.THREETENBP_LOCALDATETIME.value)) {
typeMapping.put("date-time", "org.threeten.bp.LocalDateTime");
typeMapping.put("date-time", "LocalDateTime");
typeMapping.put("DateTime", "LocalDateTime");
importMapping.put("LocalDateTime", "org.threeten.bp.LocalDateTime");
defaultIncludes.add("org.threeten.bp.LocalDateTime");
}
}

private void processStringDate() {
typeMapping.put("date-time", "kotlin.String");
typeMapping.put("date", "kotlin.String");
typeMapping.put("Date", "kotlin.String");
typeMapping.put("DateTime", "kotlin.String");
typeMapping.put("date-time", "String");
typeMapping.put("date", "String");
typeMapping.put("Date", "String");
typeMapping.put("DateTime", "String");

importMapping.put("String", "kotlin.String");
}

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

if (dateLibrary.equals(DateLibrary.JAVA8.value)) {
typeMapping.put("date-time", "java.time.OffsetDateTime");
typeMapping.put("date-time", "OffsetDateTime");
typeMapping.put("DateTime", "OffsetDateTime");
importMapping.put("OffsetDateTime", "java.time.OffsetDateTime");
} else if (dateLibrary.equals(DateLibrary.JAVA8_LOCALDATETIME.value)) {
typeMapping.put("date-time", "java.time.LocalDateTime");
typeMapping.put("date-time", "LocalDateTime");
typeMapping.put("DateTime", "LocalDateTime");
importMapping.put("LocalDateTime", "java.time.LocalDateTime");
}
Expand Down Expand Up @@ -832,14 +834,16 @@ private void processMultiplatformLibrary(final String infrastructureFolder) {
defaultIncludes.add(packageName + ".infrastructure.OctetByteArray");

// multiplatform type mapping
typeMapping.put("number", "kotlin.Double");
typeMapping.put("number", "Double");
typeMapping.put("file", "OctetByteArray");
typeMapping.put("binary", "OctetByteArray");
typeMapping.put("ByteArray", "Base64ByteArray");
typeMapping.put("object", "kotlin.String"); // kotlin.Any not serializable
typeMapping.put("object", "String"); // kotlin.Any not serializable

// multiplatform import mapping
importMapping.put("BigDecimal", "kotlin.Double");
importMapping.put("String", "kotlin.String");
importMapping.put("Double", "kotlin.Double");
importMapping.put("UUID", "kotlin.String");
importMapping.put("URI", "kotlin.String");
importMapping.put("InputProvider", "io.ktor.client.request.forms.InputProvider");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ public KotlinServerCodegen() {
artifactId = "kotlin-server";
packageName = "org.openapitools.server";

typeMapping.put("array", "kotlin.collections.List");
typeMapping.put("array", "List");
importMapping.put("List", "kotlin.collections.List");

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

if (isModelMutable()) {
typeMapping.put("array", "kotlin.collections.MutableList");
typeMapping.put("array", "MutableList");
importMapping.put("MutableList", "kotlin.collections.MutableList");
}

if (additionalProperties.containsKey(CodegenConstants.LIBRARY)) {
Expand Down
Loading

0 comments on commit b8eeedd

Please sign in to comment.