Skip to content

Commit a899cb0

Browse files
authored
Merge pull request #1036 from sproctor/fix/dot-in-rest
make filter values comply with postrest rules
2 parents acc2ec9 + 281cc2d commit a899cb0

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

Realtime/src/commonMain/kotlin/io/github/jan/supabase/realtime/PostgresChangeFilter.kt

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ class PostgresChangeFilter(private val event: String, private val schema: String
2727
*/
2828
fun filter(filter: FilterOperation) {
2929
val filterValue = when(filter.operator) {
30-
FilterOperator.EQ, FilterOperator.NEQ, FilterOperator.GT, FilterOperator.GTE, FilterOperator.LT, FilterOperator.LTE -> filter.value.toString()
30+
FilterOperator.EQ,
31+
FilterOperator.NEQ,
32+
FilterOperator.GT,
33+
FilterOperator.GTE,
34+
FilterOperator.LT,
35+
FilterOperator.LTE ->
36+
escapeValue(filter.value)
3137
FilterOperator.IN -> {
3238
if(filter.value is List<*>) {
33-
(filter.value as List<*>).joinToString(",", prefix = "(", postfix = ")") { it.toString() }
39+
(filter.value as List<*>).joinToString(",", prefix = "(", postfix = ")") { escapeValue(it) }
3440
} else {
35-
filter.value.toString()
41+
escapeValue(filter.value)
3642
}
3743
}
3844
else -> throw UnsupportedOperationException("Unsupported filter operator: ${filter.operator}")
@@ -53,4 +59,17 @@ class PostgresChangeFilter(private val event: String, private val schema: String
5359
@SupabaseInternal
5460
fun buildConfig() = PostgresJoinConfig(schema, table, filter, event)
5561

56-
}
62+
}
63+
64+
private val quotedCharacters = listOf(",", ".", ":", "(", ")")
65+
66+
private fun escapeValue(value: Any?): String {
67+
val asString = value.toString()
68+
.replace("\\", "\\\\")
69+
.replace("\"", "\\\"")
70+
return if (quotedCharacters.any { asString.contains(it) }) {
71+
"\"$asString\""
72+
} else {
73+
asString
74+
}
75+
}

0 commit comments

Comments
 (0)