Skip to content

Commit 6685aa2

Browse files
committed
Fix DecompoundedAttributes serialization
1 parent ec960f3 commit 6685aa2

File tree

6 files changed

+80
-81
lines changed

6 files changed

+80
-81
lines changed

src/commonMain/kotlin/com/algolia/search/model/settings/DecompoundedAttributes.kt

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.algolia.search.model.settings
33
import com.algolia.search.model.Attribute
44
import com.algolia.search.model.search.Language
55
import kotlinx.serialization.*
6-
import kotlinx.serialization.internal.HashMapSerializer
6+
import kotlinx.serialization.internal.PairSerializer
77

88

99
/**
@@ -14,16 +14,12 @@ import kotlinx.serialization.internal.HashMapSerializer
1414
* The goal of decompounding, regarding the previous example, is to index both Baum and Haus separately,
1515
* nstead of as a single word.
1616
*/
17-
@Serializable(DecompoundedAttributes.Companion::class)
17+
@Serializable
1818
public data class DecompoundedAttributes internal constructor(
19-
val map: Map<Language, List<Attribute>>
19+
val language: Language,
20+
val attributes: List<Attribute>
2021
) {
2122

22-
internal constructor(
23-
language: Language,
24-
attributes: List<Attribute>
25-
) : this(mapOf(language to attributes.toList()))
26-
2723
public constructor(
2824
language: Language.German,
2925
vararg attributes: Attribute
@@ -38,19 +34,4 @@ public data class DecompoundedAttributes internal constructor(
3834
language: Language.Dutch,
3935
vararg attributes: Attribute
4036
) : this(language, attributes.toList())
41-
42-
companion object : KSerializer<DecompoundedAttributes> {
43-
44-
private val serializer = HashMapSerializer(Language, Attribute.list)
45-
46-
override val descriptor: SerialDescriptor = serializer.descriptor
47-
48-
override fun serialize(encoder: Encoder, obj: DecompoundedAttributes) {
49-
serializer.serialize(encoder, obj.map)
50-
}
51-
52-
override fun deserialize(decoder: Decoder): DecompoundedAttributes {
53-
return DecompoundedAttributes(serializer.deserialize(decoder))
54-
}
55-
}
5637
}

src/commonMain/kotlin/com/algolia/search/model/settings/Settings.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public data class Settings(
210210
* Engine default: []
211211
* [Documentation][https://www.algolia.com/doc/api-reference/api-parameters/decompoundedAttributes/?language=kotlin]
212212
*/
213-
@SerialName(KeyDecompoundedAttributes) var decompoundedAttributes: List<DecompoundedAttributes>? = null,
213+
@SerialName(KeyDecompoundedAttributes) @Serializable(KSerializerDecompoundedAttributes::class) var decompoundedAttributes: List<DecompoundedAttributes>? = null,
214214

215215
/**
216216
* Characters that should not be automatically normalized by the search engine.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.algolia.search.serialize
2+
3+
import com.algolia.search.model.Attribute
4+
import com.algolia.search.model.search.Language
5+
import com.algolia.search.model.settings.DecompoundedAttributes
6+
import kotlinx.serialization.*
7+
import kotlinx.serialization.internal.HashMapSerializer
8+
import kotlinx.serialization.json.json
9+
10+
11+
public object KSerializerDecompoundedAttributes : KSerializer<List<DecompoundedAttributes>> {
12+
13+
private val serializer = HashMapSerializer(Language, Attribute.list)
14+
15+
override val descriptor: SerialDescriptor = serializer.descriptor
16+
17+
override fun deserialize(decoder: Decoder): List<DecompoundedAttributes> {
18+
val json = decoder.asJsonInput().jsonObject
19+
20+
return json.content.map {
21+
val language = Json.parse(Language.serializer(), it.key)
22+
val attributes = JsonNoDefaults.fromJson(Attribute.serializer().list, it.value.jsonArray)
23+
24+
DecompoundedAttributes(language, attributes)
25+
}
26+
}
27+
28+
override fun serialize(encoder: Encoder, obj: List<DecompoundedAttributes>) {
29+
val json = json {
30+
obj.forEach {
31+
it.language.raw to Json.toJson(Attribute.list, it.attributes)
32+
}
33+
}
34+
35+
encoder.asJsonOutput().encodeJson(json)
36+
}
37+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package serialize.serializer
2+
3+
import attributeA
4+
import attributeB
5+
import com.algolia.search.model.search.Language
6+
import com.algolia.search.model.settings.DecompoundedAttributes
7+
import com.algolia.search.serialize.KSerializerDecompoundedAttributes
8+
import kotlinx.serialization.json.json
9+
import kotlinx.serialization.json.jsonArray
10+
import serialize.TestSerializer
11+
12+
13+
internal class TestKSerializerDecompoundedAttributes :
14+
TestSerializer<List<DecompoundedAttributes>>(KSerializerDecompoundedAttributes) {
15+
16+
override val items = listOf(
17+
listOf(DecompoundedAttributes(Language.German, listOf(attributeA, attributeB))) to json {
18+
Language.German.raw to jsonArray {
19+
+attributeA.raw
20+
+attributeB.raw
21+
}
22+
}
23+
)
24+
}

src/commonTest/kotlin/serialize/settings/TestDecompoundedAttributes.kt

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/commonTest/kotlin/serialize/settings/TestSettings.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package serialize.settings
22

33
import attributeA
4+
import attributeB
45
import attributes
56
import attributesJson
67
import boolean
@@ -58,7 +59,13 @@ internal class TestSettings : TestSerializer<Settings>(Settings.serializer()) {
5859
ignorePlurals = IgnorePlurals.True,
5960
removeStopWords = RemoveStopWords.True,
6061
camelCaseAttributes = attributes,
61-
decompoundedAttributes = listOf(TestDecompoundedAttributes.item),
62+
decompoundedAttributes = listOf(
63+
DecompoundedAttributes(
64+
Language.German,
65+
attributeA,
66+
attributeB
67+
)
68+
),
6269
keepDiacriticsOnCharacters = string,
6370
queryLanguages = listOf(Language.Afrikaans, Language.Albanian),
6471
// Query-rules
@@ -122,7 +129,12 @@ internal class TestSettings : TestSerializer<Settings>(Settings.serializer()) {
122129
KeyIgnorePlurals to boolean
123130
KeyRemoveStopWords to boolean
124131
KeyCamelCaseAttributes to attributesJson
125-
KeyDecompoundedAttributes to jsonArray { +TestDecompoundedAttributes.json }
132+
KeyDecompoundedAttributes to json {
133+
Language.German.raw to jsonArray {
134+
+attributeA.raw
135+
+attributeB.raw
136+
}
137+
}
126138
KeyKeepDiacriticsOnCharacters to string
127139
KeyQueryLanguages to jsonArray {
128140
+Language.Afrikaans.raw

0 commit comments

Comments
 (0)