[BUG][Kotlin-client] Handling default values of parameters (#12255)

* Bugfix Kotlin-client: Handling default values of parameters

* Adding object
This commit is contained in:
Johan Sjöblom
2022-04-29 14:26:29 +00:00
committed by GitHub
parent b6a8037f62
commit 8950a9a3c0
221 changed files with 12616 additions and 5807 deletions

View File

@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.
# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md

View File

@@ -0,0 +1,23 @@
README.md
build.gradle
docs/Apa.md
docs/DefaultApi.md
gradle/wrapper/gradle-wrapper.jar
gradle/wrapper/gradle-wrapper.properties
gradlew
gradlew.bat
settings.gradle
src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt
src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt
src/main/kotlin/org/openapitools/client/infrastructure/BigDecimalAdapter.kt
src/main/kotlin/org/openapitools/client/infrastructure/BigIntegerAdapter.kt
src/main/kotlin/org/openapitools/client/infrastructure/ByteArrayAdapter.kt
src/main/kotlin/org/openapitools/client/infrastructure/CollectionFormats.kt
src/main/kotlin/org/openapitools/client/infrastructure/LocalDateAdapter.kt
src/main/kotlin/org/openapitools/client/infrastructure/LocalDateTimeAdapter.kt
src/main/kotlin/org/openapitools/client/infrastructure/OffsetDateTimeAdapter.kt
src/main/kotlin/org/openapitools/client/infrastructure/ResponseExt.kt
src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt
src/main/kotlin/org/openapitools/client/infrastructure/URIAdapter.kt
src/main/kotlin/org/openapitools/client/infrastructure/UUIDAdapter.kt
src/main/kotlin/org/openapitools/client/models/Apa.kt

View File

@@ -0,0 +1,50 @@
# org.openapitools.client - Kotlin client library for Demo
## Requires
* Kotlin 1.4.30
* Gradle 6.8.3
## Build
First, create the gradle wrapper script:
```
gradle wrapper
```
Then, run:
```
./gradlew check assemble
```
This runs all tests and packages the library.
## Features/Implementation Notes
* Supports JSON inputs/outputs, File inputs, and Form inputs.
* Supports collection formats for query parameters: csv, tsv, ssv, pipes.
* Some Kotlin and Java types are fully qualified to avoid conflicts with types defined in OpenAPI definitions.
* Implementation of ApiClient is intended to reduce method counts, specifically to benefit Android targets.
<a name="documentation-for-api-endpoints"></a>
## Documentation for API Endpoints
All URIs are relative to *http://localhost*
Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
*DefaultApi* | [**test**](docs/DefaultApi.md#test) | **POST** test | Tests default values
<a name="documentation-for-models"></a>
## Documentation for Models
- [org.openapitools.client.models.Apa](docs/Apa.md)
<a name="documentation-for-authorization"></a>
## Documentation for Authorization
All endpoints do not require authorization.

View File

@@ -0,0 +1,41 @@
group 'org.openapitools'
version '1.0.0'
wrapper {
gradleVersion = '6.8.3'
distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
}
buildscript {
ext.kotlin_version = '1.5.10'
ext.retrofitVersion = '2.9.0'
repositories {
maven { url "https://repo1.maven.org/maven2" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
repositories {
maven { url "https://repo1.maven.org/maven2" }
}
test {
useJUnitPlatform()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
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:logging-interceptor:4.9.1"
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
implementation "com.squareup.retrofit2:converter-moshi:$retrofitVersion"
implementation "com.squareup.retrofit2:converter-scalars:$retrofitVersion"
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2"
}

View File

@@ -0,0 +1,13 @@
# Apa
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**i0** | **kotlin.Int** | |
**n0** | [**java.math.BigDecimal**](java.math.BigDecimal.md) | |
**i1** | **kotlin.Int** | | [optional]
**n1** | [**java.math.BigDecimal**](java.math.BigDecimal.md) | | [optional]

View File

@@ -0,0 +1,101 @@
# DefaultApi
All URIs are relative to *http://localhost*
Method | HTTP request | Description
------------- | ------------- | -------------
[**test**](DefaultApi.md#test) | **POST** test | Tests default values
Tests default values
Tests default values of different parameters
### Example
```kotlin
// Import classes:
//import org.openapitools.client.*
//import org.openapitools.client.infrastructure.*
//import org.openapitools.client.models.*
val apiClient = ApiClient()
val webService = apiClient.createWebservice(DefaultApi::class.java)
val pi0 : kotlin.Int = 56 // kotlin.Int |
val pi1 : kotlin.Int = 56 // kotlin.Int |
val pn0 : java.math.BigDecimal = 8.14 // java.math.BigDecimal |
val pn1 : java.math.BigDecimal = 8.14 // java.math.BigDecimal |
val qi0 : kotlin.Int = 56 // kotlin.Int |
val qi1 : kotlin.Int = 56 // kotlin.Int |
val qi2 : kotlin.Int = 56 // kotlin.Int |
val qi3 : kotlin.Int = 56 // kotlin.Int |
val qn0 : java.math.BigDecimal = 8.14 // java.math.BigDecimal |
val qn1 : java.math.BigDecimal = 8.14 // java.math.BigDecimal |
val qn2 : java.math.BigDecimal = 8.14 // java.math.BigDecimal |
val qn3 : java.math.BigDecimal = 8.14 // java.math.BigDecimal |
val hi0 : kotlin.Int = 56 // kotlin.Int |
val hi1 : kotlin.Int = 56 // kotlin.Int |
val hi2 : kotlin.Int = 56 // kotlin.Int |
val hi3 : kotlin.Int = 56 // kotlin.Int |
val hn0 : java.math.BigDecimal = 8.14 // java.math.BigDecimal |
val hn1 : java.math.BigDecimal = 8.14 // java.math.BigDecimal |
val hn2 : java.math.BigDecimal = 8.14 // java.math.BigDecimal |
val hn3 : java.math.BigDecimal = 8.14 // java.math.BigDecimal |
val fi0 : kotlin.Int = 56 // kotlin.Int |
val fi1 : kotlin.Int = 56 // kotlin.Int |
val fi2 : kotlin.Int = 56 // kotlin.Int |
val fi3 : kotlin.Int = 56 // kotlin.Int |
val fn0 : java.math.BigDecimal = 8.14 // java.math.BigDecimal |
val fn1 : java.math.BigDecimal = 8.14 // java.math.BigDecimal |
val fn2 : java.math.BigDecimal = 8.14 // java.math.BigDecimal |
val fn3 : java.math.BigDecimal = 8.14 // java.math.BigDecimal |
webService.test(pi0, pi1, pn0, pn1, qi0, qi1, qi2, qi3, qn0, qn1, qn2, qn3, hi0, hi1, hi2, hi3, hn0, hn1, hn2, hn3, fi0, fi1, fi2, fi3, fn0, fn1, fn2, fn3)
```
### Parameters
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**pi0** | **kotlin.Int**| | [default to 10]
**pi1** | **kotlin.Int**| |
**pn0** | **java.math.BigDecimal**| | [default to 10.0]
**pn1** | **java.math.BigDecimal**| |
**qi0** | **kotlin.Int**| | [optional] [default to 10]
**qi1** | **kotlin.Int**| | [default to 71]
**qi2** | **kotlin.Int**| | [optional]
**qi3** | **kotlin.Int**| |
**qn0** | **java.math.BigDecimal**| | [optional] [default to 10.0]
**qn1** | **java.math.BigDecimal**| | [default to 71.0]
**qn2** | **java.math.BigDecimal**| | [optional]
**qn3** | **java.math.BigDecimal**| |
**hi0** | **kotlin.Int**| | [optional] [default to 10]
**hi1** | **kotlin.Int**| | [default to 71]
**hi2** | **kotlin.Int**| | [optional]
**hi3** | **kotlin.Int**| |
**hn0** | **java.math.BigDecimal**| | [optional] [default to 10.0]
**hn1** | **java.math.BigDecimal**| | [default to 71.0]
**hn2** | **java.math.BigDecimal**| | [optional]
**hn3** | **java.math.BigDecimal**| |
**fi0** | **kotlin.Int**| | [optional] [default to 10]
**fi1** | **kotlin.Int**| | [default to 71]
**fi2** | **kotlin.Int**| | [optional]
**fi3** | **kotlin.Int**| |
**fn0** | **java.math.BigDecimal**| | [optional] [default to 10.0]
**fn1** | **java.math.BigDecimal**| | [default to 71.0]
**fn2** | **java.math.BigDecimal**| | [optional]
**fn3** | **java.math.BigDecimal**| |
### Return type
null (empty response body)
### Authorization
No authorization required
### HTTP request headers
- **Content-Type**: multipart/form-data
- **Accept**: Not defined

View File

@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@@ -0,0 +1,185 @@
#!/usr/bin/env sh
#
# Copyright 2015 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
##
## Gradle start up script for UN*X
##
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
warn () {
echo "$*"
}
die () {
echo
echo "$*"
echo
exit 1
}
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
NONSTOP* )
nonstop=true
;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi
# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
# For Cygwin or MSYS, switch paths to Windows format before running java
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=`expr $i + 1`
done
case $i in
0) set -- ;;
1) set -- "$args0" ;;
2) set -- "$args0" "$args1" ;;
3) set -- "$args0" "$args1" "$args2" ;;
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi
# Escape application args
save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
APP_ARGS=`save "$@"`
# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
exec "$JAVACMD" "$@"

View File

@@ -0,0 +1,89 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

View File

@@ -0,0 +1,2 @@
rootProject.name = 'kotlin-default-values-jvm-retrofit2'

View File

@@ -0,0 +1,50 @@
package org.openapitools.client.apis
import org.openapitools.client.infrastructure.CollectionFormats.*
import retrofit2.http.*
import retrofit2.Call
import okhttp3.RequestBody
interface DefaultApi {
/**
* Tests default values
* Tests default values of different parameters
* Responses:
* - 200: Success
*
* @param pi0 (default to 10)
* @param pi1
* @param pn0 (default to 10.0)
* @param pn1
* @param qi0 (optional, default to 10)
* @param qi1 (default to 71)
* @param qi2 (optional)
* @param qi3
* @param qn0 (optional, default to 10.0)
* @param qn1 (default to 71.0)
* @param qn2 (optional)
* @param qn3
* @param hi0 (optional, default to 10)
* @param hi1 (default to 71)
* @param hi2 (optional)
* @param hi3
* @param hn0 (optional, default to 10.0)
* @param hn1 (default to 71.0)
* @param hn2 (optional)
* @param hn3
* @param fi0 (optional, default to 10)
* @param fi1 (default to 71)
* @param fi2 (optional)
* @param fi3
* @param fn0 (optional, default to 10.0)
* @param fn1 (default to 71.0)
* @param fn2 (optional)
* @param fn3
* @return [Call]<[Unit]>
*/
@Multipart
@POST("test")
fun test(@Path("pi0") pi0: kotlin.Int = 10, @Path("pi1") pi1: kotlin.Int, @Path("pn0") pn0: java.math.BigDecimal = java.math.BigDecimal("10.0"), @Path("pn1") pn1: java.math.BigDecimal, @Query("qi0") qi0: kotlin.Int? = 10, @Query("qi1") qi1: kotlin.Int = 71, @Query("qi2") qi2: kotlin.Int? = null, @Query("qi3") qi3: kotlin.Int, @Query("qn0") qn0: java.math.BigDecimal? = java.math.BigDecimal("10.0"), @Query("qn1") qn1: java.math.BigDecimal = java.math.BigDecimal("71.0"), @Query("qn2") qn2: java.math.BigDecimal? = null, @Query("qn3") qn3: java.math.BigDecimal, @Header("hi0") hi0: kotlin.Int? = 10, @Header("hi1") hi1: kotlin.Int = 71, @Header("hi2") hi2: kotlin.Int? = null, @Header("hi3") hi3: kotlin.Int, @Header("hn0") hn0: java.math.BigDecimal? = java.math.BigDecimal("10.0"), @Header("hn1") hn1: java.math.BigDecimal = java.math.BigDecimal("71.0"), @Header("hn2") hn2: java.math.BigDecimal? = null, @Header("hn3") hn3: java.math.BigDecimal, @Part("fi0") fi0: kotlin.Int? = 10, @Part("fi1") fi1: kotlin.Int = 71, @Part("fi2") fi2: kotlin.Int? = null, @Part("fi3") fi3: kotlin.Int, @Part("fn0") fn0: java.math.BigDecimal? = java.math.BigDecimal("10.0"), @Part("fn1") fn1: java.math.BigDecimal = java.math.BigDecimal("71.0"), @Part("fn2") fn2: java.math.BigDecimal? = null, @Part("fn3") fn3: java.math.BigDecimal): Call<Unit>
}

View File

@@ -0,0 +1,106 @@
package org.openapitools.client.infrastructure
import okhttp3.Call
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Converter
import retrofit2.converter.scalars.ScalarsConverterFactory
import com.squareup.moshi.Moshi
import retrofit2.converter.moshi.MoshiConverterFactory
class ApiClient(
private var baseUrl: String = defaultBasePath,
private val okHttpClientBuilder: OkHttpClient.Builder? = null,
private val serializerBuilder: Moshi.Builder = Serializer.moshiBuilder,
private val callFactory : Call.Factory? = null,
private val converterFactory: Converter.Factory? = null,
) {
private val apiAuthorizations = mutableMapOf<String, Interceptor>()
var logger: ((String) -> Unit)? = null
private val retrofitBuilder: Retrofit.Builder by lazy {
Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(MoshiConverterFactory.create(serializerBuilder.build()))
.apply {
if (converterFactory != null) {
addConverterFactory(converterFactory)
}
}
}
private val clientBuilder: OkHttpClient.Builder by lazy {
okHttpClientBuilder ?: defaultClientBuilder
}
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
})
}
init {
normalizeBaseUrl()
}
/**
* Adds an authorization to be used by the client
* @param authName Authentication name
* @param authorization Authorization interceptor
* @return ApiClient
*/
fun addAuthorization(authName: String, authorization: Interceptor): ApiClient {
if (apiAuthorizations.containsKey(authName)) {
throw RuntimeException("auth name $authName already in api authorizations")
}
apiAuthorizations[authName] = authorization
clientBuilder.addInterceptor(authorization)
return this
}
fun setLogger(logger: (String) -> Unit): ApiClient {
this.logger = logger
return this
}
fun <S> createService(serviceClass: Class<S>): S {
val usedCallFactory = this.callFactory ?: clientBuilder.build()
return retrofitBuilder.callFactory(usedCallFactory).build().create(serviceClass)
}
private fun normalizeBaseUrl() {
if (!baseUrl.endsWith("/")) {
baseUrl += "/"
}
}
private inline fun <T, reified U> Iterable<T>.runOnFirst(callback: U.() -> Unit) {
for (element in this) {
if (element is U) {
callback.invoke(element)
break
}
}
}
companion object {
@JvmStatic
protected val baseUrlKey = "org.openapitools.client.baseUrl"
@JvmStatic
val defaultBasePath: String by lazy {
System.getProperties().getProperty(baseUrlKey, "http://localhost")
}
}
}

