Skip to content

Commit c2abda8

Browse files
committed
fix spotify uri deserialization on native (segfault)
Signed-off-by: Adam Ratzman <[email protected]>
1 parent 2b1256e commit c2abda8

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

src/commonMain/kotlin/com.adamratzman.spotify/endpoints/public/BrowseApi.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ public class BrowseApi(api: GenericSpotifyApi) : SpotifyEndpoint(api) {
180180
"limit",
181181
limit
182182
).with("offset", offset)
183-
.with("market", market?.name).toString().apply { println("here1") }
184-
).toPagingObject(SimplePlaylist.serializer(), "playlists", endpoint = this, json = json).apply { println("heref") }
183+
.with("market", market?.name).toString()
184+
).toPagingObject((SimplePlaylist.serializer()), "playlists", endpoint = this, json = json)
185185

186186
/**
187187
* Create a playlist-style listening experience based on seed artists, tracks and genres.

src/commonMain/kotlin/com.adamratzman.spotify/models/SpotifyUris.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class SpotifyUriException(message: String) : SpotifyException.BadRequestE
2020

2121
private fun String.matchType(type: String, allowColon: Boolean): String? {
2222
val uriContent = "[^:]".takeUnless { allowColon } ?: "."
23-
val typeRegex = "^spotify:(?:.*:)*$type:($uriContent*)(?::.*)*$|^([^:]+)$".toRegex()
23+
val typeRegex = "^spotify:(?:.*:)?$type:($uriContent*)(?::.*)*$|^([^:]+)\$".toRegex()
2424
val match = typeRegex.matchEntire(this)?.groupValues ?: return null
2525
return match[1].takeIf { it.isNotBlank() || match[2].isEmpty() } ?: match[2].takeIf { it.isNotEmpty() }
2626
}
@@ -41,7 +41,10 @@ private fun String.remove(type: String, allowColon: Boolean): String {
4141

4242
private class SimpleUriSerializer<T : SpotifyUri>(val ctor: (String) -> T) : KSerializer<T> {
4343
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("SimpleUri", PrimitiveKind.STRING)
44-
override fun deserialize(decoder: Decoder): T = ctor(decoder.decodeString())
44+
override fun deserialize(decoder: Decoder): T {
45+
val str = decoder.decodeString()
46+
return ctor(str)
47+
}
4548
override fun serialize(encoder: Encoder, value: T) = encoder.encodeString(value.uri)
4649
}
4750

@@ -150,7 +153,9 @@ public sealed class CollectionUri(input: String, type: String, allowColon: Boole
150153
@Serializer(forClass = CollectionUri::class)
151154
public companion object : KSerializer<CollectionUri> {
152155
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("CollectionUri", PrimitiveKind.STRING)
153-
override fun deserialize(decoder: Decoder): CollectionUri = CollectionUri(decoder.decodeString())
156+
override fun deserialize(decoder: Decoder): CollectionUri {
157+
return CollectionUri(decoder.decodeString())
158+
}
154159
override fun serialize(encoder: Encoder, value: CollectionUri): Unit = encoder.encodeString(value.uri)
155160

156161
public operator fun invoke(input: String): CollectionUri {
@@ -235,6 +240,7 @@ public class ArtistUri(input: String) : SpotifyUri(input, "artist") {
235240
*/
236241
@Serializable
237242
public class UserUri(input: String) : SpotifyUri(input, "user") {
243+
238244
@Serializer(forClass = UserUri::class)
239245
public companion object : KSerializer<UserUri> by SimpleUriSerializer(::UserUri)
240246
}

src/commonMain/kotlin/com.adamratzman.spotify/models/serialization/SerializationUtils.kt

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ internal inline fun <reified T> String.toList(serializer: KSerializer<List<T>>,
5959
}
6060
}
6161

62-
internal inline fun <reified T : Any> String.toPagingObject(
62+
internal fun <T : Any> String.toPagingObject(
6363
tClazz: KClass<T>,
6464
tSerializer: KSerializer<T>,
6565
innerObjectName: String? = null,
@@ -68,24 +68,18 @@ internal inline fun <reified T : Any> String.toPagingObject(
6868
arbitraryInnerNameAllowed: Boolean = false,
6969
skipInnerNameFirstIfPossible: Boolean = true
7070
): NullablePagingObject<T> {
71-
println("here2")
7271
if (innerObjectName != null || (arbitraryInnerNameAllowed && !skipInnerNameFirstIfPossible)) {
73-
println("here3 $tClazz")
74-
val map = this.parseJson {
75-
val t = (String.serializer() to NullablePagingObject.serializer(tSerializer)).apply { println("serializer done") }
76-
val jsonObjectRoot = (json.parseToJsonElement(this) as JsonObject)
77-
val jsonElement = innerObjectName?.let { jsonObjectRoot[it] } ?: jsonObjectRoot.keys.firstOrNull()?.let { jsonObjectRoot[it] }
78-
?: throw SpotifyException.ParseException("Json element was null for class $tClazz (json $this)")
79-
val objectString = json.encodeToString(jsonElement)
80-
81-
println(objectString)
82-
println(json.decodeFromString<NullablePagingObject<T>>(objectString))
83-
json.decodeFromString(MapSerializer(t.first, t.second), this).apply { println("decoded") }
72+
val jsonObjectRoot = (json.parseToJsonElement(this) as JsonObject)
73+
val jsonElement = innerObjectName?.let { jsonObjectRoot[it] } ?: jsonObjectRoot.keys.firstOrNull()?.let { jsonObjectRoot[it] }
74+
?: throw SpotifyException.ParseException("Json element was null for class $tClazz (json $this)")
75+
//println(jsonElement.toString())
76+
val objectString = jsonElement.toString()
77+
78+
val map = objectString.parseJson {
79+
json.decodeFromString(NullablePagingObject.serializer(tSerializer),this)
8480
}
85-
println("here4")
86-
return (map[innerObjectName] ?: if (arbitraryInnerNameAllowed) map.keys.firstOrNull()?.let { map[it] }
87-
?: error("") else error(""))
88-
.apply {
81+
82+
return map.apply {
8983
this.endpoint = endpoint
9084
this.itemClazz = tClazz
9185
this.items.map { obj ->
@@ -94,7 +88,6 @@ internal inline fun <reified T : Any> String.toPagingObject(
9488
}
9589
}
9690
}
97-
println("here5")
9891

9992
return try {
10093
val pagingObject = this.parseJson { json.decodeFromString(NullablePagingObject.serializer(tSerializer), this) }
@@ -227,8 +220,10 @@ internal fun <T> String.parseJson(producer: String.() -> T): T =
227220
try {
228221
producer(this)
229222
} catch (e: Exception) {
223+
println(e.message)
224+
println(e.cause)
230225
throw SpotifyException.ParseException(
231-
"Unable to parse $this",
226+
"Unable to parse $this (${e.message})",
232227
e
233228
)
234229
}

0 commit comments

Comments
 (0)