diff --git a/bin/kotlin-client-petstore.sh b/bin/kotlin-client-petstore.sh index f2e558e2466..00fce800fb5 100755 --- a/bin/kotlin-client-petstore.sh +++ b/bin/kotlin-client-petstore.sh @@ -26,6 +26,6 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -t modules/swagger-codegen/src/main/resources/kotlin-client -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l kotlin --artifact-id kotlin-petstore-client -o samples/client/petstore/kotlin $@" +ags="generate -t modules/swagger-codegen/src/main/resources/kotlin-client -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l kotlin --artifact-id kotlin-petstore-client -D dateLibrary=java8 -o samples/client/petstore/kotlin $@" java ${JAVA_OPTS} -jar ${executable} ${ags} diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/KotlinClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/KotlinClientCodegen.java index 9310cd467f0..86a3e67bccd 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/KotlinClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/KotlinClientCodegen.java @@ -9,8 +9,32 @@ import java.io.File; public class KotlinClientCodegen extends AbstractKotlinCodegen { + public static final String DATE_LIBRARY = "dateLibrary"; + + protected String groupId = "io.swagger"; + protected String artifactId = "kotlin-client"; + protected String artifactVersion = "1.0.0"; + protected String sourceFolder = "src/main/kotlin"; + protected String packageName = "io.swagger.client"; + protected String apiDocPath = "docs/"; + protected String modelDocPath = "docs/"; + protected CodegenConstants.ENUM_PROPERTY_NAMING_TYPE enumPropertyNaming = CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.camelCase; static Logger LOGGER = LoggerFactory.getLogger(KotlinClientCodegen.class); + protected String dateLibrary = DateLibrary.JAVA8.value; + + public enum DateLibrary { + STRING("string"), + THREETENBP("threetenbp"), + JAVA8("java8"); + + public final String value; + + DateLibrary(String value) { + this.value = value; + } + } + /** * Constructs an instance of `KotlinClientCodegen`. */ @@ -28,6 +52,14 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen { embeddedTemplateDir = templateDir = "kotlin-client"; apiPackage = packageName + ".apis"; modelPackage = packageName + ".models"; + + CliOption dateLibrary = new CliOption(DATE_LIBRARY, "Option. Date library to use"); + Map dateOptions = new HashMap<>(); + dateOptions.put(DateLibrary.THREETENBP.value, "Threetenbp"); + dateOptions.put(DateLibrary.STRING.value, "String"); + dateOptions.put(DateLibrary.JAVA8.value, "Java 8 native JSR310"); + dateLibrary.setEnum(dateOptions); + cliOptions.add(dateLibrary); } public CodegenType getTag() { @@ -42,10 +74,33 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen { return "Generates a kotlin client."; } + public void setDateLibrary(String library) { + this.dateLibrary = library; + } + @Override public void processOpts() { super.processOpts(); + if (additionalProperties.containsKey(DATE_LIBRARY)) { + setDateLibrary(additionalProperties.get(DATE_LIBRARY).toString()); + } + + if (DateLibrary.THREETENBP.value.equals(dateLibrary)) { + additionalProperties.put(DateLibrary.THREETENBP.value, true); + typeMapping.put("date", "LocalDate"); + typeMapping.put("DateTime", "LocalDateTime"); + importMapping.put("LocalDate", "org.threeten.bp.LocalDate"); + importMapping.put("LocalDateTime", "org.threeten.bp.LocalDateTime"); + } else if (DateLibrary.STRING.value.equals(dateLibrary)) { + typeMapping.put("date-time", "kotlin.String"); + typeMapping.put("date", "kotlin.String"); + typeMapping.put("Date", "kotlin.String"); + typeMapping.put("DateTime", "kotlin.String"); + } else if (DateLibrary.JAVA8.value.equals(dateLibrary)) { + additionalProperties.put(DateLibrary.JAVA8.value, true); + } + supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); supportingFiles.add(new SupportingFile("build.gradle.mustache", "", "build.gradle")); diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/kotlin/KotlinClientCodegenModelTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/kotlin/KotlinClientCodegenModelTest.java index 83ae1904540..067b2f78f7c 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/kotlin/KotlinClientCodegenModelTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/kotlin/KotlinClientCodegenModelTest.java @@ -89,6 +89,66 @@ public class KotlinClientCodegenModelTest { Assert.assertTrue(property3.isNotContainer); } + @Test(description = "convert a simple model: threetenbp") + public void selectDateLibraryAsThreetenbp() { + final Model model = getSimpleModel(); + final KotlinClientCodegen codegen = new KotlinClientCodegen(); + codegen.setDateLibrary(KotlinClientCodegen.DateLibrary.THREETENBP.value); + codegen.processOpts(); + + final CodegenModel cm = codegen.fromModel("sample", model); + + final CodegenProperty property3 = cm.vars.get(2); + Assert.assertEquals(property3.baseName, "createdAt"); + Assert.assertEquals(property3.datatype, "org.threeten.bp.LocalDateTime"); + Assert.assertEquals(property3.name, "createdAt"); + Assert.assertEquals(property3.defaultValue, "null"); + Assert.assertEquals(property3.baseType, "org.threeten.bp.LocalDateTime"); + Assert.assertFalse(property3.hasMore); + Assert.assertFalse(property3.required); + Assert.assertTrue(property3.isNotContainer); + } + + @Test(description = "convert a simple model: date string") + public void selectDateLibraryAsString() { + final Model model = getSimpleModel(); + final KotlinClientCodegen codegen = new KotlinClientCodegen(); + codegen.setDateLibrary(KotlinClientCodegen.DateLibrary.STRING.value); + codegen.processOpts(); + + final CodegenModel cm = codegen.fromModel("sample", model); + + final CodegenProperty property3 = cm.vars.get(2); + Assert.assertEquals(property3.baseName, "createdAt"); + Assert.assertEquals(property3.datatype, "kotlin.String"); + Assert.assertEquals(property3.name, "createdAt"); + Assert.assertEquals(property3.defaultValue, "null"); + Assert.assertEquals(property3.baseType, "kotlin.String"); + Assert.assertFalse(property3.hasMore); + Assert.assertFalse(property3.required); + Assert.assertTrue(property3.isNotContainer); + } + + @Test(description = "convert a simple model: date java8") + public void selectDateLibraryAsJava8() { + final Model model = getSimpleModel(); + final KotlinClientCodegen codegen = new KotlinClientCodegen(); + codegen.setDateLibrary(KotlinClientCodegen.DateLibrary.JAVA8.value); + codegen.processOpts(); + + final CodegenModel cm = codegen.fromModel("sample", model); + + final CodegenProperty property3 = cm.vars.get(2); + Assert.assertEquals(property3.baseName, "createdAt"); + Assert.assertEquals(property3.datatype, "java.time.LocalDateTime"); + Assert.assertEquals(property3.name, "createdAt"); + Assert.assertEquals(property3.defaultValue, "null"); + Assert.assertEquals(property3.baseType, "java.time.LocalDateTime"); + Assert.assertFalse(property3.hasMore); + Assert.assertFalse(property3.required); + Assert.assertTrue(property3.isNotContainer); + } + @Test(description = "convert a model with array property to default kotlin.Array") public void arrayPropertyTest() { final Model model = getArrayTestModel(); @@ -113,6 +173,7 @@ public class KotlinClientCodegenModelTest { Assert.assertFalse(property.required); Assert.assertTrue(property.isContainer); } + @Test(description = "convert a model with a map property") public void mapPropertyTest() { final Model model = getMapModel(); @@ -156,13 +217,13 @@ public class KotlinClientCodegenModelTest { } @DataProvider(name = "modelNames") - public static Object[][] modelNames(){ - return new Object[][] { - { "TestNs.TestClass" , new ModelNameTest("TestNs.TestClass", "TestNsTestClass") }, - { "$", new ModelNameTest("$", "Dollar") }, - { "for", new ModelNameTest("`for`","`for`")}, - { "One