|
17 | 17 |
|
18 | 18 | package org.openapitools.codegen.languages; |
19 | 19 |
|
| 20 | +import java.io.File; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.function.Predicate; |
| 25 | +import java.util.stream.Collectors; |
| 26 | +import java.util.stream.Stream; |
| 27 | + |
20 | 28 | import org.apache.commons.lang3.StringUtils; |
21 | 29 | import org.openapitools.codegen.CliOption; |
22 | 30 | import org.openapitools.codegen.CodegenConstants; |
|
41 | 49 | import org.slf4j.Logger; |
42 | 50 | import org.slf4j.LoggerFactory; |
43 | 51 |
|
44 | | -import java.io.File; |
45 | | -import java.util.HashMap; |
46 | | -import java.util.List; |
47 | | -import java.util.Map; |
48 | | -import java.util.function.Predicate; |
49 | | -import java.util.stream.Collectors; |
50 | | -import java.util.stream.Stream; |
51 | | - |
52 | 52 | import static java.util.Collections.sort; |
53 | 53 |
|
54 | 54 | public class KotlinClientCodegen extends AbstractKotlinCodegen { |
55 | 55 |
|
56 | 56 | private final Logger LOGGER = LoggerFactory.getLogger(KotlinClientCodegen.class); |
57 | 57 |
|
58 | 58 | protected static final String JVM = "jvm"; |
| 59 | + protected static final String JVM_KTOR = "jvm-ktor"; |
59 | 60 | protected static final String JVM_OKHTTP = "jvm-okhttp"; |
60 | 61 | protected static final String JVM_OKHTTP4 = "jvm-okhttp4"; |
61 | 62 | protected static final String JVM_OKHTTP3 = "jvm-okhttp3"; |
@@ -206,10 +207,11 @@ public KotlinClientCodegen() { |
206 | 207 | collectionType.setDefault(this.collectionType); |
207 | 208 | cliOptions.add(collectionType); |
208 | 209 |
|
| 210 | + supportedLibraries.put(JVM_KTOR, "Platform: Java Virtual Machine. HTTP client: Ktor 1.6.7. JSON processing: Gson, Jackson (default)."); |
209 | 211 | supportedLibraries.put(JVM_OKHTTP4, "[DEFAULT] Platform: Java Virtual Machine. HTTP client: OkHttp 4.2.0 (Android 5.0+ and Java 8+). JSON processing: Moshi 1.8.0."); |
210 | 212 | supportedLibraries.put(JVM_OKHTTP3, "Platform: Java Virtual Machine. HTTP client: OkHttp 3.12.4 (Android 2.3+ and Java 7+). JSON processing: Moshi 1.8.0."); |
211 | 213 | supportedLibraries.put(JVM_RETROFIT2, "Platform: Java Virtual Machine. HTTP client: Retrofit 2.6.2."); |
212 | | - supportedLibraries.put(MULTIPLATFORM, "Platform: Kotlin multiplatform. HTTP client: Ktor 1.6.0. JSON processing: Kotlinx Serialization: 1.2.1."); |
| 214 | + supportedLibraries.put(MULTIPLATFORM, "Platform: Kotlin multiplatform. HTTP client: Ktor 1.6.7. JSON processing: Kotlinx Serialization: 1.2.1."); |
213 | 215 | supportedLibraries.put(JVM_VOLLEY, "Platform: JVM for Android. HTTP client: Volley 1.2.1. JSON processing: gson 2.8.9"); |
214 | 216 |
|
215 | 217 | CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "Library template (sub-template) to use"); |
@@ -419,6 +421,9 @@ public void processOpts() { |
419 | 421 | commonSupportingFiles(); |
420 | 422 |
|
421 | 423 | switch (getLibrary()) { |
| 424 | + case JVM_KTOR: |
| 425 | + processJVMKtorLibrary(infrastructureFolder); |
| 426 | + break; |
422 | 427 | case JVM_OKHTTP3: |
423 | 428 | case JVM_OKHTTP4: |
424 | 429 | processJVMOkHttpLibrary(infrastructureFolder); |
@@ -630,6 +635,32 @@ private void addSupportingSerializerAdapters(final String infrastructureFolder) |
630 | 635 | } |
631 | 636 | } |
632 | 637 |
|
| 638 | + private void processJVMKtorLibrary(final String infrastructureFolder) { |
| 639 | + // in future kotlinx.serialization may be added |
| 640 | + if (this.serializationLibrary != SERIALIZATION_LIBRARY_TYPE.gson && this.serializationLibrary != SERIALIZATION_LIBRARY_TYPE.jackson) { |
| 641 | + this.serializationLibrary = SERIALIZATION_LIBRARY_TYPE.jackson; |
| 642 | + } |
| 643 | + |
| 644 | + additionalProperties.put(JVM, true); |
| 645 | + additionalProperties.put(JVM_KTOR, true); |
| 646 | + |
| 647 | + defaultIncludes.add("io.ktor.client.request.forms.InputProvider"); |
| 648 | + |
| 649 | + importMapping.put("InputProvider", "io.ktor.client.request.forms.InputProvider"); |
| 650 | + |
| 651 | + supportingFiles.add(new SupportingFile("infrastructure/ApiAbstractions.kt.mustache", infrastructureFolder, "ApiAbstractions.kt")); |
| 652 | + supportingFiles.add(new SupportingFile("infrastructure/ApiClient.kt.mustache", infrastructureFolder, "ApiClient.kt")); |
| 653 | + supportingFiles.add(new SupportingFile("infrastructure/HttpResponse.kt.mustache", infrastructureFolder, "HttpResponse.kt")); |
| 654 | + supportingFiles.add(new SupportingFile("infrastructure/RequestConfig.kt.mustache", infrastructureFolder, "RequestConfig.kt")); |
| 655 | + supportingFiles.add(new SupportingFile("infrastructure/RequestMethod.kt.mustache", infrastructureFolder, "RequestMethod.kt")); |
| 656 | + |
| 657 | + supportingFiles.add(new SupportingFile("auth/ApiKeyAuth.kt.mustache", authFolder, "ApiKeyAuth.kt")); |
| 658 | + supportingFiles.add(new SupportingFile("auth/Authentication.kt.mustache", authFolder, "Authentication.kt")); |
| 659 | + supportingFiles.add(new SupportingFile("auth/HttpBasicAuth.kt.mustache", authFolder, "HttpBasicAuth.kt")); |
| 660 | + supportingFiles.add(new SupportingFile("auth/HttpBearerAuth.kt.mustache", authFolder, "HttpBearerAuth.kt")); |
| 661 | + supportingFiles.add(new SupportingFile("auth/OAuth.kt.mustache", authFolder, "OAuth.kt")); |
| 662 | + } |
| 663 | + |
633 | 664 | private void processJVMOkHttpLibrary(final String infrastructureFolder) { |
634 | 665 | commonJvmMultiplatformSupportingFiles(infrastructureFolder); |
635 | 666 | addSupportingSerializerAdapters(infrastructureFolder); |
@@ -842,8 +873,8 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo |
842 | 873 | }); |
843 | 874 | } |
844 | 875 |
|
845 | | - // modify the data type of binary form parameters to a more friendly type for multiplatform builds |
846 | | - if (MULTIPLATFORM.equals(getLibrary()) && operation.allParams != null) { |
| 876 | + // modify the data type of binary form parameters to a more friendly type for ktor builds |
| 877 | + if ((JVM_KTOR.equals(getLibrary()) || MULTIPLATFORM.equals(getLibrary())) && operation.allParams != null) { |
847 | 878 | for (CodegenParameter param : operation.allParams) { |
848 | 879 | if (param.dataFormat != null && param.dataFormat.equals("binary")) { |
849 | 880 | param.baseType = param.dataType = "io.ktor.client.request.forms.InputProvider"; |
|
0 commit comments