Skip to content

Commit 2d60b05

Browse files
authored
Merge branch 'master' into nodejs_generator_update
2 parents 82b624b + 52518eb commit 2d60b05

17 files changed

+154
-82
lines changed

src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,6 +2339,23 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
23392339
LOGGER.info("working on Parameter " + parameter.getName());
23402340
}
23412341

2342+
// move the defaultValue for headers, forms and params
2343+
if (parameter instanceof QueryParameter) {
2344+
QueryParameter qp = (QueryParameter) parameter;
2345+
if (qp.getSchema() != null) {
2346+
if (qp.getSchema().getDefault() != null) {
2347+
codegenParameter.defaultValue = qp.getSchema().getDefault().toString();
2348+
}
2349+
}
2350+
} else if (parameter instanceof HeaderParameter) {
2351+
HeaderParameter hp = (HeaderParameter) parameter;
2352+
if (hp.getSchema() != null) {
2353+
if (hp.getSchema().getDefault() != null) {
2354+
codegenParameter.defaultValue = hp.getSchema().getDefault().toString();
2355+
}
2356+
}
2357+
}
2358+
23422359
if (parameter.getExtensions() != null && !parameter.getExtensions().isEmpty()) {
23432360
codegenParameter.vendorExtensions.putAll(parameter.getExtensions());
23442361
}

src/main/java/io/swagger/codegen/v3/generators/kotlin/AbstractKotlinCodegen.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,14 @@ public AbstractKotlinCodegen() {
155155
typeMapping.put("double", "kotlin.Double");
156156
typeMapping.put("number", "java.math.BigDecimal");
157157
typeMapping.put("date-time", "java.time.LocalDateTime");
158-
typeMapping.put("date", "java.time.LocalDateTime");
158+
typeMapping.put("date", "java.time.LocalDate");
159159
typeMapping.put("file", "java.io.File");
160160
typeMapping.put("array", "kotlin.Array");
161161
typeMapping.put("list", "kotlin.Array");
162162
typeMapping.put("map", "kotlin.collections.Map");
163163
typeMapping.put("object", "kotlin.Any");
164164
typeMapping.put("binary", "kotlin.Array<kotlin.Byte>");
165-
typeMapping.put("Date", "java.time.LocalDateTime");
165+
typeMapping.put("Date", "java.time.LocalDate");
166166
typeMapping.put("DateTime", "java.time.LocalDateTime");
167167

168168
instantiationTypes.put("array", "arrayOf");
@@ -498,11 +498,13 @@ public String toModelName(final String name) {
498498
String modifiedName = name.replaceAll("\\.", "");
499499
modifiedName = sanitizeKotlinSpecificNames(modifiedName);
500500

501-
if (isReservedWord(modifiedName)) {
502-
modifiedName = escapeReservedWord(modifiedName);
501+
modifiedName = titleCase(modifiedName);
502+
503+
if (modifiedName.equalsIgnoreCase("Companion")) {
504+
modifiedName = "_" + modifiedName;
503505
}
504506

505-
return titleCase(modifiedName);
507+
return modifiedName;
506508
}
507509

508510
@Override

src/main/java/io/swagger/codegen/v3/generators/kotlin/KotlinClientCodegen.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,8 @@ public void processOpts() {
129129
supportingFiles.add(new SupportingFile("infrastructure/ResponseExtensions.kt.mustache", infrastructureFolder, "ResponseExtensions.kt"));
130130
supportingFiles.add(new SupportingFile("infrastructure/Serializer.kt.mustache", infrastructureFolder, "Serializer.kt"));
131131
supportingFiles.add(new SupportingFile("infrastructure/Errors.kt.mustache", infrastructureFolder, "Errors.kt"));
132+
supportingFiles.add(new SupportingFile("infrastructure/LocalDateAdapter.kt.mustache", infrastructureFolder, "LocalDateAdapter.kt"));
133+
supportingFiles.add(new SupportingFile("infrastructure/LocalDateTimeAdapter.kt.mustache", infrastructureFolder, "LocalDateTimeAdapter.kt"));
134+
132135
}
133136
}

src/main/resources/handlebars/kotlin-client/api.mustache

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ class {{classname}}(basePath: kotlin.String = "{{{basePath}}}") : ApiClient(base
4343
ResponseType.Redirection -> TODO()
4444
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
4545
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
46-
else -> throw kotlin.IllegalStateException("Undefined ResponseType.")
4746
}
4847
}
49-
5048
{{/contents}}
5149
{{/operation}}
5250
}

src/main/resources/handlebars/kotlin-client/build.gradle.mustache

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ task wrapper(type: Wrapper) {
77
}
88

