Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
7b543ec
Added support Retrofit2(RxJava/RxJava2/Coroutines) to Kotlin client c…
kuFEAR Mar 29, 2020
49ac48c
Added generated samples for Retrofit2(RxJava/RxJava2/Coroutines) on K…
kuFEAR Mar 29, 2020
8b074f4
Fixed generating retrofit2 without Rx/Coroutines
kuFEAR Mar 30, 2020
87745a9
Fixed MultipartBody template, remove redundant space after MultipartB…
kuFEAR Mar 30, 2020
684c966
Merge pull request #1 from OpenAPITools/master
kuFEAR Apr 2, 2020
82d9a88
Merge remote-tracking branch 'origin/master' into kotlin-rx-rx2-corou…
kuFEAR Apr 2, 2020
22f96f8
Fix documentation diff.
kuFEAR Apr 2, 2020
602ae5a
Fix generating build.gradle file for samples.
kuFEAR Apr 2, 2020
8e5434b
Update generated sample with rx dependencies
kuFEAR Apr 2, 2020
d6b568d
Update generated sample with coroutines
kuFEAR Apr 2, 2020
cd40923
Update generated sample with RxJava 2 dependencies
kuFEAR Apr 2, 2020
42b0abb
Update and refactoring scripts for sample generation
kuFEAR Apr 2, 2020
7f91ea8
Update generated sample code
kuFEAR Apr 2, 2020
7966a4c
revert changes by mistake
kuFEAR Apr 2, 2020
1d442c1
revert changes by mistake #2
kuFEAR Apr 2, 2020
f621481
Fix return type for first RxJava
kuFEAR Apr 2, 2020
62bac13
Add RxJavaCallAdapterFactory to ApiClient scheme for RxJava 1/2
kuFEAR Apr 2, 2020
21933ea
Fix script loggin
kuFEAR Apr 2, 2020
670346a
Update generated code for RxJava 1/2
kuFEAR Apr 2, 2020
83a5e3c
Fix kotlin.md documentation by script export_docs_generators.sh
kuFEAR Apr 2, 2020
ab87d13
Merge remote-tracking branch 'base_origin/master' into kotlin-rx-rx2-…
kuFEAR Apr 3, 2020
9de4d0b
Update Kotlin samples project in pom.xml
kuFEAR Apr 8, 2020
d132182
Revert "Update Kotlin samples project in pom.xml"
kuFEAR Apr 8, 2020
a4fde70
Fixed "" wrapping number types for annotation value
kuFEAR May 11, 2020
4efa4ac
Update Kotlin samples project after last fixes
kuFEAR May 11, 2020
031dce2
Update Kotlin samples project for kotlin-jvm-*
kuFEAR May 11, 2020
c26da0f
Update Kotlin readme.md documentations
kuFEAR May 11, 2020
4ece587
Update Kotlin client sample changes
kuFEAR May 11, 2020
9b42770
Fixed encoding braces for generics model
kuFEAR May 12, 2020
348edf6
Update Kotlin client sample changes
kuFEAR May 12, 2020
9e600cc
Merge remote-tracking branch 'base_origin/master' into kotlin-rx-rx2-…
kuFEAR May 12, 2020
19ebd7c
Update Kotlin client sample after merge with master
kuFEAR May 12, 2020
4f73f0a
Fixed adding empty braces for inherit from Map/Array
kuFEAR May 12, 2020
a1477f4
Update sample model after last fix with empty braces
kuFEAR May 12, 2020
313c1ce
Revert adding @SerializedName to Kotlin interface fields through @get…
kuFEAR May 12, 2020
6b3cc8f
Update Kotlin client samples with adding @get:SerializedName
kuFEAR May 12, 2020
fe094e5
Merge remote-tracking branch 'base_origin/master' into kotlin-rx-rx2-…
kuFEAR May 13, 2020
dc36be7
Merge remote-tracking branch 'base_origin/master' into kotlin-rx-rx2-…
kuFEAR May 13, 2020
028b9a5
Merge remote-tracking branch 'base_origin/master' into kotlin-rx-rx2-…
kuFEAR May 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
10 changes: 10 additions & 0 deletions bin/openapi3/kotlin-client-petstore-all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh


