1616import java .util .HashMap ;
1717import java .util .HashSet ;
1818import java .util .Map ;
19+ import java .util .Set ;
1920
2021public abstract class AbstractKotlinCodegen extends DefaultCodegenConfig {
22+
2123 private static Logger LOGGER = LoggerFactory .getLogger (AbstractKotlinCodegen .class );
2224
25+ private Set <String > instantiationLibraryFunction ;
26+
27+
28+
2329 protected String artifactId ;
2430 protected String artifactVersion = "1.0.0" ;
2531 protected String groupId = "io.swagger" ;
@@ -47,7 +53,7 @@ public AbstractKotlinCodegen() {
4753 "kotlin.Boolean" ,
4854 "kotlin.Char" ,
4955 "kotlin.String" ,
50- "kotlin.Array" ,
56+ "kotlin.Array" ,
5157 "kotlin.collections.List" ,
5258 "kotlin.collections.Map" ,
5359 "kotlin.collections.Set"
@@ -136,6 +142,11 @@ public AbstractKotlinCodegen() {
136142 "kotlin.collections.Map"
137143 ));
138144
145+ instantiationLibraryFunction = new HashSet <String >(Arrays .asList (
146+ "arrayOf" ,
147+ "mapOf"
148+ ));
149+
139150 typeMapping = new HashMap <String , String >();
140151 typeMapping .put ("string" , "kotlin.String" );
141152 typeMapping .put ("boolean" , "kotlin.Boolean" );
@@ -250,7 +261,7 @@ public void setEnumPropertyNaming(final String enumPropertyNamingType) {
250261 }
251262 throw new RuntimeException (sb .toString ());
252263 }
253- }
264+ }
254265
255266 /**
256267 * Output the type declaration of the property
@@ -270,8 +281,8 @@ public String getTypeDeclaration(Schema propertySchema) {
270281 return null ;
271282 }
272283 // Maps will be keyed only by primitive Kotlin string
273- return String .format ("%s<kotlin.String, %s>" , getSchemaType (propertySchema ), getTypeDeclaration (inner ));
274- }
284+ return String .format ("%s<kotlin.String, %s>" , getSchemaType (propertySchema ), getTypeDeclaration (inner ));
285+ }
275286 return super .getTypeDeclaration (propertySchema );
276287 }
277288
@@ -287,8 +298,6 @@ public String getAlias(String name) {
287298 public String getSchemaType (Schema schema ) {
288299 String schemaType = super .getSchemaType (schema );
289300
290- schemaType = getAlias (schemaType );
291-
292301 // don't apply renaming on types from the typeMapping
293302 if (typeMapping .containsKey (schemaType )) {
294303 return toModelName (typeMapping .get (schemaType ));
@@ -301,7 +310,7 @@ public String getSchemaType(Schema schema) {
301310 } else {
302311 return toModelName ("kotlin.Any" );
303312 }
304- }
313+ }
305314 return toModelName (schemaType );
306315 }
307316
@@ -496,9 +505,9 @@ public String toModelName(final String name) {
496505
497506 @ Override
498507 public String toVarName (String name ) {
499- return super .toVarName (sanitizeKotlinSpecificNames (name ));
508+ return super .toVarName (sanitizeKotlinSpecificNames (name ));
500509 }
501-
510+
502511 /**
503512 * Provides a strongly typed declaration for simple arrays of some type and arrays of arrays of some type.
504513 *
@@ -515,7 +524,7 @@ private String getArrayTypeDeclaration(ArraySchema arraySchema) {
515524 // TODO: We may want to differentiate here between generics and primitive arrays.
516525 instantiationType .append ("<" ).append (nestedType ).append (">" );
517526 return instantiationType .toString ();
518- }
527+ }
519528
520529 /**
521530 * Sanitize against Kotlin specific naming conventions, which may differ from those required by {@link DefaultCodegen#sanitizeName}.
@@ -566,7 +575,13 @@ protected boolean isReservedWord(String word) {
566575 @ Override
567576 protected boolean needToImport (String type ) {
568577 // provides extra protection against improperly trying to import language primitives and java types
569- boolean imports = !type .startsWith ("kotlin." ) && !type .startsWith ("java." ) && !defaultIncludes .contains (type ) && !languageSpecificPrimitives .contains (type );
578+ boolean imports =
579+ !type .startsWith ("kotlin." ) &&
580+ !type .startsWith ("java." ) &&
581+ !defaultIncludes .contains (type ) &&
582+ !languageSpecificPrimitives .contains (type ) &&
583+ !instantiationLibraryFunction .contains (type );
584+
570585 return imports ;
571586 }
572587}
0 commit comments