View File

@@ -0,0 +1,17 @@
package org.openapitools.client.infrastructure
import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
import java.math.BigDecimal
class BigDecimalAdapter {
@ToJson
fun toJson(value: BigDecimal): String {
return value.toPlainString()
}
@FromJson
fun fromJson(value: String): BigDecimal {
return BigDecimal(value)
}
}

View File

@@ -0,0 +1,17 @@
package org.openapitools.client.infrastructure
import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
import java.math.BigInteger
class BigIntegerAdapter {
@ToJson
fun toJson(value: BigInteger): String {
return value.toString()
}
@FromJson
fun fromJson(value: String): BigInteger {
return BigInteger(value)
}
}

View File

@@ -0,0 +1,12 @@
package org.openapitools.client.infrastructure
import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
class ByteArrayAdapter {
@ToJson
fun toJson(data: ByteArray): String = String(data)
@FromJson
fun fromJson(data: String): ByteArray = data.toByteArray()
}

View File

@@ -0,0 +1,56 @@
package org.openapitools.client.infrastructure
class CollectionFormats {
open class CSVParams {
var params: List<String>
constructor(params: List<String>) {
this.params = params
}
constructor(vararg params: String) {
this.params = listOf(*params)
}
override fun toString(): String {
return params.joinToString(",")
}
}
open class SSVParams : CSVParams {
constructor(params: List<String>) : super(params)
constructor(vararg params: String) : super(*params)
override fun toString(): String {
return params.joinToString(" ")
}
}
class TSVParams : CSVParams {
constructor(params: List<String>) : super(params)
constructor(vararg params: String) : super(*params)
override fun toString(): String {
return params.joinToString("\t")
}
}
class PIPESParams : CSVParams {
constructor(params: List<String>) : super(params)
constructor(vararg params: String) : super(*params)
override fun toString(): String {
return params.joinToString("|")
}
}
class SPACEParams : SSVParams()
}

