From 87d775acdb5129898748456704b7623bbdcf819c Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Sat, 14 Nov 2026 12:59:12 +0100 Subject: [PATCH] Show diffs in the release build again DiffOp took its JSON names from @Json on the enum constants. Moshi reads those by reflection, and R8 drops them in the release build, so every diff of the history and of the changes since the last visit failed with "Something went wrong". The sign is now kept as text and parsed by the generated adapter. A test keeps enums with @Json names out of the API models. Co-Authored-By: Claude Opus 5.5 --- .../mkdocsrestclient/history/HistoryModels.kt | 26 +++++++++----- .../mkdocsrestclient/EnumJsonNamesTest.kt | 35 +++++++++++++++++++ 2 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 rest/src/test/java/de/markusressel/mkdocsrestclient/EnumJsonNamesTest.kt diff --git a/rest/src/main/java/de/markusressel/mkdocsrestclient/history/HistoryModels.kt b/rest/src/main/java/de/markusressel/mkdocsrestclient/history/HistoryModels.kt index 98b3c4e6..e7b4ba53 100644 --- a/rest/src/main/java/de/markusressel/mkdocsrestclient/history/HistoryModels.kt +++ b/rest/src/main/java/de/markusressel/mkdocsrestclient/history/HistoryModels.kt @@ -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 + } } /** diff --git a/rest/src/test/java/de/markusressel/mkdocsrestclient/EnumJsonNamesTest.kt b/rest/src/test/java/de/markusressel/mkdocsrestclient/EnumJsonNamesTest.kt new file mode 100644 index 00000000..8d007b91 --- /dev/null +++ b/rest/src/test/java/de/markusressel/mkdocsrestclient/EnumJsonNamesTest.kt @@ -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) + } +}