Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 1.12.1 (unreleased)

- Fix sync status to make `syncStreams` return null when the database is initializing.
- Fix errors when subscribing to Sync Streams with object or array parameters.

## 1.12.0

- Remove the legacy Kotlin sync client. `newSyncClientImplementation` is now the only supported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,54 @@ class SyncStreamTest : AbstractSyncTest(true) {
}
}

@Test
fun `can subscribe with object and array parameters`() =
databaseTest {
// Regression test for https://github.com/powersync-ja/powersync-kotlin/issues/349.
val stream =
database
.syncStream(
"stream",
mapOf(
"a" to
JsonParam.Collection(
listOf(
JsonParam.String("foo"),
JsonParam.String("bar"),
),
),
"b" to JsonParam.Map(mapOf("foo" to JsonParam.String("bar"))),
),
).subscribe()

database.connect(connector, options = getOptions())
turbineScope {
val turbine = database.currentStatus.asFlow().testIn(this)
turbine.waitFor { it.connected && !it.downloading }

requestedSyncStreams shouldHaveSingleElement {
val streams = it.jsonObject["streams"]!!.jsonObject
val subscriptions = streams["subscriptions"]!!.jsonArray

subscriptions shouldHaveSize 1
JsonUtil.json.encodeToString(subscriptions[0]) shouldBe
"""{"stream":"stream","parameters":{"a":["foo","bar"],"b":{"foo":"bar"}},"override_priority":null}"""
true
}

turbine.cancelAndIgnoreRemainingEvents()
}

val streams = database.currentStatus.syncStreams!!
streams shouldHaveSize 1
streams[0].subscription.parameters shouldBe
mapOf(
"a" to listOf("foo", "bar"),
"b" to mapOf("foo" to "bar"),
)
stream.unsubscribe()
}

@Test
fun `subscriptions update while offline`() =
databaseTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.serialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
Expand Down Expand Up @@ -88,16 +89,24 @@ private data class ToTypedEntry(
override val key: String
get() = inner.key
override val value: Any?
get() = inner.value.jsonPrimitive.asData()
get() = inner.value.asData()

companion object {
private fun JsonPrimitive.asData(): Any? =
if (this === JsonNull) {
null
} else if (isString) {
content
} else {
content.jsonNumberOrBoolean()
private fun JsonElement.asData(): Any? =
when (this) {
// Note: Array and object values never appear in a CRUD row (they would be
// represented as strings instead). We also use TypedRows to represent stream
// parameters though, and those can be arrays/objects.
is JsonArray -> map { it.asData() }
is JsonObject -> mapValues { it.value.asData() }
JsonNull -> null
is JsonPrimitive -> {
if (isString) {
content
} else {
content.jsonNumberOrBoolean()
}
}
}

private fun String.jsonNumberOrBoolean(): Any =
Expand Down
Loading