@@ -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}
0 commit comments