Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions bin/kotlin-client-all.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/sh

./bin/kotlin-client-nonpublic.sh
./bin/kotlin-client-okhttp3.sh
./bin/kotlin-client-petstore-multiplatform.sh
./bin/kotlin-client-petstore.sh
Expand Down
34 changes: 34 additions & 0 deletions bin/kotlin-client-nonpublic.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/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 -B clean package
fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="generate -t modules/openapi-generator/src/main/resources/kotlin-client -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g kotlin --artifact-id kotlin-petstore-nonpublic --additional-properties nonPublicApi=true -o samples/client/petstore/kotlin-nonpublic $@"

java ${JAVA_OPTS} -jar ${executable} ${ags}

cp CI/samples.ci/client/petstore/kotlin-string/pom.xml samples/client/petstore/kotlin-string/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public enum SERIALIZATION_LIBRARY_TYPE {moshi, gson}
protected boolean needsDataClassBody = false;
protected boolean hasEnums = false;

protected boolean nonPublicApi = false;

protected CodegenConstants.ENUM_PROPERTY_NAMING_TYPE enumPropertyNaming = CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.camelCase;
protected SERIALIZATION_LIBRARY_TYPE serializationLibrary = SERIALIZATION_LIBRARY_TYPE.moshi;

Expand Down Expand Up @@ -430,6 +432,12 @@ public void processOpts() {
additionalProperties.put(CodegenConstants.PARCELIZE_MODELS, parcelizeModels);
}

if (additionalProperties.containsKey(CodegenConstants.NON_PUBLIC_API)) {
this.setNonPublicApi(Boolean.valueOf((String) additionalProperties.get(CodegenConstants.NON_PUBLIC_API)));
} else {
additionalProperties.put(CodegenConstants.NON_PUBLIC_API, nonPublicApi);
}

