From 1c9d8cb9ddfca3b7c8cf80ee18411000f2cf9dc3 Mon Sep 17 00:00:00 2001 From: ringo Date: Fri, 11 Oct 2024 12:42:16 +0400 Subject: [PATCH 1/2] Fix deserialization of RatesCalculationType --- .../v3/jsontransformer/gson/GsonTransformer.kt | 7 +++---- .../RatesCalculationTypeDeserializer.kt | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 src/main/kotlin/com/ecwid/apiclient/v3/jsontransformer/gson/typeadapters/RatesCalculationTypeDeserializer.kt diff --git a/src/main/kotlin/com/ecwid/apiclient/v3/jsontransformer/gson/GsonTransformer.kt b/src/main/kotlin/com/ecwid/apiclient/v3/jsontransformer/gson/GsonTransformer.kt index 2d3dcef08..68b410e2c 100644 --- a/src/main/kotlin/com/ecwid/apiclient/v3/jsontransformer/gson/GsonTransformer.kt +++ b/src/main/kotlin/com/ecwid/apiclient/v3/jsontransformer/gson/GsonTransformer.kt @@ -1,14 +1,12 @@ package com.ecwid.apiclient.v3.jsontransformer.gson import com.ecwid.apiclient.v3.dto.product.result.GetProductFiltersResult.ProductFilters +import com.ecwid.apiclient.v3.dto.profile.result.FetchedStoreProfile.RatesCalculationType import com.ecwid.apiclient.v3.exception.JsonDeserializationException import com.ecwid.apiclient.v3.impl.ParsedResponseWithExt import com.ecwid.apiclient.v3.jsontransformer.JsonTransformer import com.ecwid.apiclient.v3.jsontransformer.PolymorphicType -import com.ecwid.apiclient.v3.jsontransformer.gson.typeadapters.GsonNullableUpdatedValueTypeAdapterFactory -import com.ecwid.apiclient.v3.jsontransformer.gson.typeadapters.GsonPolymorphicDeserializer -import com.ecwid.apiclient.v3.jsontransformer.gson.typeadapters.GsonProductFiltersDeserializer -import com.ecwid.apiclient.v3.jsontransformer.gson.typeadapters.ParsedResponseWithExtDeserializer +import com.ecwid.apiclient.v3.jsontransformer.gson.typeadapters.* import com.google.gson.* class GsonTransformer(polymorphicTypes: List>) : JsonTransformer { @@ -23,6 +21,7 @@ class GsonTransformer(polymorphicTypes: List>) : JsonTransfor gsonBuilder.registerTypeAdapter(ProductFilters::class.java, GsonProductFiltersDeserializer()) gsonBuilder.registerTypeAdapter(ParsedResponseWithExt::class.java, ParsedResponseWithExtDeserializer()) gsonBuilder.registerTypeAdapterFactory(GsonNullableUpdatedValueTypeAdapterFactory) + gsonBuilder.registerTypeAdapter(RatesCalculationType::class.java, RatesCalculationTypeDeserializer()) } .create() diff --git a/src/main/kotlin/com/ecwid/apiclient/v3/jsontransformer/gson/typeadapters/RatesCalculationTypeDeserializer.kt b/src/main/kotlin/com/ecwid/apiclient/v3/jsontransformer/gson/typeadapters/RatesCalculationTypeDeserializer.kt new file mode 100644 index 000000000..11d1e4480 --- /dev/null +++ b/src/main/kotlin/com/ecwid/apiclient/v3/jsontransformer/gson/typeadapters/RatesCalculationTypeDeserializer.kt @@ -0,0 +1,18 @@ +package com.ecwid.apiclient.v3.jsontransformer.gson.typeadapters + +import com.ecwid.apiclient.v3.dto.profile.result.FetchedStoreProfile.RatesCalculationType +import com.google.gson.JsonDeserializationContext +import com.google.gson.JsonDeserializer +import com.google.gson.JsonElement +import java.lang.reflect.Type + +class RatesCalculationTypeDeserializer : JsonDeserializer { + override fun deserialize( + json: JsonElement, + typeOfT: Type, + context: JsonDeserializationContext + ): RatesCalculationType? { + val strValue = json.asString + return RatesCalculationType.entries.firstOrNull { it.toString() == strValue } + } +} From 87d4a45c93b37a69e6d72165f39be775ac7553ce Mon Sep 17 00:00:00 2001 From: ringo Date: Fri, 11 Oct 2024 16:34:29 +0400 Subject: [PATCH 2/2] Refactor conversion into expected string value --- .../apiclient/v3/dto/profile/result/FetchedStoreProfile.kt | 4 ++++ .../gson/typeadapters/RatesCalculationTypeDeserializer.kt | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/ecwid/apiclient/v3/dto/profile/result/FetchedStoreProfile.kt b/src/main/kotlin/com/ecwid/apiclient/v3/dto/profile/result/FetchedStoreProfile.kt index 92a27cbf9..9b10e99dc 100644 --- a/src/main/kotlin/com/ecwid/apiclient/v3/dto/profile/result/FetchedStoreProfile.kt +++ b/src/main/kotlin/com/ecwid/apiclient/v3/dto/profile/result/FetchedStoreProfile.kt @@ -334,6 +334,10 @@ data class FetchedStoreProfile( app; override fun toString(): String { + return asApiString() + } + + fun asApiString(): String { return super.toString().replace("_", "-") } } diff --git a/src/main/kotlin/com/ecwid/apiclient/v3/jsontransformer/gson/typeadapters/RatesCalculationTypeDeserializer.kt b/src/main/kotlin/com/ecwid/apiclient/v3/jsontransformer/gson/typeadapters/RatesCalculationTypeDeserializer.kt index 11d1e4480..85cefa34c 100644 --- a/src/main/kotlin/com/ecwid/apiclient/v3/jsontransformer/gson/typeadapters/RatesCalculationTypeDeserializer.kt +++ b/src/main/kotlin/com/ecwid/apiclient/v3/jsontransformer/gson/typeadapters/RatesCalculationTypeDeserializer.kt @@ -13,6 +13,6 @@ class RatesCalculationTypeDeserializer : JsonDeserializer context: JsonDeserializationContext ): RatesCalculationType? { val strValue = json.asString - return RatesCalculationType.entries.firstOrNull { it.toString() == strValue } + return RatesCalculationType.entries.firstOrNull { it.asApiString() == strValue } } }