Skip to content

Commit

Permalink
Kotlin collection type (OpenAPITools#1564)
Browse files Browse the repository at this point in the history
* Allow specifying type

* Add test

* Update docs

* Don't modify types if the default option is chosen
  • Loading branch information
absimas authored and wing328 committed Nov 29, 2018
1 parent a0d32c4 commit 5b58289
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/generators/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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`.
*/
Expand Down Expand Up @@ -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<String, String> 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() {
Expand All @@ -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();
Expand All @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<kotlin.String>");
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();
Expand Down

0 comments on commit 5b58289

Please sign in to comment.