additionalProperties.put(CodegenConstants.NEEDS_DATACLASS_BODY, this.hasEnums || serializableModel);
additionalProperties.put(CodegenConstants.API_PACKAGE, apiPackage());
additionalProperties.put(CodegenConstants.MODEL_PACKAGE, modelPackage());
Expand Down Expand Up @@ -481,6 +489,14 @@ public boolean isSerializableModel() {
public void setSerializableModel(boolean serializableModel) {
this.serializableModel = serializableModel;
}

public boolean nonPublicApi() {
return nonPublicApi;
}

public void setNonPublicApi(boolean nonPublicApi) {
this.nonPublicApi = nonPublicApi;
}

public boolean isNeedsDataClassBody() {
return needsDataClassBody;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {{packageName}}.infrastructure.Success
import {{packageName}}.infrastructure.toMultiValue

{{#operations}}
class {{classname}}(basePath: kotlin.String = "{{{basePath}}}") : ApiClient(basePath) {
{{>visibility}}class {{classname}}(basePath: kotlin.String = "{{{basePath}}}") : ApiClient(basePath) {

{{#operation}}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import java.io.Serializable
@Parcelize
{{/parcelizeModels}}
{{#multiplatform}}@Serializable{{/multiplatform}}
data class {{classname}} (
{{>visibility}}data class {{classname}} (
{{#requiredVars}}
{{>data_class_req_var}}{{^-last}},
{{/-last}}{{/requiredVars}}{{#hasRequired}}{{#hasOptional}},
Expand All @@ -49,7 +49,7 @@ data class {{classname}} (
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
*/
{{#multiplatform}}@Serializable(with = {{nameInCamelCase}}.Serializer::class){{/multiplatform}}
enum class {{{nameInCamelCase}}}(val value: {{#isListContainer}}{{{ nestedType }}}{{/isListContainer}}{{^isListContainer}}{{{dataType}}}{{/isListContainer}}){
{{>visibility}}enum class {{{nameInCamelCase}}}(val value: {{#isListContainer}}{{{ nestedType }}}{{/isListContainer}}{{^isListContainer}}{{{dataType}}}{{/isListContainer}}){
{{#allowableValues}}{{#enumVars}}
{{#jvm}}
{{#moshi}}
Expand All @@ -65,7 +65,7 @@ data class {{classname}} (
{{/enumVars}}{{/allowableValues}}

{{#multiplatform}}
object Serializer : CommonEnumSerializer<{{nameInCamelCase}}>("{{nameInCamelCase}}", values(), values().map { it.value }.toTypedArray())
{{>visibility}}object Serializer : CommonEnumSerializer<{{nameInCamelCase}}>("{{nameInCamelCase}}", values(), values().map { it.value }.toTypedArray())
{{/multiplatform}}
}
{{/isEnum}}{{/vars}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import kotlinx.serialization.internal.CommonEnumSerializer
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
*/
{{#multiplatform}}@Serializable(with = {{classname}}.Serializer::class){{/multiplatform}}
enum class {{classname}}(val value: {{{dataType}}}){
{{>visibility}}enum class {{classname}}(val value: {{{dataType}}}){

{{#allowableValues}}{{#enumVars}}
{{#jvm}}
Expand All @@ -42,6 +42,6 @@ enum class {{classname}}(val value: {{{dataType}}}){
{{/enumVars}}{{/allowableValues}}

{{#multiplatform}}
object Serializer : CommonEnumSerializer<{{classname}}>("{{classname}}", values(), values().map { it.value }.toTypedArray())
{{>visibility}}object Serializer : CommonEnumSerializer<{{classname}}>("{{classname}}", values(), values().map { it.value }.toTypedArray())
{{/multiplatform}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package {{packageName}}.infrastructure

typealias MultiValueMap = Map<String,List<String>>

fun collectionDelimiter(collectionFormat: String) = when(collectionFormat) {
{{>visibility}}fun collectionDelimiter(collectionFormat: String) = when(collectionFormat) {
"csv" -> ","
"tsv" -> "\t"
"pipes" -> "|"
"ssv" -> " "
else -> ""
}

val defaultMultiValueConverter: (item: Any?) -> String = { item -> "$item" }
{{>visibility}}val defaultMultiValueConverter: (item: Any?) -> String = { item -> "$item" }

fun <T : Any?> toMultiValue(items: Array<T>, collectionFormat: String, map: (item: T) -> String = defaultMultiValueConverter)
{{>visibility}}fun <T : Any?> toMultiValue(items: Array<T>, collectionFormat: String, map: (item: T) -> String = defaultMultiValueConverter)
= toMultiValue(items.asIterable(), collectionFormat, map)

fun <T : Any?> toMultiValue(items: Iterable<T>, collectionFormat: String, map: (item: T) -> String = defaultMultiValueConverter): List<String> {
{{>visibility}}fun <T : Any?> toMultiValue(items: Iterable<T>, collectionFormat: String, map: (item: T) -> String = defaultMultiValueConverter): List<String> {
return when(collectionFormat) {
"multi" -> items.map(map)
else -> listOf(items.joinToString(separator = collectionDelimiter(collectionFormat), transform = map))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package {{packageName}}.infrastructure
* NOTE: Headers is a Map<String,String> because rfc2616 defines
* multi-valued headers as csv-only.
*/
data class RequestConfig(
{{>visibility}}data class RequestConfig(
val method: RequestMethod,
val path: String,
val headers: MutableMap<String, String> = mutableMapOf(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package {{packageName}}.infrastructure
/**
* Provides enumerated HTTP verbs
*/
enum class RequestMethod {
{{>visibility}}enum class RequestMethod {
GET, DELETE, HEAD, OPTIONS, PATCH, POST, PUT
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.Request
import java.io.File

open class ApiClient(val baseUrl: String) {
companion object {
{{>visibility}}open class ApiClient(val baseUrl: String) {
{{>visibility}}companion object {
protected const val ContentType = "Content-Type"
protected const val Accept = "Accept"
protected const val Authorization = "Authorization"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
package {{packageName}}.infrastructure

enum class ResponseType {
{{>visibility}}enum class ResponseType {
Success, Informational, Redirection, ClientError, ServerError
}

abstract class ApiInfrastructureResponse<T>(val responseType: ResponseType) {
{{>visibility}}abstract class ApiInfrastructureResponse<T>(val responseType: ResponseType) {
abstract val statusCode: Int
abstract val headers: Map<String,List<String>>
}

class Success<T>(
{{>visibility}}class Success<T>(
val data: T,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
): ApiInfrastructureResponse<T>(ResponseType.Success)

class Informational<T>(
{{>visibility}}class Informational<T>(
val statusText: String,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
) : ApiInfrastructureResponse<T>(ResponseType.Informational)

class Redirection<T>(
{{>visibility}}class Redirection<T>(
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
) : ApiInfrastructureResponse<T>(ResponseType.Redirection)

class ClientError<T>(
{{>visibility}}class ClientError<T>(
val body: Any? = null,
override val statusCode: Int = -1,
override val headers: Map<String, List<String>> = mapOf()
) : ApiInfrastructureResponse<T>(ResponseType.ClientError)

class ServerError<T>(
{{>visibility}}class ServerError<T>(
val message: String? = null,
val body: Any? = null,
override val statusCode: Int = -1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package {{packageName}}.infrastructure
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

object ApplicationDelegates {
{{>visibility}}object ApplicationDelegates {
/**
* Provides a property delegate, allowing the property to be set once and only once.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package {{packageName}}.infrastructure
import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson

class ByteArrayAdapter {
{{>visibility}}class ByteArrayAdapter {
@ToJson
fun toJson(data: ByteArray): String = String(data)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package {{packageName}}.infrastructure

import java.lang.RuntimeException

open class ClientException : RuntimeException {
{{>visibility}}open class ClientException : RuntimeException {

/**
* Constructs an [ClientException] with no detail message.
Expand All @@ -22,7 +22,7 @@ open class ClientException : RuntimeException {
}
}

open class ServerException : RuntimeException {
{{>visibility}}open class ServerException : RuntimeException {

/**
* Constructs an [ServerException] with no detail message.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.threeten.bp.LocalDate
import org.threeten.bp.format.DateTimeFormatter
{{/threetenbp}}

class LocalDateAdapter {
{{>visibility}}class LocalDateAdapter {
@ToJson
fun toJson(value: LocalDate): String {
return DateTimeFormatter.ISO_LOCAL_DATE.format(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.threeten.bp.LocalDateTime
import org.threeten.bp.format.DateTimeFormatter
{{/threetenbp}}

class LocalDateTimeAdapter {
{{>visibility}}class LocalDateTimeAdapter {
@ToJson
fun toJson(value: LocalDateTime): String {
return DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import okhttp3.Response
/**
* Provides an extension to evaluation whether the response is a 1xx code
*/
val Response.isInformational : Boolean get() = this.code{{#jvm-okhttp3}}(){{/jvm-okhttp3}} in 100..199
{{>visibility}}val Response.isInformational : Boolean get() = this.code{{#jvm-okhttp3}}(){{/jvm-okhttp3}} in 100..199

/**
* Provides an extension to evaluation whether the response is a 3xx code
*/
val Response.isRedirect : Boolean get() = this.code{{#jvm-okhttp3}}(){{/jvm-okhttp3}} in 300..399
{{>visibility}}val Response.isRedirect : Boolean get() = this.code{{#jvm-okhttp3}}(){{/jvm-okhttp3}} in 300..399

/**
* Provides an extension to evaluation whether the response is a 4xx code
*/
val Response.isClientError : Boolean get() = this.code{{#jvm-okhttp3}}(){{/jvm-okhttp3}} in 400..499
{{>visibility}}val Response.isClientError : Boolean get() = this.code{{#jvm-okhttp3}}(){{/jvm-okhttp3}} in 400..499

/**
* Provides an extension to evaluation whether the response is a 5xx (Standard) through 999 (non-standard) code
*/
val Response.isServerError : Boolean get() = this.code{{#jvm-okhttp3}}(){{/jvm-okhttp3}} in 500..999
{{>visibility}}val Response.isServerError : Boolean get() = this.code{{#jvm-okhttp3}}(){{/jvm-okhttp3}} in 500..999
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import java.util.Date

object Serializer {
{{>visibility}}object Serializer {
@JvmStatic
val moshi: Moshi = Moshi.Builder()
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
import java.util.UUID

class UUIDAdapter {
{{>visibility}}class UUIDAdapter {
@ToJson
fun toJson(uuid: UUID) = uuid.toString()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import kotlinx.serialization.*
import kotlinx.serialization.internal.StringDescriptor

{{#operations}}
class {{classname}} @UseExperimental(UnstableDefault::class) constructor(
{{>visibility}}class {{classname}} @UseExperimental(UnstableDefault::class) constructor(
baseUrl: kotlin.String = "{{{basePath}}}",
httpClientEngine: HttpClientEngine? = null,
serializer: KotlinxSerializer)
Expand Down Expand Up @@ -101,7 +101,7 @@ class {{classname}} @UseExperimental(UnstableDefault::class) constructor(

{{/operation}}

companion object {
{{>visibility}}companion object {
internal fun setMappers(serializer: KotlinxSerializer) {
{{#operation}}
{{#hasBodyParam}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import kotlinx.serialization.json.JsonConfiguration
import {{apiPackage}}.*
import {{modelPackage}}.*

open class ApiClient(
{{>visibility}}open class ApiClient(
private val baseUrl: String,
httpClientEngine: HttpClientEngine?,
serializer: KotlinxSerializer) {
Expand All @@ -46,7 +46,7 @@ open class ApiClient(
httpClientEngine?.let { HttpClient(it, clientConfig) } ?: HttpClient(clientConfig)
}

companion object {
{{>visibility}}companion object {
protected val UNSAFE_HEADERS = listOf(HttpHeaders.ContentType)

private fun setMappers(serializer: KotlinxSerializer) {
Expand Down
Loading