diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java index 0a26de4049d..4691ac0ee0d 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java @@ -166,14 +166,14 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co typeMapping.put("ByteArray", "kotlin.ByteArray"); typeMapping.put("number", "java.math.BigDecimal"); typeMapping.put("date-time", "java.time.LocalDateTime"); - typeMapping.put("date", "java.time.LocalDateTime"); + typeMapping.put("date", "java.time.LocalDate"); typeMapping.put("file", "java.io.File"); typeMapping.put("array", "kotlin.Array"); typeMapping.put("list", "kotlin.collections.List"); typeMapping.put("map", "kotlin.collections.Map"); typeMapping.put("object", "kotlin.Any"); typeMapping.put("binary", "kotlin.Array"); - typeMapping.put("Date", "java.time.LocalDateTime"); + typeMapping.put("Date", "java.time.LocalDate"); typeMapping.put("DateTime", "java.time.LocalDateTime"); instantiationTypes.put("array", "kotlin.arrayOf"); diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java index 7b18594f2e5..96a95ade50b 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java @@ -131,6 +131,7 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen { typeMapping.put("DateTime", "LocalDateTime"); importMapping.put("LocalDate", "org.threeten.bp.LocalDate"); importMapping.put("LocalDateTime", "org.threeten.bp.LocalDateTime"); + defaultIncludes.add("org.threeten.bp.LocalDate"); defaultIncludes.add("org.threeten.bp.LocalDateTime"); } else if (DateLibrary.STRING.value.equals(dateLibrary)) { typeMapping.put("date-time", "kotlin.String"); @@ -166,5 +167,8 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen { supportingFiles.add(new SupportingFile("infrastructure/Serializer.kt.mustache", infrastructureFolder, "Serializer.kt")); supportingFiles.add(new SupportingFile("infrastructure/Errors.kt.mustache", infrastructureFolder, "Errors.kt")); supportingFiles.add(new SupportingFile("infrastructure/ByteArrayAdapter.kt.mustache", infrastructureFolder, "ByteArrayAdapter.kt")); + supportingFiles.add(new SupportingFile("infrastructure/LocalDateAdapter.kt.mustache", infrastructureFolder, "LocalDateAdapter.kt")); + supportingFiles.add(new SupportingFile("infrastructure/LocalDateTimeAdapter.kt.mustache", infrastructureFolder, "LocalDateTimeAdapter.kt")); + supportingFiles.add(new SupportingFile("infrastructure/UUIDAdapter.kt.mustache", infrastructureFolder, "UUIDAdapter.kt")); } } diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/api.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/api.mustache index 012baf0fdca..6a38138788e 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/api.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/api.mustache @@ -15,9 +15,6 @@ import {{packageName}}.infrastructure.RequestMethod import {{packageName}}.infrastructure.ResponseType import {{packageName}}.infrastructure.Success import {{packageName}}.infrastructure.toMultiValue -{{#threetenbp}} -import org.threeten.bp.LocalDateTime -{{/threetenbp}} {{#operations}} class {{classname}}(basePath: kotlin.String = "{{{basePath}}}") : ApiClient(basePath) { diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiClient.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiClient.kt.mustache index 96ca5686c8f..8d147371bcc 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiClient.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/ApiClient.kt.mustache @@ -1,9 +1,8 @@ package {{packageName}}.infrastructure -import com.squareup.moshi.FromJson import com.squareup.moshi.KotlinJsonAdapterFactory import com.squareup.moshi.Moshi -import com.squareup.moshi.ToJson +import com.squareup.moshi.Rfc3339DateJsonAdapter import okhttp3.OkHttpClient import okhttp3.RequestBody import okhttp3.MediaType @@ -12,7 +11,7 @@ import okhttp3.HttpUrl import okhttp3.ResponseBody import okhttp3.Request import java.io.File -import java.util.UUID +import java.util.Date open class ApiClient(val baseUrl: String) { companion object { @@ -63,12 +62,11 @@ open class ApiClient(val baseUrl: String) { return null } return when(mediaType) { - JsonMediaType -> Moshi.Builder().add(object { - @ToJson - fun toJson(uuid: UUID) = uuid.toString() - @FromJson - fun fromJson(s: String) = UUID.fromString(s) - }) + JsonMediaType -> Moshi.Builder() + .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) + .add(LocalDateTimeAdapter()) + .add(LocalDateAdapter()) + .add(UUIDAdapter()) .add(ByteArrayAdapter()) .add(KotlinJsonAdapterFactory()) .build().adapter(T::class.java).fromJson(bodyContent) diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/LocalDateAdapter.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/LocalDateAdapter.kt.mustache new file mode 100644 index 00000000000..5f5d93af65d --- /dev/null +++ b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/LocalDateAdapter.kt.mustache @@ -0,0 +1,25 @@ +package {{packageName}}.infrastructure + +import com.squareup.moshi.FromJson +import com.squareup.moshi.ToJson +{{^threetenbp}} +import java.time.LocalDate +import java.time.format.DateTimeFormatter +{{/threetenbp}} +{{#threetenbp}} +import org.threeten.bp.LocalDate +import org.threeten.bp.format.DateTimeFormatter +{{/threetenbp}} + +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) + } + +} diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/LocalDateTimeAdapter.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/LocalDateTimeAdapter.kt.mustache new file mode 100644 index 00000000000..765144bad5a --- /dev/null +++ b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/LocalDateTimeAdapter.kt.mustache @@ -0,0 +1,25 @@ +package {{packageName}}.infrastructure + +import com.squareup.moshi.FromJson +import com.squareup.moshi.ToJson +{{^threetenbp}} +import java.time.LocalDateTime +import java.time.format.DateTimeFormatter +{{/threetenbp}} +{{#threetenbp}} +import org.threeten.bp.LocalDateTime +import org.threeten.bp.format.DateTimeFormatter +{{/threetenbp}} + +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) + } + +} diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/Serializer.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/Serializer.kt.mustache index 9d448cb9a73..7698d9ca084 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/Serializer.kt.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/Serializer.kt.mustache @@ -3,12 +3,16 @@ package {{packageName}}.infrastructure import com.squareup.moshi.KotlinJsonAdapterFactory import com.squareup.moshi.Moshi import com.squareup.moshi.Rfc3339DateJsonAdapter -import java.util.* +import java.util.Date object Serializer { @JvmStatic val moshi: Moshi = Moshi.Builder() - .add(KotlinJsonAdapterFactory()) .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) + .add(LocalDateTimeAdapter()) + .add(LocalDateAdapter()) + .add(UUIDAdapter()) + .add(ByteArrayAdapter()) + .add(KotlinJsonAdapterFactory()) .build() } diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/UUIDAdapter.kt.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/UUIDAdapter.kt.mustache new file mode 100644 index 00000000000..cd973554cd6 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/kotlin-client/infrastructure/UUIDAdapter.kt.mustache @@ -0,0 +1,13 @@ +package {{packageName}}.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.fromString(s) +} diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/model.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/model.mustache index f0c5a9bb4a4..b9514dad4d7 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/model.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/model.mustache @@ -3,9 +3,6 @@ package {{modelPackage}} {{#imports}}import {{import}} {{/imports}} -{{#threetenbp}} -import org.threeten.bp.LocalDateTime -{{/threetenbp}} {{#models}} {{#model}} diff --git a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 9a04597450e..a1079181cf0 100644 --- a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -1,9 +1,8 @@ package org.openapitools.client.infrastructure -import com.squareup.moshi.FromJson import com.squareup.moshi.KotlinJsonAdapterFactory import com.squareup.moshi.Moshi -import com.squareup.moshi.ToJson +import com.squareup.moshi.Rfc3339DateJsonAdapter import okhttp3.OkHttpClient import okhttp3.RequestBody import okhttp3.MediaType @@ -12,7 +11,7 @@ import okhttp3.HttpUrl import okhttp3.ResponseBody import okhttp3.Request import java.io.File -import java.util.UUID +import java.util.Date open class ApiClient(val baseUrl: String) { companion object { @@ -63,12 +62,11 @@ open class ApiClient(val baseUrl: String) { return null } return when(mediaType) { - JsonMediaType -> Moshi.Builder().add(object { - @ToJson - fun toJson(uuid: UUID) = uuid.toString() - @FromJson - fun fromJson(s: String) = UUID.fromString(s) - }) + JsonMediaType -> Moshi.Builder() + .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) + .add(LocalDateTimeAdapter()) + .add(LocalDateAdapter()) + .add(UUIDAdapter()) .add(ByteArrayAdapter()) .add(KotlinJsonAdapterFactory()) .build().adapter(T::class.java).fromJson(bodyContent) diff --git a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateAdapter.kt b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateAdapter.kt new file mode 100644 index 00000000000..b2e1654479a --- /dev/null +++ b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateAdapter.kt @@ -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) + } + +} diff --git a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateTimeAdapter.kt b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateTimeAdapter.kt new file mode 100644 index 00000000000..e082db94811 --- /dev/null +++ b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateTimeAdapter.kt @@ -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) + } + +} diff --git a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt index cf3fe8203d5..e3463f0e3c1 100644 --- a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt +++ b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt @@ -3,12 +3,16 @@ package org.openapitools.client.infrastructure import com.squareup.moshi.KotlinJsonAdapterFactory import com.squareup.moshi.Moshi import com.squareup.moshi.Rfc3339DateJsonAdapter -import java.util.* +import java.util.Date object Serializer { @JvmStatic val moshi: Moshi = Moshi.Builder() - .add(KotlinJsonAdapterFactory()) .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) + .add(LocalDateTimeAdapter()) + .add(LocalDateAdapter()) + .add(UUIDAdapter()) + .add(ByteArrayAdapter()) + .add(KotlinJsonAdapterFactory()) .build() } diff --git a/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/UUIDAdapter.kt b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/UUIDAdapter.kt new file mode 100644 index 00000000000..a4a44cc18b7 --- /dev/null +++ b/samples/client/petstore/kotlin-string/src/main/kotlin/org/openapitools/client/infrastructure/UUIDAdapter.kt @@ -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.fromString(s) +} diff --git a/samples/client/petstore/kotlin-string/src/test/kotlin/org/openapitools/client/PetApiTest.kt b/samples/client/petstore/kotlin-string/src/test/kotlin/org/openapitools/client/PetApiTest.kt index ad28e4cf8ea..d1c20493bcc 100644 --- a/samples/client/petstore/kotlin-string/src/test/kotlin/org/openapitools/client/PetApiTest.kt +++ b/samples/client/petstore/kotlin-string/src/test/kotlin/org/openapitools/client/PetApiTest.kt @@ -2,8 +2,11 @@ package org.openapitools.client import io.kotlintest.shouldBe import io.kotlintest.matchers.numerics.shouldBeGreaterThan +import io.kotlintest.matchers.string.shouldContain +import io.kotlintest.shouldThrow import io.kotlintest.specs.ShouldSpec import org.openapitools.client.apis.PetApi +import org.openapitools.client.infrastructure.ClientException import org.openapitools.client.models.Category import org.openapitools.client.models.Pet import org.openapitools.client.models.Tag @@ -11,8 +14,11 @@ import org.openapitools.client.models.Tag class PetApiTest : ShouldSpec() { init { + val petId:Long = 10006 + val api = PetApi() + should("add a pet") { - val petId:Long = 10006 + val pet = Pet( id = petId, name = "kotlin client test", @@ -20,15 +26,11 @@ class PetApiTest : ShouldSpec() { category = Category(petId, "test kotlin category"), tags = arrayOf(Tag(petId, "test kotlin tag")) ) - - val api = PetApi() api.addPet(pet) } should("get pet by id") { - val petId: Long = 10006 - val api = PetApi() val result = api.getPetById(petId) result.id shouldBe (petId) @@ -42,7 +44,6 @@ class PetApiTest : ShouldSpec() { } should("find pet by status") { - val api = PetApi() val result = api.findPetsByStatus(arrayOf("available")) result.size.shouldBeGreaterThan(0) @@ -58,15 +59,12 @@ class PetApiTest : ShouldSpec() { } should("update a pet") { - val petId:Long = 10007 val pet = Pet( id = petId, name = "kotlin client updatePet", status = Pet.Status.pending, photoUrls = arrayOf("http://test_kotlin_unit_test.com") ) - - val api = PetApi() api.updatePet(pet) // verify updated Pet @@ -77,14 +75,10 @@ class PetApiTest : ShouldSpec() { } - //TODO the test fail cause client doesn't support other JSON contentType/Accept - /* should("update a pet with form") { - val petId:Long = 10007 val name = "kotlin client updatePet with Form" val status = "pending" - val api = PetApi() api.updatePetWithForm(petId, name, status) // verify updated Pet @@ -94,7 +88,16 @@ class PetApiTest : ShouldSpec() { result.status shouldBe (Pet.Status.pending) } - */ + + should("delete a pet") { + api.deletePet(petId, "apiKey") + + // verify updated Pet + val exception = shouldThrow { + api.getPetById(petId) + } + exception.message?.shouldContain("Pet not found") + } } diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 16fa15b6d6c..36c7ad82613 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -25,7 +25,6 @@ import org.openapitools.client.infrastructure.RequestMethod import org.openapitools.client.infrastructure.ResponseType import org.openapitools.client.infrastructure.Success import org.openapitools.client.infrastructure.toMultiValue -import org.threeten.bp.LocalDateTime class PetApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiClient(basePath) { diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt index 68cfec2bc37..891b91d6814 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/StoreApi.kt @@ -24,7 +24,6 @@ import org.openapitools.client.infrastructure.RequestMethod import org.openapitools.client.infrastructure.ResponseType import org.openapitools.client.infrastructure.Success import org.openapitools.client.infrastructure.toMultiValue -import org.threeten.bp.LocalDateTime class StoreApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiClient(basePath) { diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/UserApi.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/UserApi.kt index 3c31014133f..ea538f439a1 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/UserApi.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/apis/UserApi.kt @@ -24,7 +24,6 @@ import org.openapitools.client.infrastructure.RequestMethod import org.openapitools.client.infrastructure.ResponseType import org.openapitools.client.infrastructure.Success import org.openapitools.client.infrastructure.toMultiValue -import org.threeten.bp.LocalDateTime class UserApi(basePath: kotlin.String = "http://petstore.swagger.io/v2") : ApiClient(basePath) { diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 9a04597450e..a1079181cf0 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -1,9 +1,8 @@ package org.openapitools.client.infrastructure -import com.squareup.moshi.FromJson import com.squareup.moshi.KotlinJsonAdapterFactory import com.squareup.moshi.Moshi -import com.squareup.moshi.ToJson +import com.squareup.moshi.Rfc3339DateJsonAdapter import okhttp3.OkHttpClient import okhttp3.RequestBody import okhttp3.MediaType @@ -12,7 +11,7 @@ import okhttp3.HttpUrl import okhttp3.ResponseBody import okhttp3.Request import java.io.File -import java.util.UUID +import java.util.Date open class ApiClient(val baseUrl: String) { companion object { @@ -63,12 +62,11 @@ open class ApiClient(val baseUrl: String) { return null } return when(mediaType) { - JsonMediaType -> Moshi.Builder().add(object { - @ToJson - fun toJson(uuid: UUID) = uuid.toString() - @FromJson - fun fromJson(s: String) = UUID.fromString(s) - }) + JsonMediaType -> Moshi.Builder() + .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) + .add(LocalDateTimeAdapter()) + .add(LocalDateAdapter()) + .add(UUIDAdapter()) .add(ByteArrayAdapter()) .add(KotlinJsonAdapterFactory()) .build().adapter(T::class.java).fromJson(bodyContent) diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateAdapter.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateAdapter.kt new file mode 100644 index 00000000000..ff5439aeb42 --- /dev/null +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateAdapter.kt @@ -0,0 +1,19 @@ +package org.openapitools.client.infrastructure + +import com.squareup.moshi.FromJson +import com.squareup.moshi.ToJson +import org.threeten.bp.LocalDate +import org.threeten.bp.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) + } + +} diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateTimeAdapter.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateTimeAdapter.kt new file mode 100644 index 00000000000..710a9cc1317 --- /dev/null +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateTimeAdapter.kt @@ -0,0 +1,19 @@ +package org.openapitools.client.infrastructure + +import com.squareup.moshi.FromJson +import com.squareup.moshi.ToJson +import org.threeten.bp.LocalDateTime +import org.threeten.bp.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) + } + +} diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt index cf3fe8203d5..e3463f0e3c1 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt @@ -3,12 +3,16 @@ package org.openapitools.client.infrastructure import com.squareup.moshi.KotlinJsonAdapterFactory import com.squareup.moshi.Moshi import com.squareup.moshi.Rfc3339DateJsonAdapter -import java.util.* +import java.util.Date object Serializer { @JvmStatic val moshi: Moshi = Moshi.Builder() - .add(KotlinJsonAdapterFactory()) .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) + .add(LocalDateTimeAdapter()) + .add(LocalDateAdapter()) + .add(UUIDAdapter()) + .add(ByteArrayAdapter()) + .add(KotlinJsonAdapterFactory()) .build() } diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/UUIDAdapter.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/UUIDAdapter.kt new file mode 100644 index 00000000000..a4a44cc18b7 --- /dev/null +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/infrastructure/UUIDAdapter.kt @@ -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.fromString(s) +} diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/ApiResponse.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/ApiResponse.kt index f22a6670fa9..47109faf612 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/ApiResponse.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/ApiResponse.kt @@ -11,7 +11,6 @@ */ package org.openapitools.client.models -import org.threeten.bp.LocalDateTime import com.squareup.moshi.Json /** diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/Category.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/Category.kt index e0de72025fe..f068a02b1a0 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/Category.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/Category.kt @@ -11,7 +11,6 @@ */ package org.openapitools.client.models -import org.threeten.bp.LocalDateTime import com.squareup.moshi.Json /** diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/Order.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/Order.kt index 277cb9b6054..916fe3094b2 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/Order.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/Order.kt @@ -11,7 +11,6 @@ */ package org.openapitools.client.models -import org.threeten.bp.LocalDateTime import com.squareup.moshi.Json /** diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/Pet.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/Pet.kt index 0b296b0a424..88a536ea622 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/Pet.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/Pet.kt @@ -13,7 +13,6 @@ package org.openapitools.client.models import org.openapitools.client.models.Category import org.openapitools.client.models.Tag -import org.threeten.bp.LocalDateTime import com.squareup.moshi.Json /** diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/Tag.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/Tag.kt index 147643f4ff2..e67b899b1ff 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/Tag.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/Tag.kt @@ -11,7 +11,6 @@ */ package org.openapitools.client.models -import org.threeten.bp.LocalDateTime import com.squareup.moshi.Json /** diff --git a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/User.kt b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/User.kt index 8ecf3a0de2e..6a66d8e523b 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/User.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/main/kotlin/org/openapitools/client/models/User.kt @@ -11,7 +11,6 @@ */ package org.openapitools.client.models -import org.threeten.bp.LocalDateTime import com.squareup.moshi.Json /** diff --git a/samples/client/petstore/kotlin-threetenbp/src/test/kotlin/org/openapitools/client/PetApiTest.kt b/samples/client/petstore/kotlin-threetenbp/src/test/kotlin/org/openapitools/client/PetApiTest.kt index ad28e4cf8ea..d1c20493bcc 100644 --- a/samples/client/petstore/kotlin-threetenbp/src/test/kotlin/org/openapitools/client/PetApiTest.kt +++ b/samples/client/petstore/kotlin-threetenbp/src/test/kotlin/org/openapitools/client/PetApiTest.kt @@ -2,8 +2,11 @@ package org.openapitools.client import io.kotlintest.shouldBe import io.kotlintest.matchers.numerics.shouldBeGreaterThan +import io.kotlintest.matchers.string.shouldContain +import io.kotlintest.shouldThrow import io.kotlintest.specs.ShouldSpec import org.openapitools.client.apis.PetApi +import org.openapitools.client.infrastructure.ClientException import org.openapitools.client.models.Category import org.openapitools.client.models.Pet import org.openapitools.client.models.Tag @@ -11,8 +14,11 @@ import org.openapitools.client.models.Tag class PetApiTest : ShouldSpec() { init { + val petId:Long = 10006 + val api = PetApi() + should("add a pet") { - val petId:Long = 10006 + val pet = Pet( id = petId, name = "kotlin client test", @@ -20,15 +26,11 @@ class PetApiTest : ShouldSpec() { category = Category(petId, "test kotlin category"), tags = arrayOf(Tag(petId, "test kotlin tag")) ) - - val api = PetApi() api.addPet(pet) } should("get pet by id") { - val petId: Long = 10006 - val api = PetApi() val result = api.getPetById(petId) result.id shouldBe (petId) @@ -42,7 +44,6 @@ class PetApiTest : ShouldSpec() { } should("find pet by status") { - val api = PetApi() val result = api.findPetsByStatus(arrayOf("available")) result.size.shouldBeGreaterThan(0) @@ -58,15 +59,12 @@ class PetApiTest : ShouldSpec() { } should("update a pet") { - val petId:Long = 10007 val pet = Pet( id = petId, name = "kotlin client updatePet", status = Pet.Status.pending, photoUrls = arrayOf("http://test_kotlin_unit_test.com") ) - - val api = PetApi() api.updatePet(pet) // verify updated Pet @@ -77,14 +75,10 @@ class PetApiTest : ShouldSpec() { } - //TODO the test fail cause client doesn't support other JSON contentType/Accept - /* should("update a pet with form") { - val petId:Long = 10007 val name = "kotlin client updatePet with Form" val status = "pending" - val api = PetApi() api.updatePetWithForm(petId, name, status) // verify updated Pet @@ -94,7 +88,16 @@ class PetApiTest : ShouldSpec() { result.status shouldBe (Pet.Status.pending) } - */ + + should("delete a pet") { + api.deletePet(petId, "apiKey") + + // verify updated Pet + val exception = shouldThrow { + api.getPetById(petId) + } + exception.message?.shouldContain("Pet not found") + } } diff --git a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt index 9a04597450e..a1079181cf0 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt @@ -1,9 +1,8 @@ package org.openapitools.client.infrastructure -import com.squareup.moshi.FromJson import com.squareup.moshi.KotlinJsonAdapterFactory import com.squareup.moshi.Moshi -import com.squareup.moshi.ToJson +import com.squareup.moshi.Rfc3339DateJsonAdapter import okhttp3.OkHttpClient import okhttp3.RequestBody import okhttp3.MediaType @@ -12,7 +11,7 @@ import okhttp3.HttpUrl import okhttp3.ResponseBody import okhttp3.Request import java.io.File -import java.util.UUID +import java.util.Date open class ApiClient(val baseUrl: String) { companion object { @@ -63,12 +62,11 @@ open class ApiClient(val baseUrl: String) { return null } return when(mediaType) { - JsonMediaType -> Moshi.Builder().add(object { - @ToJson - fun toJson(uuid: UUID) = uuid.toString() - @FromJson - fun fromJson(s: String) = UUID.fromString(s) - }) + JsonMediaType -> Moshi.Builder() + .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) + .add(LocalDateTimeAdapter()) + .add(LocalDateAdapter()) + .add(UUIDAdapter()) .add(ByteArrayAdapter()) .add(KotlinJsonAdapterFactory()) .build().adapter(T::class.java).fromJson(bodyContent) diff --git a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateAdapter.kt b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateAdapter.kt new file mode 100644 index 00000000000..b2e1654479a --- /dev/null +++ b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateAdapter.kt @@ -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) + } + +} diff --git a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateTimeAdapter.kt b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateTimeAdapter.kt new file mode 100644 index 00000000000..e082db94811 --- /dev/null +++ b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/LocalDateTimeAdapter.kt @@ -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) + } + +} diff --git a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt index cf3fe8203d5..e3463f0e3c1 100644 --- a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt +++ b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt @@ -3,12 +3,16 @@ package org.openapitools.client.infrastructure import com.squareup.moshi.KotlinJsonAdapterFactory import com.squareup.moshi.Moshi import com.squareup.moshi.Rfc3339DateJsonAdapter -import java.util.* +import java.util.Date object Serializer { @JvmStatic val moshi: Moshi = Moshi.Builder() - .add(KotlinJsonAdapterFactory()) .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) + .add(LocalDateTimeAdapter()) + .add(LocalDateAdapter()) + .add(UUIDAdapter()) + .add(ByteArrayAdapter()) + .add(KotlinJsonAdapterFactory()) .build() } diff --git a/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/UUIDAdapter.kt b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/UUIDAdapter.kt new file mode 100644 index 00000000000..a4a44cc18b7 --- /dev/null +++ b/samples/client/petstore/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/UUIDAdapter.kt @@ -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.fromString(s) +} diff --git a/samples/client/petstore/kotlin/src/test/kotlin/org/openapitools/client/StoreApiTest.kt b/samples/client/petstore/kotlin/src/test/kotlin/org/openapitools/client/StoreApiTest.kt new file mode 100644 index 00000000000..1807e516eb0 --- /dev/null +++ b/samples/client/petstore/kotlin/src/test/kotlin/org/openapitools/client/StoreApiTest.kt @@ -0,0 +1,48 @@ +package org.openapitools.client + +import io.kotlintest.shouldBe +import io.kotlintest.specs.ShouldSpec +import org.openapitools.client.apis.PetApi +import org.openapitools.client.apis.StoreApi +import org.openapitools.client.models.Order +import org.openapitools.client.models.Pet +import java.time.LocalDateTime.now + +class StoreApiTest : ShouldSpec() { + init { + + val petId:Long = 10006 + val petApi = PetApi() + val storeApi = StoreApi() + + val pet = Pet( + id = petId, + name = "kotlin client test", + photoUrls = arrayOf("http://test_kotlin_unit_test.com") + ) + petApi.addPet(pet) + + should("add an order") { + + val order = Order( + id = 12, + petId = petId, + quantity = 2, + shipDate = now(), + complete = true + ) + storeApi.placeOrder(order); + + } + + should("get order by id") { + val result = storeApi.getOrderById(12) + + // verify order + result.petId shouldBe (petId) + result.quantity shouldBe (2) + } + + } + +}