From 5b58289fd7c058b96262ac109f2295a8375c6496 Mon Sep 17 00:00:00 2001 From: Simas Abramovas Date: Thu, 29 Nov 2018 04:33:55 +0200 Subject: [PATCH] Kotlin collection type (#1564) * Allow specifying type * Add test * Update docs * Don't modify types if the default option is chosen --- docs/generators/kotlin.md | 5 +++ .../languages/KotlinClientCodegen.java | 33 +++++++++++++++++++ .../kotlin/KotlinClientCodegenModelTest.java | 27 +++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/docs/generators/kotlin.md b/docs/generators/kotlin.md index 91ebf5fc6a6b..0aee9282c642 100644 --- a/docs/generators/kotlin.md +++ b/docs/generators/kotlin.md @@ -28,4 +28,9 @@ CONFIG OPTIONS for kotlin java8 - Java 8 native JSR310 threetenbp - Threetenbp + collectionType + Option. Collection type to use + array - kotlin.Array + list - kotlin.collections.List + Back to the [generators list](README.md) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java index 673ca1637145..85ac703ef54d 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java @@ -31,9 +31,11 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen { public static final String DATE_LIBRARY = "dateLibrary"; + public static final String COLLECTION_TYPE = "collectionType"; private static final Logger LOGGER = LoggerFactory.getLogger(KotlinClientCodegen.class); protected String dateLibrary = DateLibrary.JAVA8.value; + protected String collectionType = CollectionType.ARRAY.value; public enum DateLibrary { STRING("string"), @@ -47,6 +49,17 @@ public enum DateLibrary { } } + public enum CollectionType { + ARRAY("array"), + LIST("list"); + + public final String value; + + CollectionType(String value) { + this.value = value; + } + } + /** * Constructs an instance of `KotlinClientCodegen`. */ @@ -74,6 +87,13 @@ public KotlinClientCodegen() { dateOptions.put(DateLibrary.JAVA8.value, "Java 8 native JSR310"); dateLibrary.setEnum(dateOptions); cliOptions.add(dateLibrary); + + CliOption collectionType = new CliOption(COLLECTION_TYPE, "Option. Collection type to use"); + Map collectionOptions = new HashMap<>(); + collectionOptions.put(CollectionType.ARRAY.value, "kotlin.Array"); + collectionOptions.put(CollectionType.LIST.value, "kotlin.collections.List"); + collectionType.setEnum(collectionOptions); + cliOptions.add(collectionType); } public CodegenType getTag() { @@ -92,6 +112,10 @@ public void setDateLibrary(String library) { this.dateLibrary = library; } + public void setCollectionType(String collectionType) { + this.collectionType = collectionType; + } + @Override public void processOpts() { super.processOpts(); @@ -116,6 +140,15 @@ public void processOpts() { additionalProperties.put(DateLibrary.JAVA8.value, true); } + if (additionalProperties.containsKey(COLLECTION_TYPE)) { + setCollectionType(additionalProperties.get(COLLECTION_TYPE).toString()); + } + + if (CollectionType.LIST.value.equals(collectionType)) { + typeMapping.put("array", "kotlin.collections.List"); + typeMapping.put("list", "kotlin.collections.List"); + } + supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); supportingFiles.add(new SupportingFile("build.gradle.mustache", "", "build.gradle")); supportingFiles.add(new SupportingFile("settings.gradle.mustache", "", "settings.gradle")); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenModelTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenModelTest.java index 4c6894c17d48..0def29694753 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenModelTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenModelTest.java @@ -199,6 +199,33 @@ public void arrayPropertyTest() { Assert.assertTrue(property.isContainer); } + @Test(description = "convert a model with array property to a kotlin.collections.List") + public void listPropertyTest() { + final Schema model = getArrayTestSchema(); + + final KotlinClientCodegen codegen = new KotlinClientCodegen(); + codegen.setCollectionType(KotlinClientCodegen.CollectionType.LIST.value); + codegen.processOpts(); + final CodegenModel generated = codegen.fromModel("sample", model, Collections.singletonMap("sample", model)); + + Assert.assertEquals(generated.name, "sample"); + Assert.assertEquals(generated.classname, "Sample"); + Assert.assertEquals(generated.description, "a sample model"); + Assert.assertEquals(generated.vars.size(), 2); + + final CodegenProperty property = generated.vars.get(1); + Assert.assertEquals(property.baseName, "examples"); + Assert.assertEquals(property.getter, "getExamples"); + Assert.assertEquals(property.setter, "setExamples"); + Assert.assertEquals(property.dataType, "kotlin.collections.List"); + Assert.assertEquals(property.name, "examples"); + Assert.assertEquals(property.defaultValue, "null"); + Assert.assertEquals(property.baseType, "kotlin.collections.List"); + Assert.assertEquals(property.containerType, "array"); + Assert.assertFalse(property.required); + Assert.assertTrue(property.isContainer); + } + @Test(description = "convert a model with a map property") public void mapPropertyTest() { final Schema schema = getMapSchema();