Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,29 @@ data class DiffHunkModel(
val offset: Int? = null,
)

/**
* @param sign `" "`, `"-"` or `"+"` as sent. Kept as text: Moshi reads the JSON names of enum constants by reflection,
* and R8 drops those annotations in the release build, so every diff failed to parse there.
*/
@JsonClass(generateAdapter = true)
data class DiffLineModel(
val op: DiffOp,
@Json(name = "op") val sign: String,
val text: String,
)
) {
constructor(op: DiffOp, text: String) : this(op.sign, text)

enum class DiffOp {
@Json(name = " ")
CONTEXT,
val op: DiffOp get() = DiffOp.of(sign)
}

@Json(name = "-")
REMOVED,
enum class DiffOp(val sign: String) {
CONTEXT(" "),
REMOVED("-"),
ADDED("+");

@Json(name = "+")
ADDED,
companion object {
/** an unknown sign is shown as unchanged */
fun of(sign: String): DiffOp = entries.firstOrNull { it.sign == sign } ?: CONTEXT
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package de.markusressel.mkdocsrestclient

import com.squareup.moshi.Json
import java.io.File
import java.util.jar.JarFile
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

/**
* R8 drops the `@Json` names of enum constants in the release build, and Moshi reads them by reflection, so such an
* enum parses in tests but fails in the app. API models keep those values as text instead.
*/
class EnumJsonNamesTest {

@Test
fun `no enum of the API models takes its JSON names from annotations`() {
val location = File(IMkDocsRestClient::class.java.protectionDomain!!.codeSource.location.toURI())
val classFiles = if (location.isDirectory) {
location.walk().filter { it.extension == "class" }.map { it.relativeTo(location).invariantSeparatorsPath }.toList()
} else {
JarFile(location).use { jar -> jar.entries().toList().map { it.name }.filter { it.endsWith(".class") } }
}
val enums = classFiles
.filter { it.startsWith("de/markusressel/mkdocsrestclient/") }
.map { Class.forName(it.removeSuffix(".class").replace('/', '.'), false, javaClass.classLoader) }
.filter { it.isEnum }
assertTrue(enums.isNotEmpty(), "the classes were found")

val annotated = enums.flatMap { enum ->
enum.declaredFields.filter { it.isEnumConstant && it.isAnnotationPresent(Json::class.java) }.map { "${enum.simpleName}.${it.name}" }
}
assertEquals(emptyList(), annotated)
}
}
Loading