Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/workflows/samples-kotlin-client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ jobs:
- samples/client/petstore/kotlin-string
- samples/client/petstore/kotlin-threetenbp
- samples/client/petstore/kotlin-uppercase-enum
- samples/client/petstore/kotlin-array-simple-string
- samples/client/petstore/kotlin-bigdecimal-default
- samples/client/petstore/kotlin-default-values-jvm-okhttp3
- samples/client/petstore/kotlin-default-values-jvm-okhttp4
- samples/client/petstore/kotlin-default-values-jvm-retrofit2
- samples/client/petstore/kotlin-default-values-jvm-volley
- samples/client/petstore/kotlin-default-values-multiplatform
- samples/client/petstore/kotlin-array-simple-string-jvm-okhttp3
- samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4
- samples/client/petstore/kotlin-array-simple-string-jvm-volley
- samples/client/petstore/kotlin-array-simple-string-multiplatform
- samples/client/petstore/kotlin-bigdecimal-default-multiplatform
- samples/client/petstore/kotlin-bigdecimal-default-okhttp4
- samples/client/petstore/kotlin-jvm-ktor-jackson
- samples/client/petstore/kotlin-jvm-ktor-gson
steps:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
generatorName: kotlin
outputDir: samples/client/petstore/kotlin-array-simple-string
outputDir: samples/client/petstore/kotlin-array-simple-string-jvm-okhttp3
inputSpec: modules/openapi-generator/src/test/resources/3_0/issue_7199_array_simple_string.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-client
additionalProperties:
artifactId: kotlin-array-simple-string
artifactId: kotlin-array-simple-string-jvm-okhttp3
library: jvm-okhttp3
7 changes: 7 additions & 0 deletions bin/configs/kotlin-array-simple-string-jvm-okhttp4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
generatorName: kotlin
outputDir: samples/client/petstore/kotlin-array-simple-string-jvm-okhttp4
inputSpec: modules/openapi-generator/src/test/resources/3_0/issue_7199_array_simple_string.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-client
additionalProperties:
artifactId: kotlin-array-simple-string-jvm-okhttp4
library: jvm-okhttp4
9 changes: 9 additions & 0 deletions bin/configs/kotlin-array-simple-string-jvm-volley.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
generatorName: kotlin
outputDir: samples/client/petstore/kotlin-array-simple-string-jvm-volley
inputSpec: modules/openapi-generator/src/test/resources/3_0/issue_7199_array_simple_string.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-client
additionalProperties:
artifactId: kotlin-array-simple-string-jvm-volley
library: jvm-volley
serializationLibrary: gson
generateRoomModels: false
7 changes: 7 additions & 0 deletions bin/configs/kotlin-array-simple-string-multiplatform.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
generatorName: kotlin
outputDir: samples/client/petstore/kotlin-array-simple-string-multiplatform
inputSpec: modules/openapi-generator/src/test/resources/3_0/issue_7199_array_simple_string.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-client
additionalProperties:
artifactId: kotlin-array-simple-string-multiplatform
library: multiplatform
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,20 @@ import {{packageName}}.infrastructure.toMultiValue
{{#enumVars}}
{{^multiplatform}}
{{#moshi}}
@Json(name = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}),
@Json(name = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}
{{/moshi}}
{{#gson}}
@SerializedName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}),
@SerializedName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}
{{/gson}}
{{#jackson}}
@JsonProperty(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}),
@JsonProperty(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}
{{/jackson}}
{{#kotlinx_serialization}}
@SerialName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}),
@SerialName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}
{{/kotlinx_serialization}}
{{/multiplatform}}
{{#multiplatform}}
@SerialName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}),
@SerialName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}
{{/multiplatform}}
{{/enumVars}}
{{/allowableValues}}
Expand Down Expand Up @@ -109,7 +109,6 @@ import {{packageName}}.infrastructure.toMultiValue
{{/enumVars}}
{{/allowableValues}}
{{/enumUnknownDefaultCase}}
;
}