./bin/openapi3/kotlin-client-petstore.sh
./bin/openapi3/kotlin-client-petstore-multiplatform.sh
./bin/openapi3/kotlin-client-petstore-nullable-required.sh
./bin/openapi3/kotlin-client-petstore-retrofit2-coroutines.sh
./bin/openapi3/kotlin-client-petstore-retrofit2-rx.sh
./bin/openapi3/kotlin-client-petstore-retrofit2-rx2.sh

8 changes: 5 additions & 3 deletions bin/openapi3/kotlin-client-petstore-multiplatform.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ then
mvn clean package
fi

samplePath="samples/openapi3/client/petstore/kotlin-multiplatform"

export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml -t modules/openapi-generator/src/main/resources/kotlin-client -g kotlin --artifact-id kotlin-client-petstore-multiplatform --library multiplatform -o samples/openapi3/client/petstore/kotlin-multiplatform $@"
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml -t modules/openapi-generator/src/main/resources/kotlin-client -g kotlin --artifact-id kotlin-client-petstore-multiplatform --library multiplatform -o $samplePath $@"

echo "Cleaning previously generated files if any from samples/openapi3/client/petstore/kotlin-multiplatform"
rm -rf samples/openapi3/client/petstore/kotlin-multiplatform
echo "Cleaning previously generated files if any from $samplePath"
rm -rf $samplePath

echo "Generating Kotling client..."
java $JAVA_OPTS -jar $executable $ags
8 changes: 5 additions & 3 deletions bin/openapi3/kotlin-client-petstore-nullable-required.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ then
mvn clean package
fi

samplePath="samples/openapi3/client/petstore/kotlin-nullable-required"

export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore-with-nullable-required.yaml -t modules/openapi-generator/src/main/resources/kotlin-client -g kotlin --artifact-id kotlin-petstore-nullable-required --additional-properties allowRequiredAsNullable=true -o samples/openapi3/client/petstore/kotlin-nullable-required $@"
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore-with-nullable-required.yaml -t modules/openapi-generator/src/main/resources/kotlin-client -g kotlin --artifact-id kotlin-petstore-nullable-required --additional-properties allowRequiredAsNullable=true -o $samplePath $@"

echo "Cleaning previously generated files if any from samples/openapi3/client/petstore/kotlin-nullable-required"
rm -rf samples/openapi3/client/petstore/kotlin-nullable-required
echo "Cleaning previously generated files if any from $samplePath"
rm -rf $samplePath

echo "Generating Kotling client..."
java $JAVA_OPTS -jar $executable $ags
37 changes: 37 additions & 0 deletions bin/openapi3/kotlin-client-petstore-retrofit2-coroutines.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/sh

SCRIPT="$0"
echo "# START SCRIPT: $SCRIPT"

while [ -h "$SCRIPT" ] ; do
ls=$(ls -ld "$SCRIPT")
link=$(expr "$ls" : '.*-> \(.*\)$')
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=$(dirname "$SCRIPT")/"$link"
fi
done

if [ ! -d "${APP_DIR}" ]; then
APP_DIR=$(dirname "$SCRIPT")/..
APP_DIR=$(cd "${APP_DIR}"; pwd)
fi

executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"

if [ ! -f "$executable" ]
then
mvn clean package
fi

samplePath="samples/openapi3/client/petstore/kotlin-jvm-retrofit2-coroutines"

export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml -t modules/openapi-generator/src/main/resources/kotlin-client -g kotlin --artifact-id kotlin-petstore-coroutines-client --library jvm-retrofit2 --additional-properties serializationLibrary=gson,dateLibrary=java8,serializableModel=true,useCoroutines=true -o $samplePath $@"

echo "Cleaning previously generated files if any from $samplePath"
rm -rf $samplePath

