Skip to content

Commit

Permalink
Kotlin client lists (swagger-api#5729)
Browse files Browse the repository at this point in the history
* [kotlin] array->List instead of Array

Serialization to/from primitive arrays can cause issues with valid
responses. This commit considers swagger 'array' types as 'List' which,
although not as memory efficient should provide a cleaner interface for
users (and avoid serialization issues).

Also, updates README.md to list new generator and excludes
folder at samples/client/kotlin/bin/, which is not used.

* [kotlin] Move sample under conventional directory

samples/client/kotlin/ -> samples/client/petstore/kotlin/

Updated new.sh to generate client/server/docs into similar structure.
Current documentation generators (cwiki, html, html.md, html2) aren't
following a convention like client/server generators.
  • Loading branch information
jimschubert authored and wing328 committed May 30, 2017
1 parent 9282098 commit 23cf641
Show file tree
Hide file tree
Showing 35 changed files with 58 additions and 54 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -850,13 +850,14 @@ Swagger Codegen core team members are contributors who have been making signific
|:-------------|:-------------|
| ActionScript | |
| C++ | |
| C# | @jimschubert (2016/05/01) | |
| C# | @jimschubert (2016/05/01) |
| Clojure | @xhh (2016/05/01) |
| Dart | |
| Groovy | |
| Go | @guohuang (2016/05/01) @neilotoole (2016/05/01) |
| Java | @cbornet (2016/05/01) @xhh (2016/05/01) @epaul (2016/06/04) |
| Java (Spring Cloud) | @cbornet (2016/07/19) |
| Kotlin | @jimschubert (2016/05/01) |
| NodeJS/Javascript | @xhh (2016/05/01) |
| ObjC | @mateuszmackowiak (2016/05/09) |
| Perl | @wing328 (2016/05/01) |
Expand Down Expand Up @@ -916,6 +917,7 @@ Here is a list of template creators:
* Javascript/NodeJS: @jfiala
* Javascript (Closure-annotated Angular) @achew22
* JMeter @davidkiss
* Kotlin @jimschubert
* Perl: @wing328
* PHP (Guzzle): @baartosz
* Swift: @tkqubo
Expand Down
2 changes: 1 addition & 1 deletion bin/kotlin-client-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ generate -t modules/swagger-codegen/src/main/resources/kotlin-client -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l kotlin -o samples/client/kotlin"
ags="$@ generate -t modules/swagger-codegen/src/main/resources/kotlin-client -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l kotlin -o samples/client/petstore/kotlin"

java ${JAVA_OPTS} -jar ${executable} ${ags}
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ public KotlinClientCodegen() {
typeMapping.put("date-time", "java.time.LocalDateTime");
typeMapping.put("date", "java.time.LocalDateTime");
typeMapping.put("file", "java.io.File");
typeMapping.put("array", "kotlin.Array");
typeMapping.put("array", "kotlin.collections.List");
typeMapping.put("list", "kotlin.collections.List");
typeMapping.put("map", "kotlin.collections.Map");
typeMapping.put("object", "kotlin.Any");
typeMapping.put("binary", "kotlin.Array<kotlin.Byte>");
typeMapping.put("Date", "java.time.LocalDateTime");
typeMapping.put("DateTime", "java.time.LocalDateTime");

instantiationTypes.put("array", "arrayOf");
instantiationTypes.put("array", "listOf");
instantiationTypes.put("list", "listOf");
instantiationTypes.put("map", "mapOf");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ public void arrayPropertyTest() {
Assert.assertEquals(property.baseName, "examples");
Assert.assertEquals(property.getter, "getExamples");
Assert.assertEquals(property.setter, "setExamples");
Assert.assertEquals(property.datatype, "kotlin.Array<kotlin.String>");
Assert.assertEquals(property.datatype, "kotlin.collections.List<kotlin.String>");
Assert.assertEquals(property.name, "examples");
Assert.assertEquals(property.defaultValue, "null");
Assert.assertEquals(property.baseType, "kotlin.Array");
Assert.assertEquals(property.baseType, "kotlin.collections.List");
Assert.assertEquals(property.containerType, "array");
Assert.assertFalse(property.required);
Assert.assertTrue(property.isContainer);
Expand Down
2 changes: 1 addition & 1 deletion new.sh
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ fi
# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="\${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="\$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l ${gen_name} -o samples/${gen_type}/${gen_name}"
ags="\$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l ${gen_name} -o samples/${gen_type}/petstore/${gen_name}"
java \${JAVA_OPTS} -jar \${executable} \${ags}
EOF
Expand Down
32 changes: 0 additions & 32 deletions samples/client/kotlin/build.gradle

This file was deleted.

1 change: 0 additions & 1 deletion samples/client/kotlin/settings.gradle

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

./bin/
# Created by https://www.gitignore.io/api/java,intellij,gradle

### Intellij ###
Expand Down
File renamed without changes.
File renamed without changes.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Mon May 29 10:58:54 EDT 2017
#Mon May 29 15:39:04 EDT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCli
* Finds Pets by status
* Multiple status values can be provided with comma separated strings
* @param status Status values that need to be considered for filter
* @return kotlin.Array<Pet>
* @return kotlin.collections.List<Pet>
*/
@Suppress("UNCHECKED_CAST")
fun findPetsByStatus(status: kotlin.Array<kotlin.String>) : kotlin.Array<Pet> {
fun findPetsByStatus(status: kotlin.collections.List<kotlin.String>) : kotlin.collections.List<Pet> {
val localVariableBody: kotlin.Any? = null
val localVariableQuery: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("status" to status.joinToString(separator = collectionDelimiter("csv")))
val localVariableHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf()
Expand All @@ -98,13 +98,13 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCli
query = localVariableQuery,
headers = localVariableHeaders
)
val response = request<kotlin.Array<Pet>>(
val response = request<kotlin.collections.List<Pet>>(
localVariableConfig,
localVariableBody
)

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as kotlin.Array<Pet>
ResponseType.Success -> (response as Success<*>).data as kotlin.collections.List<Pet>
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
Expand All @@ -117,10 +117,10 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCli
* Finds Pets by tags
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
* @param tags Tags to filter by
* @return kotlin.Array<Pet>
* @return kotlin.collections.List<Pet>
*/
@Suppress("UNCHECKED_CAST")
fun findPetsByTags(tags: kotlin.Array<kotlin.String>) : kotlin.Array<Pet> {
fun findPetsByTags(tags: kotlin.collections.List<kotlin.String>) : kotlin.collections.List<Pet> {
val localVariableBody: kotlin.Any? = null
val localVariableQuery: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("tags" to tags.joinToString(separator = collectionDelimiter("csv")))
val localVariableHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf()
Expand All @@ -130,13 +130,13 @@ class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCli
query = localVariableQuery,
headers = localVariableHeaders
)
val response = request<kotlin.Array<Pet>>(
val response = request<kotlin.collections.List<Pet>>(
localVariableConfig,
localVariableBody
)

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as kotlin.Array<Pet>
ResponseType.Success -> (response as Success<*>).data as kotlin.collections.List<Pet>
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class UserApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCl
* @param body List of user object
* @return void
*/
fun createUsersWithArrayInput(body: kotlin.Array<User>) : Unit {
fun createUsersWithArrayInput(body: kotlin.collections.List<User>) : Unit {
val localVariableBody: kotlin.Any? = body
val localVariableQuery: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf()
val localVariableHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf()
Expand Down Expand Up @@ -85,7 +85,7 @@ class UserApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiCl
* @param body List of user object
* @return void
*/
fun createUsersWithListInput(body: kotlin.Array<User>) : Unit {
fun createUsersWithListInput(body: kotlin.collections.List<User>) : Unit {
val localVariableBody: kotlin.Any? = body
val localVariableQuery: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf()
val localVariableHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ data class Pet (
val id: kotlin.Long?,
val category: Category?,
val name: kotlin.String,
val photoUrls: kotlin.Array<kotlin.String>,
val tags: kotlin.Array<Tag>?,
val photoUrls: kotlin.collections.List<kotlin.String>,
val tags: kotlin.collections.List<Tag>?,
/* pet status in the store */
val status: kotlin.String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.swagger.client.functional

import io.kotlintest.matchers.should
import io.kotlintest.matchers.beGreaterThan
import io.kotlintest.specs.ShouldSpec
import io.swagger.client.apis.PetApi

class EvaluateTest : ShouldSpec() {
init {
should("query against pet statuses") {
val api = PetApi()
val results = api.findPetsByStatus(listOf("available", "pending"))

results.size should beGreaterThan(1)
}

// TODO: Handle default (200) response
/*
should("post data (new pet)") {
val api = PetApi()
val pet = Pet(
id = 0,
name = "kotlin client test",
category = Category(0, "string"),
tags = listOf(Tag(0, "string"))
)
val result = api.addPet(pet)
result.name shouldBe(pet.name)
result.category shouldBe(pet.category)
result.tags shouldBe(pet.tags)
}
*/
}
}

0 comments on commit 23cf641

Please sign in to comment.