{{/isEnum}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import rx.Observable
import io.reactivex.Single
{{/useRxJava2}}
{{#useRxJava3}}
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.core.Single
{{/useRxJava3}}
{{^returnType}}
{{#useRxJava2}}
import io.reactivex.Completable
{{/useRxJava2}}
{{#useRxJava3}}
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Completable
{{/useRxJava3}}
{{/returnType}}
{{/doNotUseRxAndCoroutines}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class OAuth(
) : this(
OAuthClientRequest.tokenLocation(tokenUrl).setScope(scopes)
) {
setFlow(flow);
authenticationRequestBuilder = OAuthClientRequest.authorizationLocation(authorizationUrl);
setFlow(flow)
authenticationRequestBuilder = OAuthClientRequest.authorizationLocation(authorizationUrl)
}

fun setFlow(flow: OAuthFlow) {
Expand Down Expand Up @@ -146,6 +146,6 @@ class OAuth(
throw IOException(e)
}
}
return true;
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,9 @@ import okhttp3.MediaType.Companion.toMediaType
private val defaultClientBuilder: OkHttpClient.Builder by lazy {
OkHttpClient()
.newBuilder()
.addInterceptor(HttpLoggingInterceptor(object : HttpLoggingInterceptor.Logger {
override fun log(message: String) {
logger?.invoke(message)
}
}).apply {
level = HttpLoggingInterceptor.Level.BODY
})
.addInterceptor(HttpLoggingInterceptor { message -> logger?.invoke(message) }
.apply { level = HttpLoggingInterceptor.Level.BODY }
)
Comment on lines 103 to 105

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplifying code

}

init {
Expand All @@ -125,7 +121,7 @@ import okhttp3.MediaType.Companion.toMediaType
{{#authMethods}}"{{name}}" -> {{#isBasic}}{{#isBasicBasic}}HttpBasicAuth(){{/isBasicBasic}}{{#isBasicBearer}}HttpBearerAuth("{{scheme}}"){{/isBasicBearer}}{{/isBasic}}{{#isApiKey}}ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{#isKeyInQuery}}"query"{{/isKeyInQuery}}{{#isKeyInCookie}}"cookie"{{/isKeyInCookie}}, "{{keyParamName}}"){{/isApiKey}}{{#isOAuth}}OAuth(OAuthFlow.{{flow}}, "{{authorizationUrl}}", "{{tokenUrl}}", "{{#scopes}}{{scope}}{{^-last}}, {{/-last}}{{/scopes}}"){{/isOAuth}}{{/authMethods}}
else -> throw RuntimeException("auth name $authName not found in available auth names")
}
addAuthorization(authName, auth);
addAuthorization(authName, auth)
}
}

Expand Down Expand Up @@ -182,7 +178,7 @@ import okhttp3.MediaType.Companion.toMediaType
{{#isBasicBasic}}
fun setCredentials(username: String, password: String): ApiClient {
apiAuthorizations.values.runOnFirst<Interceptor, HttpBasicAuth> {
setCredentials(username, password);
setCredentials(username, password)
}
{{#hasOAuthMethods}}
apiAuthorizations.values.runOnFirst<Interceptor, OAuth> {
Expand Down Expand Up @@ -269,7 +265,7 @@ import okhttp3.MediaType.Companion.toMediaType
?.setClientId(clientId)
?.setRedirectURI(redirectURI)
}
return this;
return this
}

/**
Expand All @@ -281,7 +277,7 @@ import okhttp3.MediaType.Companion.toMediaType
apiAuthorizations.values.runOnFirst<Interceptor, OAuth> {
registerAccessTokenListener(accessTokenListener)
}
return this;
return this
}

{{/hasOAuthMethods}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.toolbox.BaseHttpStack
import com.android.volley.toolbox.Volley
import java.util.*;
import java.util.*
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
Expand Down Expand Up @@ -52,47 +52,34 @@ class {{classname}} (
{{^bodyParam}}
val body: Any? = null
{{/bodyParam}}
{{#allParams}}
{{#required}}
// verify the required parameter '{{paramName}}' is set
// This is probably taken care of by non-null types anyway
requireNotNull({{paramName}})
{{/required}}
{{/allParams}}
Comment on lines 55 to 61

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Kotlin type systems makes sure that the parameters cannot be null. Completely redundant check.


val contentTypes : Array<String> = arrayOf({{#consumes}}"{{{mediaType}}}"{{^-last}},{{/-last}}{{/consumes}})
val contentType: String = if (contentTypes.isNotEmpty()) { contentTypes.first() } else { "application/json" }

// Do some work or avoid some work based on what we know about the model,
// Do some work or avoid some work based on what we know about the model,
// before we delegate to a pluggable request factory template
// The request factory template contains only pure code and no templates
// to make it easy to override with your own.

// create path and map variables
val path = "{{{path}}}"{{#pathParams}}.replace("{" + "{{baseName}}" + "}", IRequestFactory.escapeString({{{paramName}}}.toString())){{/pathParams}};
val path = "{{{path}}}"{{#pathParams}}.replace("{" + "{{baseName}}" + "}", {{#isContainer}}{{paramName}}.joinToString(","){{/isContainer}}{{^isContainer}}IRequestFactory.escapeString({{{paramName}}}.toString()){{/isContainer}}){{/pathParams}}
Comment on lines 72 to 65

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bugfix (#7199) to handle that you can't use toString() on lists


// form params
val formParams = mapOf<String, String>(
{{#formParams}}
"{{baseName}}" to IRequestFactory.parameterToString({{paramName}}),
{{/formParams}}
)
val formParams = mapOf<String, String>({{^formParams}}){{/formParams}}{{#formParams}}
"{{baseName}}" to IRequestFactory.parameterToString({{paramName}}){{^-last}},{{/-last}}{{#-last}}
){{/-last}}{{/formParams}}


// TODO: Cater for allowing empty values
// TODO, if its apikey auth, then add the header names here and the hardcoded auth key
// Only support hard coded apikey in query param auth for when we do this first path
val queryParams = mapOf<String, String>(
{{#queryParams}}
"{{baseName}}" to IRequestFactory.parameterToString({{paramName}}),
{{/queryParams}}
).filter { it.value.isNotEmpty() }

val headerParams: Map<String, String> = mapOf(
{{#headerParams}}
"{{baseName}}" to IRequestFactory.parameterToString({{paramName}}),
{{/headerParams}}
)
val queryParams = mapOf<String, String>({{^queryParams}}){{/queryParams}}{{#queryParams}}
"{{baseName}}" to IRequestFactory.parameterToString({{paramName}}){{^-last}},{{/-last}}{{#-last}}
){{/-last}}{{/queryParams}}
.filter { it.value.isNotEmpty() }

val headerParams: Map<String, String> = mapOf({{^headerParams}}){{/headerParams}}{{#headerParams}}
"{{baseName}}" to IRequestFactory.parameterToString({{paramName}}){{^-last}},{{/-last}}{{#-last}}
){{/-last}}{{/headerParams}}

return suspendCoroutine { continuation ->
val responseListener = Response.Listener<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}}> { response ->
Expand All @@ -119,7 +106,7 @@ class {{classname}} (
responseListener,
errorListener)

postProcessors.forEach{ it.invoke(request)}
postProcessors.forEach { it.invoke(request) }

requestQueue.value.add(request)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ project.version = '{{artifactVersion}}'
buildscript {

ext.kotlin_version = '1.5.10'

ext.swagger_annotations_version = "1.6.2"

ext.gson_version = "2.8.6"

ext.volley_version = "1.2.0"

ext.junit_version = "4.13.2"

ext.robolectric_version = "4.5.1"

ext.concurrent_unit_version = "0.4.6"

repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{#generateRoomModels}}
android.useAndroidX=true
{{#generateRoomModels}}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jvm-volley refuses to build without android.useAndroidX=true, even if generateRoomModels is false

android.enableJetifier=true
{{/generateRoomModels}}
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,14 @@ import kotlinx.serialization.encoding.*
{{/hasFormParams}}
{{/hasBodyParam}}

val localVariableQuery = mutableMapOf<String, List<String>>()
{{#queryParams}}
{{{paramName}}}?.apply { localVariableQuery["{{baseName}}"] = {{#isContainer}}toMultiValue(this, "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf("${{{paramName}}}"){{/isContainer}} }
{{/queryParams}}

val localVariableHeaders = mutableMapOf<String, String>()
{{#headerParams}}
{{{paramName}}}?.apply { localVariableHeaders["{{baseName}}"] = {{#isContainer}}this.joinToString(separator = collectionDelimiter("{{collectionFormat}}")){{/isContainer}}{{^isContainer}}this.toString(){{/isContainer}} }
{{/headerParams}}
val localVariableQuery = mutableMapOf<String, List<String>>(){{#queryParams}}
{{{paramName}}}?.apply { localVariableQuery["{{baseName}}"] = {{#isContainer}}toMultiValue(this, "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}listOf("${{{paramName}}}"){{/isContainer}} }{{/queryParams}}
val localVariableHeaders = mutableMapOf<String, String>(){{#headerParams}}
{{{paramName}}}?.apply { localVariableHeaders["{{baseName}}"] = {{#isContainer}}this.joinToString(separator = collectionDelimiter("{{collectionFormat}}")){{/isContainer}}{{^isContainer}}this.toString(){{/isContainer}} }{{/headerParams}}

val localVariableConfig = RequestConfig<kotlin.Any?>(
RequestMethod.{{httpMethod}},
"{{path}}"{{#pathParams}}.replace("{" + "{{baseName}}" + "}", "${{{paramName}}}"){{/pathParams}},
"{{path}}"{{#pathParams}}.replace("{" + "{{baseName}}" + "}", {{#isContainer}}{{paramName}}.joinToString(","){{/isContainer}}{{^isContainer}}"${{{paramName}}}"{{/isContainer}}){{/pathParams}},
Comment on lines 75 to 70

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bugfix (#7199) to handle that you can't use toString() on lists

query = localVariableQuery,
headers = localVariableHeaders
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "com.squareup.moshi:moshi-kotlin:1.12.0"
implementation "com.squareup.moshi:moshi-adapters:1.12.0"
implementation "com.squareup.okhttp3:okhttp:4.9.1"
implementation "com.squareup.okhttp3:okhttp:3.12.13"
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

rootProject.name = 'kotlin-array-simple-string-jvm-okhttp3'
Loading