echo "Generating Kotling client..."
java $JAVA_OPTS -jar $executable $ags
37 changes: 37 additions & 0 deletions bin/openapi3/kotlin-client-petstore-retrofit2-rx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/sh

SCRIPT="$0"
echo "# START SCRIPT: $SCRIPT"

while [ -h "$SCRIPT" ] ; do
ls=$(ls -ld "$SCRIPT")
link=$(expr "$ls" : '.*-> \(.*\)$')
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=$(dirname "$SCRIPT")/"$link"
fi
done

if [ ! -d "${APP_DIR}" ]; then
APP_DIR=$(dirname "$SCRIPT")/..
APP_DIR=$(cd "${APP_DIR}"; pwd)
fi

executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"

if [ ! -f "$executable" ]
then
mvn clean package
fi

samplePath="samples/openapi3/client/petstore/kotlin-jvm-retrofit2-rx"

export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml -t modules/openapi-generator/src/main/resources/kotlin-client -g kotlin --artifact-id kotlin-petstore-rx-client --library jvm-retrofit2 --additional-properties serializationLibrary=gson,dateLibrary=java8,serializableModel=true,useRxJava=true -o $samplePath $@"

echo "Cleaning previously generated files if any from $samplePath"
rm -rf $samplePath

echo "Generating Kotling client..."
java $JAVA_OPTS -jar $executable $ags
37 changes: 37 additions & 0 deletions bin/openapi3/kotlin-client-petstore-retrofit2-rx2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/sh

SCRIPT="$0"
echo "# START SCRIPT: $SCRIPT"

while [ -h "$SCRIPT" ] ; do
ls=$(ls -ld "$SCRIPT")
link=$(expr "$ls" : '.*-> \(.*\)$')
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=$(dirname "$SCRIPT")/"$link"
fi
done

if [ ! -d "${APP_DIR}" ]; then
APP_DIR=$(dirname "$SCRIPT")/..
APP_DIR=$(cd "${APP_DIR}"; pwd)
fi

executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"

if [ ! -f "$executable" ]
then
mvn clean package
fi

samplePath="samples/openapi3/client/petstore/kotlin-jvm-retrofit2-rx2"

export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml -t modules/openapi-generator/src/main/resources/kotlin-client -g kotlin --artifact-id kotlin-petstore-rx2-client --library jvm-retrofit2 --additional-properties serializationLibrary=gson,dateLibrary=java8,serializableModel=true,useRxJava2=true -o $samplePath $@"

echo "Cleaning previously generated files if any from $samplePath"
rm -rf $samplePath

echo "Generating Kotling client..."
java $JAVA_OPTS -jar $executable $ags
8 changes: 5 additions & 3 deletions bin/openapi3/kotlin-client-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ then
mvn clean package
fi

samplePath="samples/openapi3/client/petstore/kotlin"

export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml -t modules/openapi-generator/src/main/resources/kotlin-client -g kotlin --artifact-id kotlin-petstore-client --additional-properties dateLibrary=java8,serializableModel=true -o samples/openapi3/client/petstore/kotlin $@"
ags="generate -i modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml -t modules/openapi-generator/src/main/resources/kotlin-client -g kotlin --artifact-id kotlin-petstore-client --additional-properties dateLibrary=java8,serializableModel=true -o $samplePath $@"

echo "Cleaning previously generated files if any from samples/openapi3/client/petstore/kotlin"
rm -rf samples/openapi3/client/petstore/kotlin
echo "Cleaning previously generated files if any from $samplePath"
rm -rf $samplePath

echo "Generating Kotling client..."
java $JAVA_OPTS -jar $executable $ags
6 changes: 3 additions & 3 deletions docs/generators/kotlin-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ sidebar_label: kotlin-server

| Type/Alias | Instantiated By |
| ---------- | --------------- |
|array|kotlin.arrayOf|
|list|kotlin.arrayOf|
|map|kotlin.mapOf|
|array|kotlin.Array|
|list|kotlin.collections.ArrayList|
|map|kotlin.collections.HashMap|


