Modern Apps git

← Modern-Apps.git

network: append query parameters before the URL fragment

Commit
1c6f8df9714e66bce2e908c720fb8aa8893549ee
Author
Vayun Mathur <[email protected]>
When
2026-12-02 01:08 UTC (4 days ago)
Parents
30b54bf
Symptom: Urls.appendQuery on a URL carrying a fragment (a deep
link, a documentation or share URL) appended the parameters after
the #, where they become part of the fragment: the server never
receives them and the client router does not see a query either, so
the parameters were silently lost. A ? inside the fragment also
made the helper choose & as the separator for a URL with no query
at all.

Fix: split the URL at the first #, decide the separator and append
the encoded parameters against the pre-fragment part only, then
re-attach the fragment unchanged.

Verification: new UrlsFragmentTest failed all 4 cases before the
fix and passes after, alongside the existing UrlsTest;
:library:network:testDebugUnitTest, :library:network:lint and
:library:network:lintVitalRelease are all green.

Diff

library/network/src/main/java/com/vayunmathur/library/network/Urls.kt
diff --git a/library/network/src/main/java/com/vayunmathur/library/network/Urls.kt b/library/network/src/main/java/com/vayunmathur/library/network/Urls.ktindex fa9e986924..c6c407e3f3 100644--- a/library/network/src/main/java/com/vayunmathur/library/network/Urls.kt+++ b/library/network/src/main/java/com/vayunmathur/library/network/Urls.kt@@ -19,16 +19,23 @@ object Urls {      */     fun appendQuery(url: String, params: Map<String, String>): String {         if (params.isEmpty()) return url-        val sb = StringBuilder(url)+        // Query parameters live before the fragment: appended after '#'+        // they would become part of the fragment and never reach a server,+        // and a '?' inside the fragment is not a query separator either.+        val hash = url.indexOf('#')+        val base = if (hash >= 0) url.substring(0, hash) else url+        val fragment = if (hash >= 0) url.substring(hash) else ""+        val sb = StringBuilder(base)         // Whether a separator is still owed before the next parameter.-        var needSeparator = url.endsWith('?') || url.endsWith('&')-        if (!needSeparator) sb.append(if (url.contains('?')) '&' else '?')+        var needSeparator = base.endsWith('?') || base.endsWith('&')+        if (!needSeparator) sb.append(if (base.contains('?')) '&' else '?')         needSeparator = false         for ((k, v) in params) {             if (needSeparator) sb.append('&')             needSeparator = true             sb.append(encode(k)).append('=').append(encode(v))         }+        sb.append(fragment)         return sb.toString()     } 
library/network/src/test/java/com/vayunmathur/library/network/UrlsFragmentTest.kt
diff --git a/library/network/src/test/java/com/vayunmathur/library/network/UrlsFragmentTest.kt b/library/network/src/test/java/com/vayunmathur/library/network/UrlsFragmentTest.ktnew file mode 100644index 0000000000..c277af7559--- /dev/null+++ b/library/network/src/test/java/com/vayunmathur/library/network/UrlsFragmentTest.kt@@ -0,0 +1,46 @@+package com.vayunmathur.library.network++import kotlin.test.Test+import kotlin.test.assertEquals++/**+ * Query parameters belong before the fragment: anything appended after+ * `#` is part of the fragment and never reaches the server (and is not+ * seen as a query by the client either), so [Urls.appendQuery] must+ * insert before it and must decide the separator from the pre-fragment+ * part of the URL only.+ */+class UrlsFragmentTest {++    @Test+    fun appendQueryInsertsBeforeFragment() {+        assertEquals(+            "https://x.test/s?q=1#section",+            Urls.appendQuery("https://x.test/s#section", mapOf("q" to "1")),+        )+    }++    @Test+    fun appendQueryExtendsExistingQueryBeforeFragment() {+        assertEquals(+            "https://x.test/s?a=1&b=2#section",+            Urls.appendQuery("https://x.test/s?a=1#section", mapOf("b" to "2")),+        )+    }++    @Test+    fun questionMarkInsideFragmentDoesNotCountAsQuery() {+        assertEquals(+            "https://x.test/s?q=1#frag?x=1",+            Urls.appendQuery("https://x.test/s#frag?x=1", mapOf("q" to "1")),+        )+    }++    @Test+    fun trailingSeparatorBeforeFragmentIsRespected() {+        assertEquals(+            "https://x.test/s?a=1#section",+            Urls.appendQuery("https://x.test/s?#section", mapOf("a" to "1")),+        )+    }+}