From ca5c134a778f8638f188e34ec758ae7ff1f90757 Mon Sep 17 00:00:00 2001 From: Kristina Koneva Date: Tue, 18 Jun 2024 17:30:09 +0200 Subject: [PATCH 1/5] Explicitly decode Map properties in PrimitiveDeserializerDelegate --- config.gradle | 4 ++-- .../delegates/PrimitiveDeserializerDelegate.kt | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/config.gradle b/config.gradle index 0ed4f22..1bc5586 100644 --- a/config.gradle +++ b/config.gradle @@ -1,7 +1,7 @@ ext { def major = 0 def minor = 0 - def patch = 5 + def patch = 6 buildConfig = [ "minSdk" : 21, @@ -14,4 +14,4 @@ ext { "version" : "$major.$minor.$patch", "versionCode": major * 100 * 100 + minor * 100 + patch ] -} \ No newline at end of file +} diff --git a/halley-core/src/main/kotlin/com/infinum/halley/core/serializers/primitive/delegates/PrimitiveDeserializerDelegate.kt b/halley-core/src/main/kotlin/com/infinum/halley/core/serializers/primitive/delegates/PrimitiveDeserializerDelegate.kt index 997b1cf..a2773c2 100644 --- a/halley-core/src/main/kotlin/com/infinum/halley/core/serializers/primitive/delegates/PrimitiveDeserializerDelegate.kt +++ b/halley-core/src/main/kotlin/com/infinum/halley/core/serializers/primitive/delegates/PrimitiveDeserializerDelegate.kt @@ -10,8 +10,10 @@ import com.infinum.halley.core.serializers.hal.models.HalResource import com.infinum.halley.core.serializers.shared.delegates.DeserializerDelegate import kotlin.reflect.KClass import kotlin.reflect.KProperty1 +import kotlinx.serialization.SerializationException import kotlinx.serialization.builtins.ArraySerializer import kotlinx.serialization.builtins.ListSerializer +import kotlinx.serialization.builtins.MapSerializer import kotlinx.serialization.builtins.SetSerializer import kotlinx.serialization.json.JsonDecoder import kotlinx.serialization.json.JsonNull @@ -53,6 +55,7 @@ internal class PrimitiveDeserializerDelegate( result, jsonObject[name]?.jsonPrimitive?.contentOrNull ) + Map::class -> decodeMap() else -> if (property.isCollection()) { decodeCollection() } else { @@ -106,4 +109,19 @@ internal class PrimitiveDeserializerDelegate( property.setField(result, value) } + + private fun decodeMap() { + val mapType = property.returnType + val keyType = mapType.arguments[0].type ?: throw SerializationException("Cannot determine map key type") + val valueType = mapType.arguments[1].type ?: throw SerializationException("Cannot determine map value type") + + val value = decoder.json.decodeFromJsonElement( + MapSerializer( + decoder.serializersModule.serializer(keyType), + decoder.serializersModule.serializer(valueType) + ), + jsonObject[name]?.jsonObject ?: JsonObject(emptyMap()) + ) + property.setField(result, value) + } } From 7769e9bcbd1d86991fd1a3ca88396364cbc23958 Mon Sep 17 00:00:00 2001 From: Kristina Koneva Date: Wed, 26 Jun 2024 11:03:01 +0200 Subject: [PATCH 2/5] Update proguard rules so that classes extending from HalResource are preserved --- .../resources/META-INF/proguard/halley.pro | 20 +------------------ 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/halley-core/src/main/resources/META-INF/proguard/halley.pro b/halley-core/src/main/resources/META-INF/proguard/halley.pro index 4d9dcca..96c76b5 100644 --- a/halley-core/src/main/resources/META-INF/proguard/halley.pro +++ b/halley-core/src/main/resources/META-INF/proguard/halley.pro @@ -25,22 +25,4 @@ # @Serializable and @Polymorphic are used at runtime for polymorphic serialization. -keepattributes RuntimeVisibleAnnotations,AnnotationDefault -# Serializer for classes with named companion objects are retrieved using `getDeclaredClasses`. -# If you have any, uncomment and replace classes with those containing named companion objects. -#-keepattributes InnerClasses # Needed for `getDeclaredClasses`. -#-if @kotlinx.serialization.Serializable class -#com.example.myapplication.HasNamedCompanion, # <-- List serializable classes with named companions. -#com.example.myapplication.HasNamedCompanion2 -#{ -# static **$* *; -#} -#-keepnames class <1>$$serializer { # -keepnames suffices; class is kept when serializer() is kept. -# static <1>$$serializer INSTANCE; -#} - -# This is generated automatically by the Android Gradle plugin. --dontwarn org.conscrypt.** --dontwarn org.bouncycastle.** --dontwarn org.openjsse.** --dontwarn org.joda.** --dontwarn org.slf4j.** \ No newline at end of file +-keep class * extends com.infinum.halley.core.serializers.hal.models.HalResource { *; } From 48846a452a5aebf1acdc502f89d8b5f7ab80971b Mon Sep 17 00:00:00 2001 From: Kristina Koneva Date: Wed, 26 Jun 2024 13:54:58 +0200 Subject: [PATCH 3/5] Update sample app models --- gradle/libs.versions.toml | 2 +- sample/build.gradle | 18 ++++++------- .../models/deserialization/ProfileResource.kt | 8 +++++- sample/src/main/resources/profile.json | 25 ++++++++++++++++++- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 900e3af..ba5155c 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,5 @@ [versions] -halley = "0.0.5" +halley = "0.0.6" gradle = "7.4.2" kotlin = "1.8.10" serialization = "1.5.0" diff --git a/sample/build.gradle b/sample/build.gradle index 3fc9f52..bc5216b 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -6,7 +6,7 @@ buildscript { } dependencies { classpath libs.tools.gradle - // classpath libs.library.plugin + classpath libs.library.plugin } } @@ -15,7 +15,7 @@ plugins { alias libs.plugins.kotlin.android } -// apply plugin: "com.infinum.halley.plugin" +apply plugin: "com.infinum.halley.plugin" android { compileSdkVersion buildConfig.compileSdk @@ -70,13 +70,13 @@ android { } dependencies { -// implementation libs.library.retrofit -// implementation libs.library.ktor - implementation project(":halley-core") - implementation project(":halley-retrofit") - implementation project(":halley-ktor") + implementation libs.library.retrofit + implementation libs.library.ktor +// implementation project(":halley-core") +// implementation project(":halley-retrofit") +// implementation project(":halley-ktor") -// implementation libs.kotlin.core + implementation libs.kotlin.core implementation libs.bundles.androidx implementation libs.material @@ -88,4 +88,4 @@ dependencies { implementation libs.bundles.coroutines implementation libs.bundles.rxjava -} \ No newline at end of file +} diff --git a/sample/src/main/kotlin/com/infinum/halley/sample/data/models/deserialization/ProfileResource.kt b/sample/src/main/kotlin/com/infinum/halley/sample/data/models/deserialization/ProfileResource.kt index 9573859..1db1c9b 100644 --- a/sample/src/main/kotlin/com/infinum/halley/sample/data/models/deserialization/ProfileResource.kt +++ b/sample/src/main/kotlin/com/infinum/halley/sample/data/models/deserialization/ProfileResource.kt @@ -31,7 +31,13 @@ data class ProfileResource( val user: UserResource? = null, @Transient - val age: Int = 40 + val age: Int = 40, + + @SerialName(value = "favouriteMeals") + val favouriteMeals: Map? = null, + + @SerialName(value = "categorizedAnimals") + val categorizedAnimals: Map>? = null ) : HalResource // CPD-ON diff --git a/sample/src/main/resources/profile.json b/sample/src/main/resources/profile.json index 0bdd709..2d84f77 100644 --- a/sample/src/main/resources/profile.json +++ b/sample/src/main/resources/profile.json @@ -14,6 +14,29 @@ }, "name": "Niko", "timezone": "Europe/Zagreb", + "favouriteMeals": { + "breakfast": "pancakes", + "lunch": "pasta", + "dinner": "lasagna" + }, + "categorizedAnimals": { + "mammals": [ + { + "name": "Lion", + "age": 3 + }, + { + "name": "Tiger", + "type": "2" + } + ], + "birds": [ + { + "name": "Eagle", + "age": 5 + } + ] + }, "_links": { "self": { "href": "http://localhost:8080/api/Profile/self" @@ -22,4 +45,4 @@ "href": "http://localhost:8080/api/Profile/block" } } -} \ No newline at end of file +} From 9d55b3805e4f692a66e4f995d0426c6a8f07bc5d Mon Sep 17 00:00:00 2001 From: Kristina Koneva Date: Thu, 27 Jun 2024 09:34:35 +0200 Subject: [PATCH 4/5] Update proguard rules --- .../resources/META-INF/proguard/halley.pro | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/halley-core/src/main/resources/META-INF/proguard/halley.pro b/halley-core/src/main/resources/META-INF/proguard/halley.pro index 96c76b5..968c827 100644 --- a/halley-core/src/main/resources/META-INF/proguard/halley.pro +++ b/halley-core/src/main/resources/META-INF/proguard/halley.pro @@ -25,4 +25,24 @@ # @Serializable and @Polymorphic are used at runtime for polymorphic serialization. -keepattributes RuntimeVisibleAnnotations,AnnotationDefault +# Serializer for classes with named companion objects are retrieved using `getDeclaredClasses`. +# If you have any, uncomment and replace classes with those containing named companion objects. +#-keepattributes InnerClasses # Needed for `getDeclaredClasses`. +#-if @kotlinx.serialization.Serializable class +#com.example.myapplication.HasNamedCompanion, # <-- List serializable classes with named companions. +#com.example.myapplication.HasNamedCompanion2 +#{ +# static **$* *; +#} +#-keepnames class <1>$$serializer { # -keepnames suffices; class is kept when serializer() is kept. +# static <1>$$serializer INSTANCE; +#} + +# This is generated automatically by the Android Gradle plugin. +-dontwarn org.conscrypt.** +-dontwarn org.bouncycastle.** +-dontwarn org.openjsse.** +-dontwarn org.joda.** +-dontwarn org.slf4j.** + -keep class * extends com.infinum.halley.core.serializers.hal.models.HalResource { *; } From f9962f8073ef054fc5e99e061c5bd69e0a78ee6d Mon Sep 17 00:00:00 2001 From: Kristina Koneva Date: Thu, 27 Jun 2024 09:49:30 +0200 Subject: [PATCH 5/5] Revert halley version --- gradle/libs.versions.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index ba5155c..900e3af 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,5 @@ [versions] -halley = "0.0.6" +halley = "0.0.5" gradle = "7.4.2" kotlin = "1.8.10" serialization = "1.5.0"