forked from loafle/openapi-generator-original
[kotlin-client][multiplatform] add support for kotlinx.datetime.LocalTime (#19590)
This commit is contained in:
parent
3832cb4eb7
commit
1b30c1995f
@ -593,12 +593,15 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
|
||||
|
||||
typeMapping.put("date-time", "Instant");
|
||||
typeMapping.put("date", "LocalDate");
|
||||
typeMapping.put("time", "LocalTime");
|
||||
|
||||
typeMapping.put("DateTime", "Instant");
|
||||
typeMapping.put("Date", "LocalDate");
|
||||
typeMapping.put("Time", "LocalTime");
|
||||
|
||||
importMapping.put("Instant", "kotlinx.datetime.Instant");
|
||||
importMapping.put("LocalDate", "kotlinx.datetime.LocalDate");
|
||||
importMapping.put("LocalTime", "kotlinx.datetime.LocalTime");
|
||||
}
|
||||
|
||||
private void processJVMRetrofit2Library(String infrastructureFolder) {
|
||||
@ -664,7 +667,7 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
|
||||
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/LocalDateAdapter.kt.mustache", infrastructureFolder, "LocalDateAdapter.kt"));
|
||||
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/LocalDateTimeAdapter.kt.mustache", infrastructureFolder, "LocalDateTimeAdapter.kt"));
|
||||
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/OffsetDateTimeAdapter.kt.mustache", infrastructureFolder, "OffsetDateTimeAdapter.kt"));
|
||||
addKotlinxDateTimeInstantAdapter(infrastructureFolder);
|
||||
addKotlinxDateTimeAdapters(infrastructureFolder);
|
||||
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/BigDecimalAdapter.kt.mustache", infrastructureFolder, "BigDecimalAdapter.kt"));
|
||||
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/BigIntegerAdapter.kt.mustache", infrastructureFolder, "BigIntegerAdapter.kt"));
|
||||
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/URIAdapter.kt.mustache", infrastructureFolder, "URIAdapter.kt"));
|
||||
@ -675,7 +678,7 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
|
||||
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/LocalDateAdapter.kt.mustache", infrastructureFolder, "LocalDateAdapter.kt"));
|
||||
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/LocalDateTimeAdapter.kt.mustache", infrastructureFolder, "LocalDateTimeAdapter.kt"));
|
||||
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/OffsetDateTimeAdapter.kt.mustache", infrastructureFolder, "OffsetDateTimeAdapter.kt"));
|
||||
addKotlinxDateTimeInstantAdapter(infrastructureFolder);
|
||||
addKotlinxDateTimeAdapters(infrastructureFolder);
|
||||
break;
|
||||
|
||||
case jackson:
|
||||
@ -699,9 +702,10 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
|
||||
}
|
||||
}
|
||||
|
||||
private void addKotlinxDateTimeInstantAdapter(final String infrastructureFolder) {
|
||||
private void addKotlinxDateTimeAdapters(final String infrastructureFolder) {
|
||||
if (DateLibrary.KOTLINX_DATETIME.value.equals(dateLibrary)) {
|
||||
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/InstantAdapter.kt.mustache", infrastructureFolder, "InstantAdapter.kt"));
|
||||
supportingFiles.add(new SupportingFile("jvm-common/infrastructure/LocalTimeAdapter.kt.mustache", infrastructureFolder, "LocalTimeAdapter.kt"));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,36 +21,51 @@ import kotlinx.serialization.descriptors.PrimitiveKind
|
||||
import kotlinx.serialization.descriptors.SerialDescriptor
|
||||
{{/kotlinx_serialization}}
|
||||
{{^threetenbp}}
|
||||
{{^kotlinx-datetime}}
|
||||
import java.time.LocalDate
|
||||
import java.time.format.DateTimeFormatter
|
||||
{{/kotlinx-datetime}}
|
||||
{{/threetenbp}}
|
||||
{{#threetenbp}}
|
||||
import org.threeten.bp.LocalDate
|
||||
import org.threeten.bp.format.DateTimeFormatter
|
||||
{{/threetenbp}}
|
||||
{{#kotlinx-datetime}}
|
||||
import kotlinx.datetime.LocalDate
|
||||
{{/kotlinx-datetime}}
|
||||
|
||||
{{#moshi}}
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalDateAdapter {
|
||||
@ToJson
|
||||
fun toJson(value: LocalDate): String {
|
||||
{{#kotlinx-datetime}}
|
||||
return value.toString()
|
||||
{{/kotlinx-datetime}}
|
||||
{{^kotlinx-datetime}}
|
||||
return DateTimeFormatter.ISO_LOCAL_DATE.format(value)
|
||||
{{/kotlinx-datetime}}
|
||||
}
|
||||
|
||||
@FromJson
|
||||
fun fromJson(value: String): LocalDate {
|
||||
return LocalDate.parse(value, DateTimeFormatter.ISO_LOCAL_DATE)
|
||||
return LocalDate.parse(value{{^kotlinx-datetime}}, DateTimeFormatter.ISO_LOCAL_DATE{{/kotlinx-datetime}})
|
||||
}
|
||||
|
||||
}
|
||||
{{/moshi}}
|
||||
{{#gson}}
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalDateAdapter(private val formatter: DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE) : TypeAdapter<LocalDate>() {
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalDateAdapter({{^kotlinx-datetime}}private val formatter: DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE{{/kotlinx-datetime}}) : TypeAdapter<LocalDate>() {
|
||||
@Throws(IOException::class)
|
||||
override fun write(out: JsonWriter?, value: LocalDate?) {
|
||||
if (value == null) {
|
||||
out?.nullValue()
|
||||
} else {
|
||||
{{#kotlinx-datetime}}
|
||||
out?.value(value.toString())
|
||||
{{/kotlinx-datetime}}
|
||||
{{^kotlinx-datetime}}
|
||||
out?.value(formatter.format(value))
|
||||
{{/kotlinx-datetime}}
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,13 +79,14 @@ import org.threeten.bp.format.DateTimeFormatter
|
||||
return null
|
||||
}
|
||||
else -> {
|
||||
return LocalDate.parse(out.nextString(), formatter)
|
||||
return LocalDate.parse(out.nextString(){{^kotlinx-datetime}}, formatter{{/kotlinx-datetime}})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
{{/gson}}
|
||||
{{#kotlinx_serialization}}
|
||||
{{^kotlinx-datetime}}
|
||||
@Serializer(forClass = LocalDate::class)
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}object LocalDateAdapter : KSerializer<LocalDate> {
|
||||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("LocalDate", PrimitiveKind.STRING)
|
||||
@ -83,4 +99,5 @@ import org.threeten.bp.format.DateTimeFormatter
|
||||
return LocalDate.parse(decoder.decodeString(), DateTimeFormatter.ISO_LOCAL_DATE)
|
||||
}
|
||||
}
|
||||
{{/kotlinx-datetime}}
|
||||
{{/kotlinx_serialization}}
|
||||
|
@ -21,36 +21,51 @@ import kotlinx.serialization.descriptors.PrimitiveKind
|
||||
import kotlinx.serialization.descriptors.SerialDescriptor
|
||||
{{/kotlinx_serialization}}
|
||||
{{^threetenbp}}
|
||||
{{^kotlinx-datetime}}
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
{{/kotlinx-datetime}}
|
||||
{{/threetenbp}}
|
||||
{{#threetenbp}}
|
||||
import org.threeten.bp.LocalDateTime
|
||||
import org.threeten.bp.format.DateTimeFormatter
|
||||
{{/threetenbp}}
|
||||
{{#kotlinx-datetime}}
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
{{/kotlinx-datetime}}
|
||||
|
||||
{{#moshi}}
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalDateTimeAdapter {
|
||||
@ToJson
|
||||
fun toJson(value: LocalDateTime): String {
|
||||
{{#kotlinx-datetime}}
|
||||
return value.toString()
|
||||
{{/kotlinx-datetime}}
|
||||
{{^kotlinx-datetime}}
|
||||
return DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(value)
|
||||
{{/kotlinx-datetime}}
|
||||
}
|
||||
|
||||
@FromJson
|
||||
fun fromJson(value: String): LocalDateTime {
|
||||
return LocalDateTime.parse(value, DateTimeFormatter.ISO_LOCAL_DATE_TIME)
|
||||
return LocalDateTime.parse(value{{^kotlinx-datetime}}, DateTimeFormatter.ISO_LOCAL_DATE_TIME{{/kotlinx-datetime}})
|
||||
}
|
||||
|
||||
}
|
||||
{{/moshi}}
|
||||
{{#gson}}
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalDateTimeAdapter(private val formatter: DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME) : TypeAdapter<LocalDateTime>() {
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalDateTimeAdapter({{^kotlinx-datetime}}private val formatter: DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME{{/kotlinx-datetime}}) : TypeAdapter<LocalDateTime>() {
|
||||
@Throws(IOException::class)
|
||||
override fun write(out: JsonWriter?, value: LocalDateTime?) {
|
||||
if (value == null) {
|
||||
out?.nullValue()
|
||||
} else {
|
||||
{{#kotlinx-datetime}}
|
||||
out?.value(value.toString())
|
||||
{{/kotlinx-datetime}}
|
||||
{{^kotlinx-datetime}}
|
||||
out?.value(formatter.format(value))
|
||||
{{/kotlinx-datetime}}
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,13 +79,14 @@ import org.threeten.bp.format.DateTimeFormatter
|
||||
return null
|
||||
}
|
||||
else -> {
|
||||
return LocalDateTime.parse(out.nextString(), formatter)
|
||||
return LocalDateTime.parse(out.nextString(){{^kotlinx-datetime}}, formatter{{/kotlinx-datetime}})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
{{/gson}}
|
||||
{{#kotlinx_serialization}}
|
||||
{{^kotlinx-datetime}}
|
||||
@Serializer(forClass = LocalDateTime::class)
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}object LocalDateTimeAdapter : KSerializer<LocalDateTime> {
|
||||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("LocalDateTime", PrimitiveKind.STRING)
|
||||
@ -83,4 +99,5 @@ import org.threeten.bp.format.DateTimeFormatter
|
||||
return LocalDateTime.parse(decoder.decodeString(), DateTimeFormatter.ISO_LOCAL_DATE_TIME)
|
||||
}
|
||||
}
|
||||
{{/kotlinx-datetime}}
|
||||
{{/kotlinx_serialization}}
|
||||
|
@ -0,0 +1,56 @@
|
||||
package {{packageName}}.infrastructure
|
||||
|
||||
{{#moshi}}
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.ToJson
|
||||
{{/moshi}}
|
||||
{{#gson}}
|
||||
import com.google.gson.TypeAdapter
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import com.google.gson.stream.JsonToken.NULL
|
||||
import java.io.IOException
|
||||
{{/gson}}
|
||||
import kotlinx.datetime.LocalTime
|
||||
|
||||
{{#moshi}}
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalTimeAdapter {
|
||||
@ToJson
|
||||
fun toJson(value: LocalTime): String {
|
||||
return value.toString()
|
||||
}
|
||||
|
||||
@FromJson
|
||||
fun fromJson(value: String): LocalTime {
|
||||
return LocalTime.parse(value)
|
||||
}
|
||||
|
||||
}
|
||||
{{/moshi}}
|
||||
{{#gson}}
|
||||
{{#nonPublicApi}}internal {{/nonPublicApi}}class LocalTimeAdapter : TypeAdapter<LocalTime>() {
|
||||
@Throws(IOException::class)
|
||||
override fun write(out: JsonWriter?, value: LocalTime?) {
|
||||
if (value == null) {
|
||||
out?.nullValue()
|
||||
} else {
|
||||
out?.value(value.toString())
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun read(out: JsonReader?): LocalTime? {
|
||||
out ?: return null
|
||||
|
||||
when (out.peek()) {
|
||||
NULL -> {
|
||||
out.nextNull()
|
||||
return null
|
||||
}
|
||||
else -> {
|
||||
return LocalTime.parse(out.nextString())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
{{/gson}}
|
@ -24,6 +24,7 @@ import org.threeten.bp.OffsetDateTime
|
||||
{{/threetenbp}}
|
||||
{{#kotlinx-datetime}}
|
||||
import kotlinx.datetime.Instant
|
||||
import kotlinx.datetime.LocalTime
|
||||
{{/kotlinx-datetime}}
|
||||
import java.util.UUID
|
||||
{{/gson}}
|
||||
@ -64,6 +65,7 @@ import java.util.concurrent.atomic.AtomicLong
|
||||
.add(OffsetDateTimeAdapter())
|
||||
{{#kotlinx-datetime}}
|
||||
.add(InstantAdapter())
|
||||
.add(LocalTimeAdapter())
|
||||
{{/kotlinx-datetime}}
|
||||
.add(LocalDateTimeAdapter())
|
||||
.add(LocalDateAdapter())
|
||||
@ -90,6 +92,7 @@ import java.util.concurrent.atomic.AtomicLong
|
||||
.registerTypeAdapter(OffsetDateTime::class.java, OffsetDateTimeAdapter())
|
||||
{{#kotlinx-datetime}}
|
||||
.registerTypeAdapter(Instant::class.java, InstantAdapter())
|
||||
.registerTypeAdapter(LocalTime::class.java, LocalTimeAdapter())
|
||||
{{/kotlinx-datetime}}
|
||||
.registerTypeAdapter(LocalDateTime::class.java, LocalDateTimeAdapter())
|
||||
.registerTypeAdapter(LocalDate::class.java, LocalDateAdapter())
|
||||
@ -121,9 +124,11 @@ import java.util.concurrent.atomic.AtomicLong
|
||||
val kotlinxSerializationAdapters = SerializersModule {
|
||||
contextual(BigDecimal::class, BigDecimalAdapter)
|
||||
contextual(BigInteger::class, BigIntegerAdapter)
|
||||
{{^kotlinx-datetime}}
|
||||
contextual(LocalDate::class, LocalDateAdapter)
|
||||
contextual(LocalDateTime::class, LocalDateTimeAdapter)
|
||||
contextual(OffsetDateTime::class, OffsetDateTimeAdapter)
|
||||
{{/kotlinx-datetime}}
|
||||
contextual(UUID::class, UUIDAdapter)
|
||||
contextual(AtomicInteger::class, AtomicIntegerAdapter)
|
||||
contextual(AtomicLong::class, AtomicLongAdapter)
|
||||
|
@ -27,6 +27,7 @@ src/main/kotlin/org/openapitools/client/infrastructure/Errors.kt
|
||||
src/main/kotlin/org/openapitools/client/infrastructure/InstantAdapter.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/LocalTimeAdapter.kt
|
||||
src/main/kotlin/org/openapitools/client/infrastructure/OffsetDateTimeAdapter.kt
|
||||
src/main/kotlin/org/openapitools/client/infrastructure/PartConfig.kt
|
||||
src/main/kotlin/org/openapitools/client/infrastructure/RequestConfig.kt
|
||||
|
@ -2,18 +2,17 @@ package org.openapitools.client.infrastructure
|
||||
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.ToJson
|
||||
import java.time.LocalDate
|
||||
import java.time.format.DateTimeFormatter
|
||||
import kotlinx.datetime.LocalDate
|
||||
|
||||
class LocalDateAdapter {
|
||||
@ToJson
|
||||
fun toJson(value: LocalDate): String {
|
||||
return DateTimeFormatter.ISO_LOCAL_DATE.format(value)
|
||||
return value.toString()
|
||||
}
|
||||
|
||||
@FromJson
|
||||
fun fromJson(value: String): LocalDate {
|
||||
return LocalDate.parse(value, DateTimeFormatter.ISO_LOCAL_DATE)
|
||||
return LocalDate.parse(value)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,18 +2,17 @@ package org.openapitools.client.infrastructure
|
||||
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.ToJson
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
|
||||
class LocalDateTimeAdapter {
|
||||
@ToJson
|
||||
fun toJson(value: LocalDateTime): String {
|
||||
return DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(value)
|
||||
return value.toString()
|
||||
}
|
||||
|
||||
@FromJson
|
||||
fun fromJson(value: String): LocalDateTime {
|
||||
return LocalDateTime.parse(value, DateTimeFormatter.ISO_LOCAL_DATE_TIME)
|
||||
return LocalDateTime.parse(value)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,18 @@
|
||||
package org.openapitools.client.infrastructure
|
||||
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.ToJson
|
||||
import kotlinx.datetime.LocalTime
|
||||
|
||||
class LocalTimeAdapter {
|
||||
@ToJson
|
||||
fun toJson(value: LocalTime): String {
|
||||
return value.toString()
|
||||
}
|
||||
|
||||
@FromJson
|
||||
fun fromJson(value: String): LocalTime {
|
||||
return LocalTime.parse(value)
|
||||
}
|
||||
|
||||
}
|
@ -8,6 +8,7 @@ object Serializer {
|
||||
val moshiBuilder: Moshi.Builder = Moshi.Builder()
|
||||
.add(OffsetDateTimeAdapter())
|
||||
.add(InstantAdapter())
|
||||
.add(LocalTimeAdapter())
|
||||
.add(LocalDateTimeAdapter())
|
||||
.add(LocalDateAdapter())
|
||||
.add(UUIDAdapter())
|
||||
|
Loading…
x
Reference in New Issue
Block a user