Skip to content

Commit f15dc1b

Browse files
authored
Merge pull request #3 from claricepoh/kotlin-issues-46
SWCRS-83:
2 parents d5054e3 + 0bb7f98 commit f15dc1b

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

src/main/java/io/swagger/codegen/languages/kotlin/AbstractKotlinCodegen.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616
import java.util.HashMap;
1717
import java.util.HashSet;
1818
import java.util.Map;
19+
import java.util.Set;
1920

2021
public 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
}

src/main/java/io/swagger/codegen/languages/kotlin/KotlinServerCodegen.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
import io.swagger.v3.oas.models.Operation;
44
import io.swagger.v3.oas.models.OpenAPI;
55
import io.swagger.v3.oas.models.media.Schema;
6-
import io.swagger.codegen.CodegenOperation;
6+
import io.swagger.v3.oas.models.media.ArraySchema;
77
import io.swagger.v3.oas.models.parameters.Parameter;
88

99
import io.swagger.codegen.CliOption;
1010
import io.swagger.codegen.CodegenConstants;
1111
import io.swagger.codegen.CodegenType;
12+
import io.swagger.codegen.CodegenModel;
13+
import io.swagger.codegen.CodegenProperty;
14+
import io.swagger.codegen.CodegenOperation;
1215
import io.swagger.codegen.SupportingFile;
1316
import org.slf4j.Logger;
1417
import org.slf4j.LoggerFactory;
@@ -35,15 +38,15 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen {
3538
private Boolean compressionFeatureEnabled = true;
3639

3740
// This is here to potentially warn the user when an option is not supoprted by the target framework.
38-
private Map<String, List<String>> optionsSupportedPerFramework =
39-
singletonMap(Constants.KTOR,
41+
private Map<String, List<String>> optionsSupportedPerFramework =
42+
singletonMap(Constants.KTOR,
4043
Arrays.asList(
4144
Constants.AUTOMATIC_HEAD_REQUESTS,
4245
Constants.CONDITIONAL_HEADERS,
4346
Constants.HSTS,
4447
Constants.CORS,
4548
Constants.COMPRESSION
46-
));
49+
));
4750

4851
/**
4952
* Constructs an instance of `KotlinServerCodegen`.
@@ -128,9 +131,21 @@ public CodegenType getTag() {
128131
return CodegenType.SERVER;
129132
}
130133

134+
/**
135+
* Handle typealias for schema of Array type
136+
*/
137+
@Override
138+
public CodegenModel fromModel(String name, Schema schema, Map<String, Schema> allDefinitions) {
139+
CodegenModel codegenModel = super.fromModel(name, schema, allDefinitions);
140+
141+
if (schema instanceof ArraySchema) {
142+
codegenModel.dataType = getTypeDeclaration(schema);
143+
}
144+
return codegenModel;
145+
}
146+
131147
@Override
132148
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Schema> schemas, OpenAPI openAPI) {
133-
134149
// Ensure that the parameter names in the path are valid kotlin names
135150
// they need to match the names in the generated data class, this is required by ktor Location
136151
String modifiedPath = path;
@@ -198,7 +213,6 @@ public void processOpts() {
198213

199214
String packageFolder = (sourceFolder + File.separator + packageName).replace(".", File.separator);
200215
String resourcesFolder = "src/main/resources"; // not sure this can be user configurable.
201-
202216
Boolean generateApis = additionalProperties.containsKey(GENERATE_APIS) && (Boolean)additionalProperties.get(GENERATE_APIS);
203217

204218
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
@@ -210,9 +224,9 @@ public void processOpts() {
210224

211225
supportingFiles.add(new SupportingFile("AppMain.kt.mustache", packageFolder, "AppMain.kt"));
212226
supportingFiles.add(new SupportingFile("Configuration.kt.mustache", packageFolder, "Configuration.kt"));
213-
227+
214228
if (generateApis) {
215-
supportingFiles.add(new SupportingFile("Paths.kt.mustache", packageFolder, "Paths.kt"));
229+
supportingFiles.add(new SupportingFile("Paths.kt.mustache", packageFolder, "Paths.kt"));
216230
}
217231

218232
supportingFiles.add(new SupportingFile("application.conf.mustache", resourcesFolder, "application.conf"));

src/main/resources/v2/kotlin-server/model.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ import org.threeten.bp.LocalDateTime
99

1010
{{#models}}
1111
{{#model}}
12-
{{#is this 'alias'}}typealias {{classname}} = {{dataType}}{{/is}}{{#isNot this 'alias'}}{{#is this 'enum'}}{{>enum_class}}{{/is}}{{#isNot this 'enum'}}{{>data_class}}{{/isNot}}{{/isNot}}
12+
{{#is this 'alias'}}typealias {{classname}} = {{{dataType}}}{{/is}}{{#isNot this 'alias'}}{{#is this 'enum'}}{{>enum_class}}{{/is}}{{#isNot this 'enum'}}{{>data_class}}{{/isNot}}{{/isNot}}
1313
{{/model}}
1414
{{/models}}

0 commit comments

Comments
 (0)