forked from loafle/openapi-generator-original
Kotlin collection type (#1564)
* Allow specifying type * Add test * Update docs * Don't modify types if the default option is chosen
This commit is contained in:
parent
41185d3c6f
commit
777bf1f3aa
@ -28,4 +28,9 @@ CONFIG OPTIONS for kotlin
|
|||||||
java8 - Java 8 native JSR310
|
java8 - Java 8 native JSR310
|
||||||
threetenbp - Threetenbp
|
threetenbp - Threetenbp
|
||||||
|
|
||||||
|
collectionType
|
||||||
|
Option. Collection type to use
|
||||||
|
array - kotlin.Array
|
||||||
|
list - kotlin.collections.List
|
||||||
|
|
||||||
Back to the [generators list](README.md)
|
Back to the [generators list](README.md)
|
||||||
|
@ -31,9 +31,11 @@ import java.util.Map;
|
|||||||
public class KotlinClientCodegen extends AbstractKotlinCodegen {
|
public class KotlinClientCodegen extends AbstractKotlinCodegen {
|
||||||
|
|
||||||
public static final String DATE_LIBRARY = "dateLibrary";
|
public static final String DATE_LIBRARY = "dateLibrary";
|
||||||
|
public static final String COLLECTION_TYPE = "collectionType";
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(KotlinClientCodegen.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(KotlinClientCodegen.class);
|
||||||
|
|
||||||
protected String dateLibrary = DateLibrary.JAVA8.value;
|
protected String dateLibrary = DateLibrary.JAVA8.value;
|
||||||
|
protected String collectionType = CollectionType.ARRAY.value;
|
||||||
|
|
||||||
public enum DateLibrary {
|
public enum DateLibrary {
|
||||||
STRING("string"),
|
STRING("string"),
|
||||||
@ -47,6 +49,17 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum CollectionType {
|
||||||
|
ARRAY("array"),
|
||||||
|
LIST("list");
|
||||||
|
|
||||||
|
public final String value;
|
||||||
|
|
||||||
|
CollectionType(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an instance of `KotlinClientCodegen`.
|
* Constructs an instance of `KotlinClientCodegen`.
|
||||||
*/
|
*/
|
||||||
@ -74,6 +87,13 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
|
|||||||
dateOptions.put(DateLibrary.JAVA8.value, "Java 8 native JSR310");
|
dateOptions.put(DateLibrary.JAVA8.value, "Java 8 native JSR310");
|
||||||
dateLibrary.setEnum(dateOptions);
|
dateLibrary.setEnum(dateOptions);
|
||||||
cliOptions.add(dateLibrary);
|
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() {
|
public CodegenType getTag() {
|
||||||
@ -92,6 +112,10 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
|
|||||||
this.dateLibrary = library;
|
this.dateLibrary = library;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setCollectionType(String collectionType) {
|
||||||
|
this.collectionType = collectionType;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void processOpts() {
|
public void processOpts() {
|
||||||
super.processOpts();
|
super.processOpts();
|
||||||
@ -116,6 +140,15 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
|
|||||||
additionalProperties.put(DateLibrary.JAVA8.value, true);
|
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("README.mustache", "", "README.md"));
|
||||||
supportingFiles.add(new SupportingFile("build.gradle.mustache", "", "build.gradle"));
|
supportingFiles.add(new SupportingFile("build.gradle.mustache", "", "build.gradle"));
|
||||||
supportingFiles.add(new SupportingFile("settings.gradle.mustache", "", "settings.gradle"));
|
supportingFiles.add(new SupportingFile("settings.gradle.mustache", "", "settings.gradle"));
|
||||||
|
@ -199,6 +199,33 @@ public class KotlinClientCodegenModelTest {
|
|||||||
Assert.assertTrue(property.isContainer);
|
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")
|
@Test(description = "convert a model with a map property")
|
||||||
public void mapPropertyTest() {
|
public void mapPropertyTest() {
|
||||||
final Schema schema = getMapSchema();
|
final Schema schema = getMapSchema();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user