99
buildscript {
10-
ext.kotlin_version = '1.1.2'
10+
ext.kotlin_version = '1.3.50'
1111
1212
repositories {
1313
mavenCentral()
@@ -25,9 +25,9 @@ repositories {
2525

2626
dependencies {
2727
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
28-
compile "com.squareup.moshi:moshi-kotlin:1.5.0"
29-
compile "com.squareup.moshi:moshi-adapters:1.5.0"
30-
compile "com.squareup.okhttp3:okhttp:3.8.0"
31-
compile "org.threeten:threetenbp:1.3.6"
32-
testCompile "io.kotlintest:kotlintest:2.0.2"
33-
}
28+
compile "com.squareup.moshi:moshi-kotlin:1.8.0"
29+
compile "com.squareup.moshi:moshi-adapters:1.8.0"
30+
compile "com.squareup.okhttp3:okhttp:3.14.2"
31+
compile "org.threeten:threetenbp:1.4.0"
32+
testCompile "io.kotlintest:kotlintest:2.0.7"
33+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
2-
* {{{description}}}
3-
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^@last}},{{/@last}}{{/enumVars}}{{/allowableValues}}
4-
*/
2+
* {{{description}}}
3+
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^@last}},{{/@last}}{{/enumVars}}{{/allowableValues}}
4+
*/
55
enum class {{classname}}(val value: {{dataType}}){
66
{{#allowableValues}}{{#enumVars}}
77
{{&name}}({{{value}}}){{^@last}},{{/@last}}{{#@last}};{{/@last}}
88
{{/enumVars}}{{/allowableValues}}
9-
}
9+
}

src/main/resources/handlebars/kotlin-client/infrastructure/ApiAbstractions.kt.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package {{packageName}}.infrastructure
22

3-
typealias MultiValueMap = Map<String,List<String>>
3+
typealias MultiValueMap = Map<String, List<String>>
44

5-
fun collectionDelimiter(collectionFormat: String) = when(collectionFormat) {
5+
fun collectionDelimiter(collectionFormat: String) = when (collectionFormat) {
66
"csv" -> ","
77
"tsv" -> "\t"
88
"pipes" -> "|"
@@ -12,8 +12,8 @@ fun collectionDelimiter(collectionFormat: String) = when(collectionFormat) {
1212

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

15-
fun <T: Any?> toMultiValue(items: List<T>, collectionFormat: String, map: (item: Any?) -> String = defaultMultiValueConverter): List<String> {
16-
return when(collectionFormat) {
15+
fun <T : Any?> toMultiValue(items: List<T>, collectionFormat: String, map: (item: Any?) -> String = defaultMultiValueConverter): List<String> {
16+
return when (collectionFormat) {
1717
"multi" -> items.map(map)
1818
else -> listOf(items.map(map).joinToString(separator = collectionDelimiter(collectionFormat)))
1919
}

src/main/resources/handlebars/kotlin-client/infrastructure/ApiClient.kt.mustache

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import java.io.File
55

66
open class ApiClient(val baseUrl: String) {
77
companion object {
8-
protected val ContentType = "Content-Type"
9-
protected val Accept = "Accept"
10-
protected val JsonMediaType = "application/json"
11-
protected val FormDataMediaType = "multipart/form-data"
12-
protected val XmlMediaType = "application/xml"
8+
protected const val ContentType = "Content-Type"
9+
protected const val Accept = "Accept"
10+
protected const val JsonMediaType = "application/json"
11+
protected const val FormDataMediaType = "multipart/form-data"
12+
protected const val XmlMediaType = "application/xml"
1313
1414
@JvmStatic
15-
val client : OkHttpClient = OkHttpClient()
15+
val client: OkHttpClient = OkHttpClient()
1616
1717
@JvmStatic
1818
var defaultHeaders: Map<String, String> by ApplicationDelegates.setOnce(mapOf(ContentType to JsonMediaType, Accept to JsonMediaType))
@@ -21,40 +21,37 @@ open class ApiClient(val baseUrl: String) {
2121
val jsonHeaders: Map<String, String> = mapOf(ContentType to JsonMediaType, Accept to JsonMediaType)
2222
}
2323

24-
inline protected fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody {
25-
if(content is File) {
26-
return RequestBody.create(
27-
MediaType.parse(mediaType), content
28-
)
29-
} else if(mediaType == FormDataMediaType) {
30-
var builder = FormBody.Builder()
31-
// content's type *must* be Map<String, Any>
32-
@Suppress("UNCHECKED_CAST")
33-
(content as Map<String,String>).forEach { key, value ->
34-
builder = builder.add(key, value)
24+
protected inline fun <reified T> requestBody(content: T, mediaType: String = JsonMediaType): RequestBody =
25+
when {
26+
content is File -> RequestBody.create(MediaType.parse(mediaType), content)
27+
28+
mediaType == FormDataMediaType -> {
29+
var builder = FormBody.Builder()
30+
// content's type *must* be Map<String, Any>
31+
@Suppress("UNCHECKED_CAST")
32+
(content as Map<String, String>).forEach { key, value ->
33+
builder = builder.add(key, value)
34+
}
35+
builder.build()
36+
}
37+
mediaType == JsonMediaType -> RequestBody.create(
38+
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
39+
)
40+
mediaType == XmlMediaType -> TODO("xml not currently supported.")
41+
42+
// TODO: this should be extended with other serializers
43+
else -> TODO("requestBody currently only supports JSON body and File body.")
3544
}
36-
return builder.build()
37-
} else if(mediaType == JsonMediaType) {
38-
return RequestBody.create(
39-
MediaType.parse(mediaType), Serializer.moshi.adapter(T::class.java).toJson(content)
40-
)
41-
} else if (mediaType == XmlMediaType) {
42-
TODO("xml not currently supported.")
43-
}
44-
45-
// TODO: this should be extended with other serializers
46-
TODO("requestBody currently only supports JSON body and File body.")
47-
}
4845
49-
inline protected fun <reified T: Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
50-
if(body == null) return null
51-
return when(mediaType) {
46+
protected inline fun <reified T : Any?> responseBody(body: ResponseBody?, mediaType: String = JsonMediaType): T? {
47+
if (body == null) return null
48+
return when (mediaType) {
5249
JsonMediaType -> Serializer.moshi.adapter(T::class.java).fromJson(body.source())
5350
else -> TODO()
5451
}
5552
}
5653
57-
inline protected fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> {
54+
protected inline fun <reified T : Any?> request(requestConfig: RequestConfig, body: Any? = null): ApiInfrastructureResponse<T?> {
5855
val httpUrl = HttpUrl.parse(baseUrl) ?: throw IllegalStateException("baseUrl is invalid.")
5956
6057
var urlBuilder = httpUrl.newBuilder()
@@ -69,19 +66,19 @@ open class ApiClient(val baseUrl: String) {
6966
val url = urlBuilder.build()
7067
val headers = requestConfig.headers + defaultHeaders
7168
72-
if(headers[ContentType] ?: "" == "") {
69+
if (headers[ContentType] ?: "" == "") {
7370
throw kotlin.IllegalStateException("Missing Content-Type header. This is required.")
7471
}
7572
76-
if(headers[Accept] ?: "" == "") {
73+
if (headers[Accept] ?: "" == "") {
7774
throw kotlin.IllegalStateException("Missing Accept header. This is required.")
7875
}
7976
8077
// TODO: support multiple contentType,accept options here.
8178
val contentType = (headers[ContentType] as String).substringBefore(";").toLowerCase()
8279
val accept = (headers[Accept] as String).substringBefore(";").toLowerCase()
8380
84-
var request : Request.Builder = when (requestConfig.method) {
81+
var request: Request.Builder = when (requestConfig.method) {
8582
RequestMethod.DELETE -> Request.Builder().url(url).delete()
8683
RequestMethod.GET -> Request.Builder().url(url)
8784
RequestMethod.HEAD -> Request.Builder().url(url).head()

src/main/resources/handlebars/kotlin-client/infrastructure/ApiInfrastructureResponse.kt.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ enum class ResponseType {
66

77
abstract class ApiInfrastructureResponse<T>(val responseType: ResponseType) {
88
abstract val statusCode: Int
9-
abstract val headers: Map<String,List<String>>
9+
abstract val headers: Map<String, List<String>>
1010
}
1111

1212
class Success<T>(
1313
val data: T,
1414
override val statusCode: Int = -1,
1515
override val headers: Map<String, List<String>> = mapOf()
16-
): ApiInfrastructureResponse<T>(ResponseType.Success)
16+
) : ApiInfrastructureResponse<T>(ResponseType.Success)
1717

1818
class Informational<T>(
1919
val statusText: String,
@@ -37,4 +37,4 @@ class ServerError<T>(
3737
val body: Any? = null,
3838
override val statusCode: Int = -1,
3939
override val headers: Map<String, List<String>>
40-
): ApiInfrastructureResponse<T>(ResponseType.ServerError)
40+
) : ApiInfrastructureResponse<T>(ResponseType.ServerError)

src/main/resources/handlebars/kotlin-client/infrastructure/ApplicationDelegates.kt.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object ApplicationDelegates {
99
*
1010
* If unset (no default value), a get on the property will throw [IllegalStateException].
1111
*/
12-
fun <T> setOnce(defaultValue: T? = null) : ReadWriteProperty<Any?, T> = SetOnce(defaultValue)
12+
fun <T> setOnce(defaultValue: T? = null): ReadWriteProperty<Any?, T> = SetOnce(defaultValue)
1313
1414
private class SetOnce<T>(defaultValue: T? = null) : ReadWriteProperty<Any?, T> {
1515
private var isSet = false

0 commit comments

Comments
 (0)