## LANGUAGE PRIMITIVES
Expand Down
6 changes: 3 additions & 3 deletions docs/generators/kotlin-spring.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ sidebar_label: kotlin-spring

| Type/Alias | Instantiated By |
| ---------- | --------------- |
|array|kotlin.arrayOf|
|list|kotlin.arrayOf|
|map|kotlin.mapOf|
|array|kotlin.Array|
|list|kotlin.collections.ArrayList|
|map|kotlin.collections.HashMap|


## LANGUAGE PRIMITIVES
Expand Down
6 changes: 3 additions & 3 deletions docs/generators/kotlin-vertx.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ sidebar_label: kotlin-vertx

| Type/Alias | Instantiated By |
| ---------- | --------------- |
|array|kotlin.arrayOf|
|list|kotlin.arrayOf|
|map|kotlin.mapOf|
|array|kotlin.Array|
|list|kotlin.collections.ArrayList|
|map|kotlin.collections.HashMap|


## LANGUAGE PRIMITIVES
Expand Down
9 changes: 6 additions & 3 deletions docs/generators/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ sidebar_label: kotlin
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |null|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |null|
|sourceFolder|source folder for generated code| |src/main/kotlin|
|useCoroutines|Whether to use the Coroutines adapter with the retrofit2 library.| |false|
|useRxJava|Whether to use the RxJava adapter with the retrofit2 library.| |false|
|useRxJava2|Whether to use the RxJava2 adapter with the retrofit2 library.| |false|

## IMPORT MAPPING

Expand All @@ -43,9 +46,9 @@ sidebar_label: kotlin

| Type/Alias | Instantiated By |
| ---------- | --------------- |
|array|kotlin.arrayOf|
|list|kotlin.arrayOf|
|map|kotlin.mapOf|
|array|kotlin.Array|
|list|kotlin.collections.ArrayList|
|map|kotlin.collections.HashMap|


## LANGUAGE PRIMITIVES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ public AbstractKotlinCodegen() {
typeMapping.put("Date", "java.time.LocalDate");
typeMapping.put("DateTime", "java.time.LocalDateTime");

instantiationTypes.put("array", "kotlin.arrayOf");
instantiationTypes.put("list", "kotlin.arrayOf");
instantiationTypes.put("map", "kotlin.mapOf");
instantiationTypes.put("array", "kotlin.Array");
instantiationTypes.put("list", "kotlin.collections.ArrayList");
instantiationTypes.put("map", "kotlin.collections.HashMap");