View File

@@ -0,0 +1,19 @@
package org.openapitools.client.infrastructure
import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
import java.time.LocalDate
import java.time.format.DateTimeFormatter
class LocalDateAdapter {
@ToJson
fun toJson(value: LocalDate): String {
return DateTimeFormatter.ISO_LOCAL_DATE.format(value)
}
@FromJson
fun fromJson(value: String): LocalDate {
return LocalDate.parse(value, DateTimeFormatter.ISO_LOCAL_DATE)
}
}

View File

@@ -0,0 +1,19 @@
package org.openapitools.client.infrastructure
import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
class LocalDateTimeAdapter {
@ToJson
fun toJson(value: LocalDateTime): String {
return DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(value)
}
@FromJson
fun fromJson(value: String): LocalDateTime {
return LocalDateTime.parse(value, DateTimeFormatter.ISO_LOCAL_DATE_TIME)
}
}

View File

@@ -0,0 +1,19 @@
package org.openapitools.client.infrastructure
import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
class OffsetDateTimeAdapter {
@ToJson
fun toJson(value: OffsetDateTime): String {
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(value)
}
@FromJson
fun fromJson(value: String): OffsetDateTime {
return OffsetDateTime.parse(value, DateTimeFormatter.ISO_OFFSET_DATE_TIME)
}
}

