[kotlin] support selection of datelibrary (#7054)

* [kotlin] support selection of datelibrary

* remove additional property from string

* replace string with boolean
This commit is contained in:
magiepooh 2018-01-28 18:16:01 +09:00 committed by William Cheng
parent 88c5112f2e
commit 914275fe79
5 changed files with 128 additions and 8 deletions

View File

@ -26,6 +26,6 @@ fi
# if you've executed sbt assembly previously it will use that instead. # 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" 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} java ${JAVA_OPTS} -jar ${executable} ${ags}

View File

@ -9,8 +9,32 @@ import java.io.File;
public class KotlinClientCodegen extends AbstractKotlinCodegen { 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); 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`. * Constructs an instance of `KotlinClientCodegen`.
*/ */
@ -28,6 +52,14 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
embeddedTemplateDir = templateDir = "kotlin-client"; embeddedTemplateDir = templateDir = "kotlin-client";
apiPackage = packageName + ".apis"; apiPackage = packageName + ".apis";
modelPackage = packageName + ".models"; modelPackage = packageName + ".models";
CliOption dateLibrary = new CliOption(DATE_LIBRARY, "Option. Date library to use");
Map<String, String> 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() { public CodegenType getTag() {
@ -42,10 +74,33 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
return "Generates a kotlin client."; return "Generates a kotlin client.";
} }
public void setDateLibrary(String library) {
this.dateLibrary = library;
}
@Override @Override
public void processOpts() { public void processOpts() {
super.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("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("build.gradle.mustache", "", "build.gradle")); supportingFiles.add(new SupportingFile("build.gradle.mustache", "", "build.gradle"));

View File

@ -89,6 +89,66 @@ public class KotlinClientCodegenModelTest {
Assert.assertTrue(property3.isNotContainer); 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") @Test(description = "convert a model with array property to default kotlin.Array")
public void arrayPropertyTest() { public void arrayPropertyTest() {
final Model model = getArrayTestModel(); final Model model = getArrayTestModel();
@ -113,6 +173,7 @@ public class KotlinClientCodegenModelTest {
Assert.assertFalse(property.required); Assert.assertFalse(property.required);
Assert.assertTrue(property.isContainer); Assert.assertTrue(property.isContainer);
} }
@Test(description = "convert a model with a map property") @Test(description = "convert a model with a map property")
public void mapPropertyTest() { public void mapPropertyTest() {
final Model model = getMapModel(); final Model model = getMapModel();
@ -156,13 +217,13 @@ public class KotlinClientCodegenModelTest {
} }
@DataProvider(name = "modelNames") @DataProvider(name = "modelNames")
public static Object[][] modelNames(){ public static Object[][] modelNames() {
return new Object[][] { return new Object[][]{
{ "TestNs.TestClass" , new ModelNameTest("TestNs.TestClass", "TestNsTestClass") }, {"TestNs.TestClass", new ModelNameTest("TestNs.TestClass", "TestNsTestClass")},
{ "$", new ModelNameTest("$", "Dollar") }, {"$", new ModelNameTest("$", "Dollar")},
{ "for", new ModelNameTest("`for`","`for`")}, {"for", new ModelNameTest("`for`", "`for`")},
{ "One<Two", new ModelNameTest("One<Two", "OneLess_ThanTwo")}, {"One<Two", new ModelNameTest("One<Two", "OneLess_ThanTwo")},
{ "this is a test", new ModelNameTest("this is a test", "This_is_a_test")} {"this is a test", new ModelNameTest("this is a test", "This_is_a_test")}
}; };
} }

View File

@ -38,6 +38,7 @@ public class KotlinClientCodegenOptionsTest extends AbstractOptionsTest {
times = 1; times = 1;
codegen.setEnumPropertyNaming(KotlinClientCodegenOptionsProvider.ENUM_PROPERTY_NAMING); codegen.setEnumPropertyNaming(KotlinClientCodegenOptionsProvider.ENUM_PROPERTY_NAMING);
times = 1; times = 1;
codegen.setDateLibrary(KotlinClientCodegenOptionsProvider.DATE_LIBRARY);
}}; }};
} }
} }

View File

@ -3,6 +3,7 @@ package io.swagger.codegen.options;
import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.CodegenConstants;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import io.swagger.codegen.languages.KotlinClientCodegen;
import java.util.Map; import java.util.Map;
@ -13,6 +14,7 @@ public class KotlinClientCodegenOptionsProvider implements OptionsProvider {
public static final String GROUP_ID = "io.swagger.tests"; public static final String GROUP_ID = "io.swagger.tests";
public static final String SOURCE_FOLDER = "./generated/kotlin"; public static final String SOURCE_FOLDER = "./generated/kotlin";
public static final String ENUM_PROPERTY_NAMING = "camelCase"; public static final String ENUM_PROPERTY_NAMING = "camelCase";
public static final String DATE_LIBRARY = KotlinClientCodegen.DateLibrary.JAVA8.value;
@Override @Override
public String getLanguage() { public String getLanguage() {
@ -29,6 +31,7 @@ public class KotlinClientCodegenOptionsProvider implements OptionsProvider {
.put(CodegenConstants.GROUP_ID, GROUP_ID) .put(CodegenConstants.GROUP_ID, GROUP_ID)
.put(CodegenConstants.SOURCE_FOLDER, SOURCE_FOLDER) .put(CodegenConstants.SOURCE_FOLDER, SOURCE_FOLDER)
.put(CodegenConstants.ENUM_PROPERTY_NAMING, ENUM_PROPERTY_NAMING) .put(CodegenConstants.ENUM_PROPERTY_NAMING, ENUM_PROPERTY_NAMING)
.put(KotlinClientCodegen.DATE_LIBRARY, DATE_LIBRARY)
.build(); .build();
} }