Skip to content

Commit f16861b

Browse files
committed
Make deep link path arguments to not include slash
Slash is used for URI path splitting. Arguments should not include more than one path section. Test: ./gradlew test connectedCheck Fixes: b/184072811
1 parent c473530 commit f16861b

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

navigation/navigation-common/src/androidTest/java/androidx/navigation/NavDestinationAndroidTest.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,26 @@ class NavDestinationAndroidTest {
111111
.isEqualTo(99)
112112
}
113113

114+
@Test
115+
fun matchDeepLinkBestMatchPathTail() {
116+
val destination = NoOpNavigator().createDestination()
117+
118+
destination.addArgument("id", stringArgument())
119+
destination.addDeepLink("www.example.com/users/{id}")
120+
destination.addDeepLink("www.example.com/users/{id}/posts")
121+
122+
val match = destination.matchDeepLink(
123+
Uri.parse("https://www.example.com/users/u43/posts")
124+
)
125+
126+
assertWithMessage("Deep link should match")
127+
.that(match)
128+
.isNotNull()
129+
assertWithMessage("Deep link should extract id argument correctly")
130+
.that(match?.matchingArgs?.getString("id"))
131+
.isEqualTo("u43")
132+
}
133+
114134
@Test
115135
fun matchDeepLinkBestMimeType() {
116136
val destination = NoOpNavigator().createDestination()
@@ -178,6 +198,18 @@ class NavDestinationAndroidTest {
178198
.that(destination.hasDeepLink(deepLink)).isFalse()
179199
}
180200

201+
@Test
202+
fun testIsValidDeepLinkInvalidLinkPathTail() {
203+
val destination = NoOpNavigator().createDestination()
204+
destination.addArgument("testString", stringArgument())
205+
destination.addDeepLink("android-app://androidx.navigation.test/{testString}")
206+
207+
val deepLink = Uri.parse("android-app://androidx.navigation.test/test/extra")
208+
209+
assertWithMessage("Deep link should not match")
210+
.that(destination.hasDeepLink(deepLink)).isFalse()
211+
}
212+
181213
@Test
182214
fun addInDefaultArgs() {
183215
val destination = NoOpNavigator().createDestination()

navigation/navigation-common/src/main/java/androidx/navigation/NavDeepLink.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public class NavDeepLink internal constructor(
7979
arguments.add(argName)
8080
// Use Pattern.quote() to treat the input string as a literal
8181
uriRegex.append(Pattern.quote(uri.substring(appendPos, matcher.start())))
82-
uriRegex.append("(.+?)")
82+
uriRegex.append("([^/]+?)")
8383
appendPos = matcher.end()
8484
exactDeepLink = false
8585
}

0 commit comments

Comments
 (0)