Verified Commit c80c6599 authored by Konstantin Kopper's avatar Konstantin Kopper
Browse files

Replace gson with kotlinx serialization for update checks

parent e2399846
Pipeline #27086 failed with stages
in 21 seconds
......@@ -51,7 +51,7 @@ dependencies {
// Ktor Apache client and server framework
implementation("io.ktor", "ktor-client-apache", ktorVersion)
implementation("io.ktor", "ktor-client-gson", ktorVersion)
implementation("io.ktor", "ktor-client-serialization", ktorVersion)
implementation("io.ktor", "ktor-server-netty", ktorVersion)
implementation("io.ktor", "ktor-websockets", ktorVersion)
implementation("io.ktor", "ktor-freemarker", ktorVersion)
......
@file:UseSerializers(DateSerializer::class)
package org.gitlab.api.v4.commits
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
import util.DateSerializer
import java.util.Date
/**
......@@ -14,24 +20,26 @@ import java.util.Date
* @property authorEmail TODO
* @property authoredDate TODO
* @property committerName TODO
* @property committerEmail TODO
* @property committedDate TODO
* @property createdAt TODO
* @property message The commit message.
* @property parentIds A list containing the commit SHAs of the parent commits.
*/
@Serializable
data class Commit(
val id: String,
val shortId: String,
@SerialName("short_id") val shortId: String,
val title: String,
val authorName: String,
val authorEmail: String,
val authoredDate: Date,
val committerName: String,
val committedDate: Date,
val createdAt: Date,
@SerialName("author_name") val authorName: String,
@SerialName("author_email") val authorEmail: String,
@SerialName("authored_date") val authoredDate: Date,
@SerialName("committer_name") val committerName: String,
@SerialName("committer_email") val committerEmail: String,
@SerialName("committed_date") val committedDate: Date,
@SerialName("created_at") val createdAt: Date,
val message: String,
val parentIds: List<String>,
val stats: Statistics?
@SerialName("parent_ids") val parentIds: List<String>,
) {
init {
......
package org.gitlab.api.v4.commits
data class Statistics(val additions: Int, val deletions: Int, val total: Int) {
init {
assert(additions + deletions == total) { "Inconsistent statistics" }
}
}
package org.gitlab.api.v4.tags
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
/**
* The release information associated with a [Tag] as returned by the GitLab API v4.
*
......@@ -8,4 +11,5 @@ package org.gitlab.api.v4.tags
* @property tagName The name of the tag this release information is associated with.
* @property description The release notes.
*/
data class Release(val tagName: String, val description: String)
@Serializable
data class Release(@SerialName("tag_name") val tagName: String, val description: String)
package org.gitlab.api.v4.tags
import kotlinx.serialization.Serializable
import org.gitlab.api.v4.commits.Commit
/**
......@@ -13,6 +14,7 @@ import org.gitlab.api.v4.commits.Commit
* @property release The release information associated with this tag, `null` if none is available.
* @property target The SHA of the commit this tag refers to.
*/
@Serializable
data class Tag(
val commit: Commit,
val message: String?,
......@@ -22,6 +24,14 @@ data class Tag(
) {
init {
assert(target == commit.id)
/*
* The message is null when creating a lightweight tag. Otherwise, it contains the annotation.
*
* The target contains the tag objects ID when creating annotated tags,
* otherwise it contains the commit ID when creating lightweight tags.
*
* https://docs.gitlab.com/ee/api/tags.html
*/
assert(message != null || target == commit.id)
}
}
package updates
import com.google.gson.FieldNamingPolicy
import fxGui.Main
import io.ktor.client.HttpClient
import io.ktor.client.engine.apache.Apache
import io.ktor.client.features.json.GsonSerializer
import io.ktor.client.features.json.JsonFeature
import io.ktor.client.features.json.serializer.KotlinxSerializer
import io.ktor.client.request.accept
import io.ktor.client.request.get
import io.ktor.http.ContentType
import io.ktor.http.URLProtocol
import io.ktor.http.withCharset
import kotlinx.serialization.json.Json
import org.gitlab.api.v4.tags.Tag
import properties.BuildProperties
import updates.VersionCheck.checkForUpdate
......@@ -74,9 +74,7 @@ object VersionCheck {
private val client by lazy {
HttpClient(Apache) {
install(JsonFeature) {
serializer = GsonSerializer {
setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
}
serializer = KotlinxSerializer(Json { ignoreUnknownKeys = true })
}
}
}
......@@ -136,7 +134,7 @@ object VersionCheck {
versions = fetchTags()
.filter { it.name.matches(versionRegex) }
.map { Version.fromString(it.name.substring(1)) }
.sortedWith(Comparator { o1, o2 -> o1.compareTo(o2) })
.sortedWith { o1, o2 -> o1.compareTo(o2) }
}
/**
......
package util
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import java.text.SimpleDateFormat
import java.util.Date
@ExperimentalSerializationApi
@Serializer(Date::class)
object DateSerializer : KSerializer<Date> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("java.util.Date", PrimitiveKind.STRING)
override fun deserialize(decoder: Decoder): Date = formatter.parse(decoder.decodeString())
override fun serialize(encoder: Encoder, value: Date) = encoder.encodeString(formatter.format(value))
private val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment