Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Check that all required arguments are present in link
Currently we can open destination with link that doesn't contain required arguments. In this case we will get crash in runtime. This commit adds check that all required arguments are present in link.

Test: ./gradlew test connectedCheck
Fixes: 185527157
  • Loading branch information
osipxd committed Aug 16, 2021
commit d82cea9e8274089dcbe5518d059dae5ddb65a6b9
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,43 @@ class NavDeepLinkTest {
mapOf("param" to intArgument())
)

assertWithMessage("Args should be not be null")
assertWithMessage("Args should not be null")
.that(matchArgs)
.isNotNull()
assertWithMessage("Args bundle should be empty")
.that(matchArgs?.isEmpty)
.isTrue()
}

@Test
fun deepLinkMissingRequiredArgument() {
val deepLinkString = "$DEEP_LINK_EXACT_HTTPS/greeting?title={title}&text={text}"
val deepLink = NavDeepLink(deepLinkString)

val matchArgs = deepLink.getMatchingArguments(
Uri.parse("$DEEP_LINK_EXACT_HTTPS/greeting?title=No%20text"),
mapOf(
"title" to stringArgument(),
"text" to stringArgument()
)
)

assertWithMessage("Args should be null")
.that(matchArgs)
.isNull()
}

@Test
fun deepLinkMissingOptionalArgument() {
val deepLinkString = "$DEEP_LINK_EXACT_HTTPS/greeting?text={text}"
val deepLink = NavDeepLink(deepLinkString)

val matchArgs = deepLink.getMatchingArguments(
Uri.parse("$DEEP_LINK_EXACT_HTTPS/greeting"),
mapOf("text" to stringArgument("Default greeting"))
)

assertWithMessage("Args should not be null")
.that(matchArgs)
.isNotNull()
assertWithMessage("Args bundle should be empty")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ public class NavDeepLink internal constructor(
}
}
}

// Check that all required arguments are present in bundle
for ((argName, argument) in arguments.entries) {
val argumentIsRequired = argument != null && !argument.isDefaultValuePresent
if (argumentIsRequired && !bundle.containsKey(argName)) return null
}

return bundle
}

Expand Down Expand Up @@ -283,7 +290,8 @@ public class NavDeepLink internal constructor(
public class Builder {

/** @suppress */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) public constructor()
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public constructor()

private var uriPattern: String? = null
private var action: String? = null
Expand Down