View File

@@ -0,0 +1,16 @@
package org.openapitools.client.infrastructure
import com.squareup.moshi.JsonDataException
import com.squareup.moshi.Moshi
import retrofit2.Response
@Throws(JsonDataException::class)
inline fun <reified T> Response<*>.getErrorResponse(serializerBuilder: Moshi.Builder = Serializer.moshiBuilder): T? {
val serializer = serializerBuilder.build()
val parser = serializer.adapter(T::class.java)
val response = errorBody()?.string()
if (response != null) {
return parser.fromJson(response)
}
return null
}

View File

@@ -0,0 +1,23 @@
package org.openapitools.client.infrastructure
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
object Serializer {
@JvmStatic
val moshiBuilder: Moshi.Builder = Moshi.Builder()
.add(OffsetDateTimeAdapter())
.add(LocalDateTimeAdapter())
.add(LocalDateAdapter())
.add(UUIDAdapter())
.add(ByteArrayAdapter())
.add(URIAdapter())
.add(KotlinJsonAdapterFactory())
.add(BigDecimalAdapter())
.add(BigIntegerAdapter())
@JvmStatic
val moshi: Moshi by lazy {
moshiBuilder.build()
}
}

View File

@@ -0,0 +1,13 @@
package org.openapitools.client.infrastructure
import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
import java.net.URI
class URIAdapter {
@ToJson
fun toJson(uri: URI) = uri.toString()
@FromJson
fun fromJson(s: String): URI = URI.create(s)
}

View File

@@ -0,0 +1,13 @@
package org.openapitools.client.infrastructure
import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
import java.util.UUID
class UUIDAdapter {
@ToJson
fun toJson(uuid: UUID) = uuid.toString()
@FromJson
fun fromJson(s: String): UUID = UUID.fromString(s)
}

View File

@@ -0,0 +1,50 @@
/**
* Demo
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1.0.0
*
*
* Please note:
* This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* Do not edit this file manually.
*/
@file:Suppress(
"ArrayInDataClass",
"EnumEntryName",
"RemoveRedundantQualifierName",
"UnusedImport"
)
package org.openapitools.client.models
import com.squareup.moshi.Json
/**
*
*
* @param i0
* @param n0
* @param i1
* @param n1
*/
data class Apa (
@Json(name = "i0")
val i0: kotlin.Int,
@Json(name = "n0")
val n0: java.math.BigDecimal,
@Json(name = "i1")
val i1: kotlin.Int? = null,
@Json(name = "n1")
val n1: java.math.BigDecimal? = null
)