importMapping = new HashMap<String, String>();
importMapping.put("BigDecimal", "java.math.BigDecimal");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,32 @@
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.meta.features.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class KotlinClientCodegen extends AbstractKotlinCodegen {

private static final Logger LOGGER = LoggerFactory.getLogger(KotlinClientCodegen.class);

protected static final String JVM = "jvm";
protected static final String JVM_OKHTTP = "jvm-okhttp";
protected static final String JVM_OKHTTP4 = "jvm-okhttp4";
protected static final String JVM_OKHTTP3 = "jvm-okhttp3";
protected static final String JVM_RETROFIT2 = "jvm-retrofit2";
protected static final String MULTIPLATFORM = "multiplatform";

public static final String USE_RX_JAVA = "useRxJava";
public static final String USE_RX_JAVA2 = "useRxJava2";
public static final String USE_COROUTINES = "useCoroutines";
public static final String DO_NOT_USE_RX_AND_COROUTINES = "doNotUseRxAndCoroutines";

public static final String DATE_LIBRARY = "dateLibrary";
public static final String REQUEST_DATE_CONVERTER = "requestDateConverter";
public static final String COLLECTION_TYPE = "collectionType";
Expand All @@ -53,6 +61,12 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
protected String dateLibrary = DateLibrary.JAVA8.value;
protected String requestDateConverter = RequestDateConverter.TO_JSON.value;
protected String collectionType = CollectionType.ARRAY.value;
protected boolean useRxJava = false;
protected boolean useRxJava2 = false;
protected boolean useCoroutines = false;
// backwards compatibility for openapi configs that specify neither rx1 nor rx2
// (mustache does not allow for boolean operators so we need this extra field)
protected boolean doNotUseRxAndCoroutines = true;

public enum DateLibrary {
STRING("string"),
Expand Down Expand Up @@ -177,6 +191,10 @@ public KotlinClientCodegen() {
requestDateConverter.setEnum(requestDateConverterOptions);
requestDateConverter.setDefault(this.requestDateConverter);
cliOptions.add(requestDateConverter);

cliOptions.add(CliOption.newBoolean(USE_RX_JAVA, "Whether to use the RxJava adapter with the retrofit2 library."));
cliOptions.add(CliOption.newBoolean(USE_RX_JAVA2, "Whether to use the RxJava2 adapter with the retrofit2 library."));
cliOptions.add(CliOption.newBoolean(USE_COROUTINES, "Whether to use the Coroutines adapter with the retrofit2 library."));
}

public CodegenType getTag() {
Expand All @@ -191,6 +209,43 @@ public String getHelp() {
return "Generates a Kotlin client.";
}

public void setUseRxJava(boolean useRxJava) {
if (useRxJava) {
this.useRxJava2 = false;
this.doNotUseRxAndCoroutines = false;
this.useCoroutines = false;
}
this.useRxJava = useRxJava;
}

public void setUseRxJava2(boolean useRxJava2) {
if (useRxJava2) {
this.useRxJava = false;
this.doNotUseRxAndCoroutines = false;
this.useCoroutines = false;
}
this.useRxJava2 = useRxJava2;
}

public void setDoNotUseRxAndCoroutines(boolean doNotUseRxAndCoroutines) {
if (doNotUseRxAndCoroutines) {
this.useRxJava = false;
this.useRxJava2 = false;
this.useCoroutines = false;
}
this.doNotUseRxAndCoroutines = doNotUseRxAndCoroutines;
}

public void setUseCoroutines(boolean useCoroutines) {
if (useCoroutines) {
this.useRxJava = false;
this.useRxJava2 = false;
this.doNotUseRxAndCoroutines = false;
}
this.useCoroutines = useCoroutines;
}


public void setDateLibrary(String library) {
this.dateLibrary = library;
}
Expand All @@ -211,6 +266,38 @@ public void processOpts() {
sourceFolder = "src/commonMain/kotlin";
}


boolean hasRx = additionalProperties.containsKey(USE_RX_JAVA);
boolean hasRx2 = additionalProperties.containsKey(USE_RX_JAVA2);
boolean hasCoroutines = additionalProperties.containsKey(USE_COROUTINES);
int optionCount = 0;
if (hasRx) {
optionCount++;
}
if (hasRx2) {
optionCount++;
}
if (hasCoroutines) {
optionCount++;
}
boolean hasConflict = optionCount > 1;

// RxJava & Coroutines
if (hasConflict) {
LOGGER.warn("You specified both RxJava versions 1 and 2 or Coroutines together, please choose one them.");
} else if (hasRx) {
this.setUseRxJava(Boolean.valueOf(additionalProperties.get(USE_RX_JAVA).toString()));
} else if (hasRx2) {
this.setUseRxJava2(Boolean.valueOf(additionalProperties.get(USE_RX_JAVA2).toString()));
} else if (hasCoroutines) {
this.setUseCoroutines(Boolean.valueOf(additionalProperties.get(USE_COROUTINES).toString()));
}

if (!hasRx && !hasRx2 && !hasCoroutines) {
setDoNotUseRxAndCoroutines(true);
additionalProperties.put(DO_NOT_USE_RX_AND_COROUTINES, true);
}

// infrastructure destination folder
final String infrastructureFolder = (sourceFolder + File.separator + packageName + File.separator + "infrastructure").replace(".", "/");

Expand Down
Loading