Skip to content

Commit

Permalink
[kotlin] support selection of datelibrary (#7054)
Browse files Browse the repository at this point in the history
* [kotlin] support selection of datelibrary

* remove additional property from string

* replace string with boolean
  • Loading branch information
magiepooh authored and wing328 committed Jan 28, 2018
1 parent 88c5112 commit 914275f
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bin/kotlin-client-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,32 @@

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`.
*/
Expand All @@ -28,6 +52,14 @@ public KotlinClientCodegen() {
embeddedTemplateDir = templateDir = "kotlin-client";
apiPackage = packageName + ".apis";
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() {
Expand All @@ -42,10 +74,33 @@ public String getHelp() {
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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,66 @@ public void simpleModelTest() {
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();
Expand All @@ -113,6 +173,7 @@ public void arrayPropertyTest() {
Assert.assertFalse(property.required);
Assert.assertTrue(property.isContainer);
}

@Test(description = "convert a model with a map property")
public void mapPropertyTest() {
final Model model = getMapModel();
Expand Down Expand Up @@ -156,13 +217,13 @@ public void complexPropertyTest() {
}

@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<Two", new ModelNameTest("One<Two", "OneLess_ThanTwo")},
{ "this is a test", new ModelNameTest("this is a test", "This_is_a_test")}
public static Object[][] modelNames() {
return new Object[][]{
{"TestNs.TestClass", new ModelNameTest("TestNs.TestClass", "TestNsTestClass")},
{"$", new ModelNameTest("$", "Dollar")},
{"for", new ModelNameTest("`for`", "`for`")},
{"One<Two", new ModelNameTest("One<Two", "OneLess_ThanTwo")},
{"this is a test", new ModelNameTest("this is a test", "This_is_a_test")}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected void setExpectations() {
times = 1;
codegen.setEnumPropertyNaming(KotlinClientCodegenOptionsProvider.ENUM_PROPERTY_NAMING);
times = 1;
codegen.setDateLibrary(KotlinClientCodegenOptionsProvider.DATE_LIBRARY);
}};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.swagger.codegen.CodegenConstants;

import com.google.common.collect.ImmutableMap;
import io.swagger.codegen.languages.KotlinClientCodegen;

import java.util.Map;

Expand All @@ -13,6 +14,7 @@ public class KotlinClientCodegenOptionsProvider implements OptionsProvider {
public static final String GROUP_ID = "io.swagger.tests";
public static final String SOURCE_FOLDER = "./generated/kotlin";
public static final String ENUM_PROPERTY_NAMING = "camelCase";
public static final String DATE_LIBRARY = KotlinClientCodegen.DateLibrary.JAVA8.value;

@Override
public String getLanguage() {
Expand All @@ -29,6 +31,7 @@ public Map<String, String> createOptions() {
.put(CodegenConstants.GROUP_ID, GROUP_ID)
.put(CodegenConstants.SOURCE_FOLDER, SOURCE_FOLDER)
.put(CodegenConstants.ENUM_PROPERTY_NAMING, ENUM_PROPERTY_NAMING)
.put(KotlinClientCodegen.DATE_LIBRARY, DATE_LIBRARY)
.build();
}

Expand Down

0 comments on commit 914275f

Please sign in to comment.